h5cpp  0.5.1
A modern C++ wrapper for the HDF5 C library
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 <algorithm>
30 #include <cstring>
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 
67  static BufferType to_buffer(const T &)
68  {
69  return BufferType();
70  }
71 
82  static void from_buffer(const BufferType &, DataType &)
83  { }
84 };
85 
86 template<>
87 struct VarLengthStringTrait<std::string>
88 {
90  using DataType = std::string;
91 
92 
93  static BufferType to_buffer(const DataType &data)
94  {
95  return BufferType{const_cast<char*>(data.c_str())};
96  }
97 
98  static void from_buffer(const BufferType &buffer,DataType &data)
99  {
100  data = DataType(buffer[0],std::strlen(buffer[0]));
101  }
102 };
103 
104 template<>
105 struct VarLengthStringTrait<std::vector<std::string>>
106 {
108  using DataType = std::vector<std::string>;
109 
110  static BufferType to_buffer(const DataType &data)
111  {
112  BufferType buffer;
113  std::transform(data.begin(),data.end(),std::back_inserter(buffer),
114  [](const std::string &str)
115  {
116  return const_cast<char*>(str.c_str());
117  });
118  return buffer;
119  }
120 
121  static void from_buffer(const BufferType &buffer,DataType &data)
122  {
123  std::transform(buffer.begin(),buffer.end(),data.begin(),
124  [](const char *ptr)
125  {
126  return std::string(ptr,std::strlen(ptr));
127  });
128  }
129 };
130 
131 
132 }
void * ptr(T &value)
Definition: type_trait.hpp:111
top-level namespace of the entire library
Definition: attribute.hpp:45
std::vector< CharT * > VarLengthStringBuffer
buffer for variable length string data
Definition: variable_length_string.hpp:44
static BufferType to_buffer(const DataType &data)
Definition: variable_length_string.hpp:93
std::string DataType
Definition: variable_length_string.hpp:90
static void from_buffer(const BufferType &buffer, DataType &data)
Definition: variable_length_string.hpp:98
VarLengthStringBuffer< char > BufferType
Definition: variable_length_string.hpp:89
VarLengthStringBuffer< char > BufferType
Definition: variable_length_string.hpp:107
static void from_buffer(const BufferType &buffer, DataType &data)
Definition: variable_length_string.hpp:121
std::vector< std::string > DataType
Definition: variable_length_string.hpp:108
static BufferType to_buffer(const DataType &data)
Definition: variable_length_string.hpp:110
variable length string buffer trait
Definition: variable_length_string.hpp:54
static void from_buffer(const BufferType &, DataType &)
copy data to output
Definition: variable_length_string.hpp:82
static BufferType to_buffer(const T &)
create buffer with write data
Definition: variable_length_string.hpp:67
VarLengthStringBuffer< char > BufferType
Definition: variable_length_string.hpp:55
T DataType
Definition: variable_length_string.hpp:56