h5cpp  0.4.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 
40 
41 namespace hdf5 {
42 namespace node {
43 class Group;
44 }
45 }
46 
47 namespace hdf5 {
48 namespace file {
49 
51 {
52  public:
63  File() = default;
64 
70  File(const File &) = default;
71 
77  File(File &&) = default;
78 
86  explicit File(ObjectHandle &&handle);
87 
88  File &operator=(const File &) = default;
89 
95  AccessFlags intent() const;
96 
102  size_t size() const;
103 
109  ssize_t buffer_size() const;
110 
117  void flush(Scope scope) const;
118 
124  void close();
125 
131  fs::path path() const;
132 
138  size_t count_open_objects(SearchFlags flag) const;
139  size_t count_open_objects(SearchFlagsBase flags) const;
140 
152 
153  explicit operator hid_t() const
154  {
155  return static_cast<hid_t>(handle_);
156  }
157 
167  template<typename T>
168  size_t to_buffer(T &data) const;
169 
178  bool is_valid() const;
179 
185  ObjectId id() const;
186 
187  private:
188  ObjectHandle handle_;
189 
190 };
191 
192 template<typename T>
193 size_t File::to_buffer(T &data) const
194 {
195  auto memory_space = hdf5::dataspace::create(data);
196  auto memory_type = hdf5::datatype::create(data);
197  size_t databytesize = memory_space.size() * memory_type.size();
198  ssize_t s = 0;
199  if(memory_type.get_class() == datatype::Class::INTEGER)
200  {
201  flush(Scope::GLOBAL);
202  s = H5Fget_file_image(static_cast<hid_t>(*this), dataspace::ptr(data), databytesize);
203  if (s < 0)
204  {
205  error::Singleton::instance().throw_with_stack("Failure retrieving the image buffer to to small dataspace");
206  }
207  }
208  else
209  {
210  std::stringstream ss;
211  ss<<"Failure retrieving the image buffer to non-integer dataspace";
213  }
214  return (hsize_t) s;
215 }
216 
217 
218 } // namespace file
219 } // namespace hdf5
TypeTrait< T >::DataspaceType create(const T &value)
factory function for dataspaces
Definition: type_trait.hpp:115
static Singleton & instance()
reference to singleton
Definition: error.hpp:50
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
indicates an integer type
Definition: group_access.hpp:35
Wrapper for hid_t object identifiers.
Definition: object_handle.hpp:66
Definition: group.hpp:46
SearchFlags
flags controlling object search in a file
Definition: types.hpp:79
Definition: attribute.hpp:43
Scope
file scope
Definition: types.hpp:101
TypeTrait< typename std::remove_const< T >::type >::TypeClass create(const T &v=T())
factory function for creating data types
Definition: factory.hpp:38
void * ptr(T &value)
Definition: type_trait.hpp:120
#define DLL_EXPORT
Definition: windows.hpp:35
std::underlying_type< SearchFlags >::type SearchFlagsBase
Definition: types.hpp:90
Definition: file.hpp:50
Unique ID of an HDF5 object.
Definition: object_id.hpp:50
AccessFlags
flags controlling file opening and creation
Definition: types.hpp:37