h5cpp  0.3.3
A modern C++ wrapper for the HDF5 C library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
variable_length_string.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2017 DESY,ESS
3 // 2018 Eugen Wintersberger
4 //
5 // This file is part of h5cpp.
6 //
7 // This library is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation; either version 2.1 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this library; if not, write to the
19 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
20 // Boston, MA 02110-1301 USA
21 // ===========================================================================
22 //
23 // Author: Eugen Wintersberger <eugen.wintersberger@gmail.com>
24 // Created on: Apr 4, 2019
25 //
26 #pragma once
27 
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 
32 namespace hdf5 {
33 
43 template<typename CharT>
44 using VarLengthStringBuffer = std::vector<CharT*>;
45 
52 template<typename T>
54 {
56  using DataType = T;
57 
68  static BufferType to_buffer(const T &)
69  {
70  return BufferType();
71  }
72 
83  static void from_buffer(const BufferType &, DataType &)
84  { }
85 };
86 
87 template<>
88 struct VarLengthStringTrait<std::string>
89 {
91  using DataType = std::string;
92 
93 
94  static BufferType to_buffer(const DataType &data)
95  {
96  return BufferType{const_cast<char*>(data.c_str())};
97  }
98 
99  static void from_buffer(const BufferType &buffer,DataType &data)
100  {
101  data = DataType(buffer[0],std::strlen(buffer[0]));
102  }
103 };
104 
105 template<>
106 struct VarLengthStringTrait<std::vector<std::string>>
107 {
109  using DataType = std::vector<std::string>;
110 
111  static BufferType to_buffer(const DataType &data)
112  {
113  BufferType buffer;
114  std::transform(data.begin(),data.end(),std::back_inserter(buffer),
115  [](const std::string &str)
116  {
117  return const_cast<char*>(str.c_str());
118  });
119  return buffer;
120  }
121 
122  static void from_buffer(const BufferType &buffer,DataType &data)
123  {
124  std::transform(buffer.begin(),buffer.end(),data.begin(),
125  [](const char *ptr)
126  {
127  return std::string(ptr,std::strlen(ptr));
128  });
129  }
130 };
131 
132 
133 }
static BufferType to_buffer(const T &)
create buffer with write data
Definition: variable_length_string.hpp:68
VarLengthStringBuffer< char > BufferType
Definition: variable_length_string.hpp:55
static void from_buffer(const BufferType &buffer, DataType &data)
Definition: variable_length_string.hpp:99
VarLengthStringBuffer< char > BufferType
Definition: variable_length_string.hpp:108
std::vector< CharT * > VarLengthStringBuffer
buffer for variable length string data
Definition: variable_length_string.hpp:44
Definition: attribute.hpp:43
std::vector< std::string > DataType
Definition: variable_length_string.hpp:109
std::string DataType
Definition: variable_length_string.hpp:91
static BufferType to_buffer(const DataType &data)
Definition: variable_length_string.hpp:111
static BufferType to_buffer(const DataType &data)
Definition: variable_length_string.hpp:94
void * ptr(T &value)
Definition: type_trait.hpp:120
VarLengthStringBuffer< char > BufferType
Definition: variable_length_string.hpp:90
T DataType
Definition: variable_length_string.hpp:56
static void from_buffer(const BufferType &, DataType &)
copy data to output
Definition: variable_length_string.hpp:83
variable length string buffer trait
Definition: variable_length_string.hpp:53
static void from_buffer(const BufferType &buffer, DataType &data)
Definition: variable_length_string.hpp:122