h5cpp  0.5.1
A modern C++ wrapper for the HDF5 C library
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 #include <h5cpp/core/utilities.hpp>
33 
34 namespace hdf5 {
35 
36 template<typename CharT>
37 class FixedLengthStringBuffer : public std::vector<CharT>
38 {
39  public:
40 
41  using std::vector<CharT>::vector;
42 
44  const dataspace::Dataspace &dataspace)
45  {
48  buffer = FixedLengthStringBuffer<CharT>(datatype.size()*static_cast<size_t>(dataspace.size()));
49  else
50  buffer = FixedLengthStringBuffer<CharT>(datatype.size()*dataspace.selection.size());
51 
52  return buffer;
53  }
54 };
55 
56 template<typename T>
58 {
59  using DataType = T;
61 
62  static BufferType create_buffer(const datatype::String &datatype,
63  const dataspace::Dataspace &dataspace)
64  {
65  return BufferType::create(datatype,dataspace);
66  }
67 
69  const dataspace::Dataspace &)
70  {
71  return BufferType();
72  }
73 
75  const dataspace::Dataspace &)
76  {
77  return DataType();
78  }
79 };
80 
81 template<>
82 struct FixedLengthStringTrait<std::string>
83 {
84  using DataType = std::string;
86 
97  static BufferType to_buffer(const DataType &data,
98  const datatype::String &memory_type,
99  const dataspace::Dataspace &memory_space)
100  {
101  BufferType buffer = BufferType::create(memory_type,memory_space);
102 
103  auto buffer_iter = buffer.begin();
104  auto buffer_end = buffer.end();
105  auto data_iter = data.begin();
106  auto data_end = data.end();
107 
108  for(;buffer_iter!=buffer_end && data_iter != data_end; ++buffer_iter,++data_iter)
109  *buffer_iter = *data_iter;
110 
111  return buffer;
112  }
113 
119  static DataType from_buffer(const BufferType &buffer,
120  const datatype::String &,
121  const dataspace::Dataspace &)
122  {
123  return DataType(buffer.begin(),buffer.end());
124  }
125 };
126 
127 template<>
128 struct FixedLengthStringTrait<std::vector<std::string>>
129 {
130  using DataType = std::vector<std::string>;
132 
133  static BufferType to_buffer(const DataType &data,
134  const datatype::String &memory_type,
135  const dataspace::Dataspace &memory_space)
136  {
137  BufferType buffer = BufferType::create(memory_type,memory_space);
138 
139  auto iter = buffer.begin();
140  for(const auto &str: data)
141  {
142  std::copy(str.begin(),str.end(),iter);
143  std::advance(iter,unsigned2signed<ssize_t>(memory_type.size())); //move iterator to the next position
144  }
145 
146  return buffer;
147  }
148 
149  static DataType from_buffer(const BufferType &buffer,
150  const datatype::String &memory_type,
151  const dataspace::Dataspace &)
152  {
153  DataType data;
154 
155  auto start=buffer.begin();
156  while(start!=buffer.end())
157  {
158  auto end = start+unsigned2signed<std::string::difference_type>(memory_type.size());
159  data.push_back(std::string(start,end));
160  start=end;
161  }
162  return data;
163  }
164 };
165 
166 
167 }
Definition: fixed_length_string.hpp:38
static FixedLengthStringBuffer< CharT > create(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition: fixed_length_string.hpp:43
dataspace base class
Definition: dataspace.hpp:41
SelectionManager selection
access to selection manager
Definition: dataspace.hpp:144
virtual hssize_t size() const
number of elements in the dataspace
size_t size() const
return the number of elements in the current selection
SelectionType type() const
get the type of the current selection
string datatype
Definition: string.hpp:40
size_t size() const override
get current size of the string type
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
top-level namespace of the entire library
Definition: attribute.hpp:45
std::string DataType
Definition: fixed_length_string.hpp:84
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:119
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:97
std::vector< std::string > DataType
Definition: fixed_length_string.hpp:130
static DataType from_buffer(const BufferType &buffer, const datatype::String &memory_type, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:149
static BufferType to_buffer(const DataType &data, const datatype::String &memory_type, const dataspace::Dataspace &memory_space)
Definition: fixed_length_string.hpp:133
Definition: fixed_length_string.hpp:58
FixedLengthStringBuffer< char > BufferType
Definition: fixed_length_string.hpp:60
static BufferType create_buffer(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition: fixed_length_string.hpp:62
static DataType from_buffer(const BufferType &, const datatype::String &, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:74
T DataType
Definition: fixed_length_string.hpp:59
static BufferType to_buffer(const DataType &, const datatype::String &, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:68