h5cpp  0.5.0
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 }
hdf5::datatype::String::size
size_t size() const override
get current size of the string type
hdf5::dataspace::SelectionManager::size
size_t size() const
return the number of elements in the current selection
hdf5::dataspace::Dataspace::selection
SelectionManager selection
access to selection manager
Definition: dataspace.hpp:144
hdf5::FixedLengthStringTrait::from_buffer
static DataType from_buffer(const BufferType &, const datatype::String &, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:74
string.hpp
hdf5::FixedLengthStringTrait::DataType
T DataType
Definition: fixed_length_string.hpp:59
hdf5::node::copy
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
hdf5::FixedLengthStringTrait::to_buffer
static BufferType to_buffer(const DataType &, const datatype::String &, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:68
hdf5::FixedLengthStringTrait< std::vector< std::string > >::from_buffer
static DataType from_buffer(const BufferType &buffer, const datatype::String &memory_type, const dataspace::Dataspace &)
Definition: fixed_length_string.hpp:149
hdf5::FixedLengthStringTrait< std::string >::DataType
std::string DataType
Definition: fixed_length_string.hpp:84
hdf5::FixedLengthStringTrait< std::string >::from_buffer
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
hdf5::FixedLengthStringBuffer::create
static FixedLengthStringBuffer< CharT > create(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition: fixed_length_string.hpp:43
hdf5::FixedLengthStringTrait< std::vector< std::string > >::to_buffer
static BufferType to_buffer(const DataType &data, const datatype::String &memory_type, const dataspace::Dataspace &memory_space)
Definition: fixed_length_string.hpp:133
hdf5::FixedLengthStringTrait
Definition: fixed_length_string.hpp:57
hdf5::FixedLengthStringBuffer
Definition: fixed_length_string.hpp:37
hdf5::datatype::String
string datatype
Definition: string.hpp:39
hdf5::FixedLengthStringTrait< std::vector< std::string > >::DataType
std::vector< std::string > DataType
Definition: fixed_length_string.hpp:130
hdf5::dataspace::SelectionManager::type
SelectionType type() const
get the type of the current selection
hdf5::dataspace::Dataspace
dataspace base class
Definition: dataspace.hpp:41
hdf5::dataspace::Dataspace::size
virtual hssize_t size() const
number of elements in the dataspace
dataspace.hpp
hdf5::dataspace::SelectionType::All
@ All
hdf5::FixedLengthStringTrait< std::string >::to_buffer
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
hdf5::FixedLengthStringTrait::BufferType
FixedLengthStringBuffer< char > BufferType
Definition: fixed_length_string.hpp:60
utilities.hpp
hdf5
top-level namespace of the entire library
Definition: attribute.hpp:45
hdf5::FixedLengthStringTrait::create_buffer
static BufferType create_buffer(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition: fixed_length_string.hpp:62