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
array_adapter.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2018 DESY,ESS
3 //
4 // This file is part of h5cpp.
5 //
6 // This library is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation; either version 2.1 of the License, or
9 // (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the
18 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
19 // Boston, MA 02110-1301 USA
20 // ===========================================================================
21 //
22 // Author: Eugen Wintersberger <eugen.wintersberger@gmail.com>
23 // Created on: Jul 1, 2018
24 //
25 #pragma once
26 
27 #include <cstdint>
28 #include <vector>
29 
30 #include <h5cpp/core/types.hpp>
33 
34 namespace hdf5 {
35 
56 template<typename T> class ArrayAdapter
57 {
58  private:
59  T *data_;
60  size_t size_;
61 
68  void reset() noexcept
69  {
70  data_ = nullptr;
71  size_ = 0;
72  }
73 
74  public:
79  data_(nullptr),
80  size_(0)
81  {}
82 
89  ArrayAdapter(T *data,size_t size):
90  data_(data),
91  size_(size)
92  {}
93 
97  ArrayAdapter(const ArrayAdapter<T> &adapter);
98 
102  ArrayAdapter(ArrayAdapter<T> &&adapter);
103 
107  ArrayAdapter<T> &operator=(const ArrayAdapter<T> &adapter);
108 
113 
114  size_t size() const noexcept
115  {
116  return size_;
117  }
118 
119  T *data() noexcept
120  {
121  return data_;
122  }
123 
124  const T *data() const noexcept
125  {
126  return data_;
127  }
128 };
129 
130 template<typename T>
132  data_(adapter.data_),
133  size_(adapter.size_)
134 {
135 }
136 
137 template<typename T>
139  data_(adapter.data_),
140  size_(adapter.size_)
141 {
142  //declare the original adapter instance as invalid
143  adapter.reset();
144 }
145 
146 template<typename T>
148 {
149  if(this == &adapter)
150  return *this;
151 
152  data_ = adapter.data_;
153  size_ = adapter.size_;
154  return *this;
155 }
156 
157 template<typename T>
159 {
160  if(this == &adapter)
161  return *this;
162 
163  data_ = adapter.data_;
164  size_ = adapter.size_;
165  adapter.reset();
166 
167  return *this;
168 }
169 
170 
181 template<typename T>
183 {
184  return Dimensions{adapter.size()};
185 }
186 
187 
188 namespace datatype {
189 
197 template<typename T>
198 class TypeTrait<ArrayAdapter<T>>
199 {
200  public:
201  using TypeClass = typename TypeTrait<T>::TypeClass;
202 
203  static TypeClass create(const ArrayAdapter<T> & = ArrayAdapter<T>())
204  {
205  return TypeTrait<T>::create();
206  }
207 
208 };
209 
210 }
211 
212 namespace dataspace {
213 
221 template<typename T>
223 {
224  public:
226 
227  static DataspaceType create(const ArrayAdapter<T> &adapter)
228  {
229  Dimensions adapter_dimensions = get_dimensions(adapter);
230  return Simple(adapter_dimensions,adapter_dimensions);
231  }
232 
233  static void* ptr(ArrayAdapter<T> &adapter)
234  {
235  return reinterpret_cast<void*>(adapter.data());
236  }
237 
238  static const void *cptr(const ArrayAdapter<T> &adapter)
239  {
240  return reinterpret_cast<const void*>(adapter.data());
241  }
242 
243 };
244 
245 }
246 
247 }
TypeTrait< T >::DataspaceType create(const T &value)
factory function for dataspaces
Definition: type_trait.hpp:115
size_t size() const noexcept
Definition: array_adapter.hpp:114
static const void * cptr(const ArrayAdapter< T > &adapter)
Definition: array_adapter.hpp:238
static DataspaceType create(const ArrayAdapter< T > &adapter)
Definition: array_adapter.hpp:227
Dimensions get_dimensions(const ArrayAdapter< T > adapter)
get dimensions for an array adapter instance
Definition: array_adapter.hpp:182
Definition: attribute.hpp:43
ArrayAdapter()
default constructor
Definition: array_adapter.hpp:78
type trait for dataspace construction
Definition: type_trait.hpp:51
T * data() noexcept
Definition: array_adapter.hpp:119
ArrayAdapter(T *data, size_t size)
constructor
Definition: array_adapter.hpp:89
ArrayAdapter< T > & operator=(const ArrayAdapter< T > &adapter)
copy assignment
Definition: array_adapter.hpp:147
simple multidimensional dataspace
Definition: simple.hpp:39
const T * data() const noexcept
Definition: array_adapter.hpp:124
std::vector< hsize_t > Dimensions
Definition: types.hpp:36
static void * ptr(ArrayAdapter< T > &adapter)
Definition: array_adapter.hpp:233
adapter for C-style arrays
Definition: array_adapter.hpp:56