h5cpp  0.5.0
A modern C++ wrapper for the HDF5 C library
functions.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2017 DESY,ESS
3 //
4 // This file is part of h5pp.
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 
30 #include <h5cpp/file/file.hpp>
31 #include <h5cpp/file/types.hpp>
34 #include <h5cpp/core/windows.hpp>
38 #include <h5cpp/error/error.hpp>
39 #include <h5cpp/core/utilities.hpp>
40 
41 
42 namespace hdf5 {
43 namespace file {
44 
58 DLL_EXPORT File create(const fs::path &path,
60  const property::FileCreationList &fcpl = property::FileCreationList(),
61  const property::FileAccessList &fapl = property::FileAccessList());
62 DLL_EXPORT File create(const fs::path &path,
63  AccessFlagsBase flags,
64  const property::FileCreationList &fcpl = property::FileCreationList(),
65  const property::FileAccessList &fapl = property::FileAccessList());
66 
78 DLL_EXPORT File open(const fs::path &path,
80  const property::FileAccessList &fapl = property::FileAccessList());
81 DLL_EXPORT File open(const fs::path &path,
82  AccessFlagsBase flags,
83  const property::FileAccessList &fapl = property::FileAccessList());
84 
92 DLL_EXPORT bool is_hdf5_file(const fs::path &path);
93 
104 template<typename T>
105 File from_buffer(T &data,
107 template<typename T>
108 File from_buffer(T &data,
109  ImageFlagsBase flags);
110 template<typename T>
111 File from_buffer(T &data,
112  const datatype::Datatype &mem_type,
113  const dataspace::Dataspace &mem_space,
114  ImageFlagsBase flags);
115 
116 template<typename T>
117 File from_buffer(const T &data,
119 template<typename T>
120 File from_buffer(const T &data,
121  ImageFlagsBase flags);
122 
123 
124 
125 template<typename T>
126 File from_buffer(T &data, ImageFlags flags)
127 {
128  return from_buffer(data, static_cast<ImageFlagsBase>(flags));
129 }
130 
131 template<typename T>
132 File from_buffer(const T &data, ImageFlags flags)
133 {
134  return from_buffer(data, static_cast<ImageFlagsBase>(flags));
135 }
136 
137 template<typename T>
138 File from_buffer(const T &data, ImageFlagsBase flags)
139 {
140  if((flags & ImageFlags::ReadWrite) && (flags & ImageFlags::DontCopy))
141  throw std::runtime_error("Invalid ImageFlags for const buffer: the DONT_COPY flag together with the READWRITE flag");
142  return from_buffer(const_cast<T&>(data), static_cast<ImageFlagsBase>(flags));
143 }
144 
145 template<typename T>
147 {
148  auto mem_space = hdf5::dataspace::create(data);
149  hdf5::datatype::DatatypeHolder mem_type_holder;
150  return from_buffer(data, mem_type_holder.get(data), mem_space, flags);
151 }
152 
153 template<typename T>
155  const datatype::Datatype &mem_type,
156  const dataspace::Dataspace &mem_space,
157  ImageFlagsBase flags)
158 {
159  if ((flags & ImageFlags::DontCopy) && !(flags & ImageFlags::DontRelease))
160  throw std::runtime_error("Invalid ImageFlags in from_buffer: the DONT_COPY flag without the DONT_RELEASE flag");
161  size_t databytesize = signed2unsigned<size_t>(mem_space.size()) * mem_type.size();
162  hid_t fid = 0;
163  if(mem_type.get_class() == datatype::Class::Integer)
164  {
165 
166  fid = H5LTopen_file_image(dataspace::ptr(data), databytesize, flags);
167  if (fid < 0)
168  {
169  std::stringstream ss;
170  ss << "Failure opening file image";
172  }
173  return File(ObjectHandle(fid));
174  }
175  else
176  {
177  std::stringstream ss;
178  ss<<"Failure to open non-integer buffer";
180  }
181  return File(hdf5::ObjectHandle(fid));
182 }
183 
184 } // namespace file
185 } // namespace hdf5
186 
187 
188 
189 
hdf5::dataspace::create
TypeTrait< T >::DataspaceType create(const T &value)
factory function for dataspaces
Definition: type_trait.hpp:89
factory.hpp
hdf5::datatype::Datatype::size
virtual size_t size() const
get size of type in bytes
hdf5::file::SearchFlags::File
@ File
hdf5::file::create
File create(const fs::path &path, AccessFlags flags=AccessFlags::Exclusive, const property::FileCreationList &fcpl=property::FileCreationList(), const property::FileAccessList &fapl=property::FileAccessList())
create a new file
type_trait.hpp
hdf5::file::AccessFlags::ReadOnly
@ ReadOnly
file_access.hpp
file_creation.hpp
hdf5::file::ImageFlags::ReadWrite
@ ReadWrite
hdf5::datatype::DatatypeHolder::get
const Datatype & get(const T &v=T())
factory holder method for getting reference of data types
Definition: factory.hpp:74
hdf5::datatype::DatatypeHolder
data type object holder
Definition: factory.hpp:53
filesystem.hpp
hdf5::datatype::Datatype::get_class
Class get_class() const
returns the datatypes class
hdf5::file::File
Definition: file.hpp:51
hdf5::file::ImageFlags::ReadOnly
@ ReadOnly
hdf5::file::AccessFlags::Exclusive
@ Exclusive
hdf5::file::open
File open(const fs::path &path, AccessFlags flags=AccessFlags::ReadOnly, const property::FileAccessList &fapl=property::FileAccessList())
open an existing file
hdf5::file::is_hdf5_file
bool is_hdf5_file(const fs::path &path)
check if the file reference by the path is an HDF5 file
file.hpp
hdf5::file::ImageFlags
ImageFlags
flags controlling image file opening and getting
Definition: types.hpp:67
windows.hpp
hdf5::dataspace::ptr
void * ptr(T &value)
Definition: type_trait.hpp:111
hdf5::error::Singleton::instance
static Singleton & instance()
reference to singleton
Definition: error.hpp:59
hdf5::file::AccessFlagsBase
std::underlying_type< AccessFlags >::type AccessFlagsBase
Definition: types.hpp:52
hdf5::error::Singleton::throw_with_stack
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
hdf5::file::AccessFlags
AccessFlags
flags controlling file opening and creation
Definition: types.hpp:39
types.hpp
hdf5::ObjectHandle
Wrapper for hid_t object identifiers.
Definition: object_handle.hpp:66
hdf5::file::ImageFlagsBase
std::underlying_type< ImageFlags >::type ImageFlagsBase
Definition: types.hpp:76
hdf5::datatype::Class::Integer
@ Integer
indicates an integer type
DLL_EXPORT
#define DLL_EXPORT
Definition: windows.hpp:29
hdf5::dataspace::Dataspace
dataspace base class
Definition: dataspace.hpp:41
hdf5::file::ImageFlags::DontCopy
@ DontCopy
hdf5::file::ImageFlags::DontRelease
@ DontRelease
hdf5::dataspace::Dataspace::size
virtual hssize_t size() const
number of elements in the dataspace
dataspace.hpp
hdf5::file::from_buffer
File from_buffer(T &data, ImageFlags flags=ImageFlags::ReadOnly)
load an image file from a buffer
Definition: functions.hpp:126
utilities.hpp
hdf5::datatype::Datatype
base class for all data types
Definition: datatype.hpp:41
hdf5
top-level namespace of the entire library
Definition: attribute.hpp:45
error.hpp