h5cpp  0.5.1
A modern C++ wrapper for the HDF5 C library
file.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2017 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:
23 // Eugen Wintersberger <eugen.wintersberger@desy.de>
24 // Jan Kotanski <jan.kotanski@desy.de>
25 // Created on: Sep 8, 2017
26 //
27 #pragma once
28 
29 #include <h5cpp/file/types.hpp>
31 #include <h5cpp/core/windows.hpp>
33 #include <h5cpp/core/object_id.hpp>
38 #include <h5cpp/error/error.hpp>
39 #include <h5cpp/core/utilities.hpp>
40 
41 
42 namespace hdf5 {
43 namespace node {
44 class Group;
45 }
46 }
47 
48 namespace hdf5 {
49 namespace file {
50 
52 {
53  public:
64  File() = default;
65 
71  File(const File &) = default;
72 
78  File(File &&) = default;
79 
87  explicit File(ObjectHandle &&handle);
88 
89  File &operator=(const File &) = default;
90 
97 
103  size_t size() const;
104 
110  ssize_t buffer_size() const;
111 
118  void flush(Scope scope) const;
119 
125  void close();
126 
132  fs::path path() const;
133 
139  size_t count_open_objects(SearchFlags flag) const;
140  size_t count_open_objects(SearchFlagsBase flags) const;
141 
153 
154  explicit operator hid_t() const
155  {
156  return static_cast<hid_t>(handle_);
157  }
158 
168  template<typename T>
169  size_t to_buffer(T &data) const;
170 
184  template<typename T>
185  size_t to_buffer_reshape(T &data,
186  const datatype::Datatype &mem_type,
187  const dataspace::Dataspace &mem_space) const;
188 
197  bool is_valid() const;
198 
204  ObjectId id() const;
205 
206  private:
207  ObjectHandle handle_;
208 
209 };
210 
211 template<typename T>
212 size_t File::to_buffer(T &data) const
213 {
214  auto mem_space = hdf5::dataspace::create(data);
215  hdf5::datatype::DatatypeHolder mem_type_holder;
216  return to_buffer_reshape(data, mem_type_holder.get(data), mem_space);
217 }
218 
219 template<typename T>
220 size_t File::to_buffer_reshape(T &data,
221  const datatype::Datatype &mem_type,
222  const dataspace::Dataspace &mem_space) const
223 {
224  size_t databytesize = signed2unsigned<unsigned long long>(mem_space.size()) * mem_type.size();
225  ssize_t s = 0;
226  if(mem_type.get_class() == datatype::Class::Integer)
227  {
229  s = H5Fget_file_image(static_cast<hid_t>(*this), dataspace::ptr(data), databytesize);
230  if (s < 0)
231  {
232  error::Singleton::instance().throw_with_stack("Failure retrieving the image buffer to to small dataspace");
233  }
234  }
235  else
236  {
237  std::stringstream ss;
238  ss<<"Failure retrieving the image buffer to non-integer dataspace";
240  }
241  return static_cast<hsize_t>(s);
242 }
243 
244 
245 } // namespace file
246 } // namespace hdf5
Wrapper for hid_t object identifiers.
Definition: object_handle.hpp:67
Unique ID of an HDF5 object.
Definition: object_id.hpp:51
dataspace base class
Definition: dataspace.hpp:41
virtual hssize_t size() const
number of elements in the dataspace
data type object holder
Definition: factory.hpp:54
const Datatype & get(const T &v=T{})
factory holder method for getting reference of data types
Definition: factory.hpp:74
base class for all data types
Definition: datatype.hpp:42
Class get_class() const
returns the datatypes class
virtual size_t size() const
get size of type in bytes
static Singleton & instance()
reference to singleton
Definition: error.hpp:59
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
Definition: file.hpp:52
ObjectId id() const
return a unique id for the file object
File()=default
default constructor
File & operator=(const File &)=default
size_t to_buffer(T &data) const
retrieve a copy of the image of an existing, open file
Definition: file.hpp:212
File(ObjectHandle &&handle)
constructor
File(File &&)=default
move constructor
size_t count_open_objects(SearchFlags flag) const
count number of open objects
AccessFlags intent() const
get access flags for the file
ssize_t buffer_size() const
get the buffer size in bytes
bool is_valid() const
check validity of the instance
void close()
close the file
size_t to_buffer_reshape(T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space) const
retrieve a copy of the image of an existing, open file
Definition: file.hpp:220
size_t size() const
get the file size in bytes
node::Group root(const property::GroupAccessList &gapl=property::GroupAccessList()) const
get root group
size_t count_open_objects(SearchFlagsBase flags) const
void flush(Scope scope) const
flush the file
fs::path path() const
get path on file system
File(const File &)=default
copy constructor
Definition: group.hpp:51
Definition: group_access.hpp:35
void * ptr(T &value)
Definition: type_trait.hpp:111
TypeTrait< T >::DataspaceType create(const T &value)
factory function for dataspaces
Definition: type_trait.hpp:89
@ Integer
indicates an integer type
Scope
file scope
Definition: types.hpp:118
AccessFlags
flags controlling file opening and creation
Definition: types.hpp:40
std::underlying_type< SearchFlags >::type SearchFlagsBase
Definition: types.hpp:102
SearchFlags
flags controlling object search in a file
Definition: types.hpp:92
top-level namespace of the entire library
Definition: attribute.hpp:45
#define DLL_EXPORT
Definition: windows.hpp:29