h5cpp 0.7
A modern C++ wrapper for the HDF5 C library
Loading...
Searching...
No Matches
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// Authors: Eugen Wintersberger <eugen.wintersberger@gmail.com>
23// Jan Kotanski <jan.kotanski@desy.de>
24// Created on: Jul 1, 2018
25//
26#pragma once
27
28#include <cstdint>
29#include <vector>
30
31#include <h5cpp/core/types.hpp>
34
35namespace hdf5 {
36
57template<typename T> class ArrayAdapter
58{
59 private:
60 T *data_;
61 size_t size_;
62
69 void reset() noexcept
70 {
71 data_ = nullptr;
72 size_ = 0;
73 }
74
75 public:
80 data_(nullptr),
81 size_(0)
82 {}
83
91 data_(data),
92 size_(size)
93 {}
94
98 ArrayAdapter(const ArrayAdapter<T> &adapter);
99
103 ArrayAdapter(ArrayAdapter<T> &&adapter);
104
109
114
115 size_t size() const noexcept
116 {
117 return size_;
118 }
119
120 T *data() noexcept
121 {
122 return data_;
123 }
124
125 const T *data() const noexcept
126 {
127 return data_;
128 }
129};
130
131template<typename T>
133 data_(adapter.data_),
134 size_(adapter.size_)
135{
136}
137
138template<typename T>
140 data_(adapter.data_),
141 size_(adapter.size_)
142{
143 //declare the original adapter instance as invalid
144 adapter.reset();
145}
146
147template<typename T>
149{
150 if(this == &adapter)
151 return *this;
152
153 data_ = adapter.data_;
154 size_ = adapter.size_;
155 return *this;
156}
157
158template<typename T>
160{
161 if(this == &adapter)
162 return *this;
163
164 data_ = adapter.data_;
165 size_ = adapter.size_;
166 adapter.reset();
167
168 return *this;
169}
170
171
182template<typename T>
184{
185 return Dimensions{adapter.size()};
186}
187
188
189namespace datatype {
190
198template<typename T>
199class TypeTrait<ArrayAdapter<T>>
200{
201 public:
202 using TypeClass = typename TypeTrait<T>::TypeClass;
203
204 static TypeClass create(const ArrayAdapter<T> & = ArrayAdapter<T>())
205 {
206 return TypeTrait<T>::create();
207 }
208 const static TypeClass & get(const ArrayAdapter<T> & = ArrayAdapter<T>()) {
209 const static TypeClass & cref_ = create();
210 return cref_;
211 }
212
213};
214
215}
216
217namespace dataspace {
218
226template<typename T>
228{
229 public:
231
232 static DataspaceType create(const ArrayAdapter<T> &adapter)
233 {
234 Dimensions adapter_dimensions = get_dimensions(adapter);
235 return Simple(adapter_dimensions,adapter_dimensions);
236 }
237
238 const static DataspaceType & get(const ArrayAdapter<T> &adapter, dataspace::DataspacePool &pool) {
239 return pool.getSimple(get_dimensions(adapter));
240 }
241
242 static void* ptr(ArrayAdapter<T> &adapter)
243 {
244 return reinterpret_cast<void*>(adapter.data());
245 }
246
247 static const void *cptr(const ArrayAdapter<T> &adapter)
248 {
249 return reinterpret_cast<const void*>(adapter.data());
250 }
251
252};
253
254}
255
256}
adapter for C-style arrays
Definition array_adapter.hpp:58
ArrayAdapter< T > & operator=(const ArrayAdapter< T > &adapter)
copy assignment
Definition array_adapter.hpp:148
size_t size() const noexcept
Definition array_adapter.hpp:115
const T * data() const noexcept
Definition array_adapter.hpp:125
ArrayAdapter()
default constructor
Definition array_adapter.hpp:79
ArrayAdapter(T *data, size_t size)
constructor
Definition array_adapter.hpp:90
T * data() noexcept
Definition array_adapter.hpp:120
data space object pool
Definition pool.hpp:42
const Simple & getSimple(size_t size)
reference of Simple data spaces
simple multidimensional dataspace
Definition simple.hpp:43
static void * ptr(ArrayAdapter< T > &adapter)
Definition array_adapter.hpp:242
static const DataspaceType & get(const ArrayAdapter< T > &adapter, dataspace::DataspacePool &pool)
Definition array_adapter.hpp:238
static DataspaceType create(const ArrayAdapter< T > &adapter)
Definition array_adapter.hpp:232
static const void * cptr(const ArrayAdapter< T > &adapter)
Definition array_adapter.hpp:247
type trait for dataspace construction
Definition type_trait.hpp:54
Datatype TypeClass
subtype of Datatype which will be used
Definition type_trait.hpp:58
static TypeClass create(const T &=T())
create the new type instance
static const TypeClass & get(const T &=T())
reference to const static type instance
top-level namespace of the entire library
Definition attribute.hpp:45
std::vector< hsize_t > Dimensions
Definition types.hpp:32
Dimensions get_dimensions(const ArrayAdapter< T > adapter)
get dimensions for an array adapter instance
Definition array_adapter.hpp:183