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
fixed_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>
32 
33 namespace hdf5 {
34 
35 template<typename CharT>
36 class FixedLengthStringBuffer : public std::vector<CharT>
37 {
38  public:
39 
40  using std::vector<CharT>::vector;
41 
43  const dataspace::Dataspace &dataspace)
44  {
47  buffer = FixedLengthStringBuffer<CharT>(datatype.size()*dataspace.size());
48  else
49  buffer = FixedLengthStringBuffer<CharT>(datatype.size()*dataspace.selection.size());
50 
51  return buffer;
52  }
53 };
54 
55 template<typename T>
57 {
58  using DataType = T;
60 
61  static BufferType create_buffer(const datatype::String &datatype,
62  const dataspace::Dataspace &dataspace)
63  {
64  return BufferType::create(datatype,dataspace);
65  }
66 
68  const dataspace::Dataspace &)
69  {
70  return BufferType();
71  }
72 
74  const dataspace::Dataspace &)
75  {
76  return DataType();
77  }
78 };
79 
80 template<>
81 struct FixedLengthStringTrait<std::string>
82 {
83  using DataType = std::string;
85 
96  static BufferType to_buffer(const DataType &data,
97  const datatype::String &memory_type,
98  const dataspace::Dataspace &memory_space)
99  {
100  BufferType buffer = BufferType::create(memory_type,memory_space);
101 
102  auto buffer_iter = buffer.begin();
103  auto buffer_end = buffer.end();
104  auto data_iter = data.begin();
105  auto data_end = data.end();
106 
107  for(;buffer_iter!=buffer_end && data_iter != data_end; ++buffer_iter,++data_iter)
108  *buffer_iter = *data_iter;
109 
110  return buffer;
111  }
112 
118  static DataType from_buffer(const BufferType &buffer,
119  const datatype::String &,
120  const dataspace::Dataspace &)
121  {
122  return DataType(buffer.begin(),buffer.end());
123  }
124 };
125 
126 template<>
127 struct FixedLengthStringTrait<std::vector<std::string>>
128 {
129  using DataType = std::vector<std::string>;
131 
132  static BufferType to_buffer(const DataType &data,
133  const datatype::String &memory_type,
134  const dataspace::Dataspace &memory_space)
135  {
136  BufferType buffer = BufferType::create(memory_type,memory_space);
137 
138  auto iter = buffer.begin();
139  for(const auto &str: data)
140  {
141  std::copy(str.begin(),str.end(),iter);
142  std::advance(iter,memory_type.size()); //move iterator to the next position
143  }
144 
145  return buffer;
146  }
147 
148  static DataType from_buffer(const BufferType &buffer,
149  const datatype::String &memory_type,
150  const dataspace::Dataspace &)
151  {
152  DataType data;
153 
154  auto start=buffer.begin();
155  while(start!=buffer.end())
156  {
157  auto end = start+memory_type.size();
158  data.push_back(std::string(start,end));
159  start=end;
160  }
161  return data;
162  }
163 };
164 
165 
166 }
TypeTrait< T >::DataspaceType create(const T &value)
factory function for dataspaces
Definition: type_trait.hpp:115
dataspace base class
Definition: dataspace.hpp:41
string datatype
Definition: string.hpp:39
size_t size() const override
get current size of the string type
static DataType from_buffer(const BufferType &buffer, const datatype::String &, const dataspace::Dataspace &)
store data from buffer in target memory
Definition: fixed_length_string.hpp:118
SelectionType type() const
get the type of the current selection
static FixedLengthStringBuffer< CharT > create(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition: fixed_length_string.hpp:42
static DataType from_buffer(const BufferType &buffer, const datatype::String &memory_type, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:148
static BufferType to_buffer(const DataType &, const datatype::String &, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:67
DLL_EXPORT void copy(const Node &source, const Group &base, const Path &relative_path, const property::ObjectCopyList &ocpl=property::ObjectCopyList(), const property::LinkCreationList &lcpl=property::LinkCreationList())
copy node object
std::string DataType
Definition: fixed_length_string.hpp:83
std::vector< std::string > DataType
Definition: fixed_length_string.hpp:129
virtual hssize_t size() const
number of elements in the dataspace
Definition: attribute.hpp:43
Definition: fixed_length_string.hpp:36
static BufferType create_buffer(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition: fixed_length_string.hpp:61
size_t size() const
return the number of elements in the current selection
SelectionManager selection
access to selection manager
Definition: dataspace.hpp:151
T DataType
Definition: fixed_length_string.hpp:58
Definition: fixed_length_string.hpp:56
static BufferType to_buffer(const DataType &data, const datatype::String &memory_type, const dataspace::Dataspace &memory_space)
create fixed length string buffer from data
Definition: fixed_length_string.hpp:96
static DataType from_buffer(const BufferType &, const datatype::String &, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:73
static BufferType to_buffer(const DataType &data, const datatype::String &memory_type, const dataspace::Dataspace &memory_space)
Definition: fixed_length_string.hpp:132