h5cpp 0.7.1
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>
38#include <h5cpp/error/error.hpp>
40
41
42namespace hdf5 {
43namespace file {
44
58DLL_EXPORT File create(const fs::path &path,
62DLL_EXPORT File create(const fs::path &path,
63 AccessFlagsBase flags,
66
78DLL_EXPORT File open(const fs::path &path,
81DLL_EXPORT File open(const fs::path &path,
82 AccessFlagsBase flags,
84
92DLL_EXPORT bool is_hdf5_file(const fs::path &path);
93
104template<typename T>
105File from_buffer(T &data,
107template<typename T>
108File from_buffer(T &data,
109 ImageFlagsBase flags);
110template<typename T>
111File from_buffer(T &data,
112 const datatype::Datatype &mem_type,
113 const dataspace::Dataspace &mem_space,
114 ImageFlagsBase flags);
115
116template<typename T>
117File from_buffer(const T &data,
119template<typename T>
120File from_buffer(const T &data,
121 ImageFlagsBase flags);
122
123
124
125template<typename T>
127{
128 return from_buffer(data, static_cast<ImageFlagsBase>(flags));
129}
130
131template<typename T>
132File from_buffer(const T &data, ImageFlags flags)
133{
134 return from_buffer(data, static_cast<ImageFlagsBase>(flags));
135}
136
137template<typename T>
138File 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
145template<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
153template<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
Wrapper for hid_t object identifiers.
Definition: object_handle.hpp:67
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
file access property list
Definition: file_access.hpp:78
dataset creation property list
Definition: file_creation.hpp:38
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
File open(const fs::path &path, AccessFlags flags=AccessFlags::ReadOnly, const property::FileAccessList &fapl=property::FileAccessList())
open an existing file
AccessFlags
flags controlling file opening and creation
Definition: types.hpp:40
File from_buffer(T &data, ImageFlags flags=ImageFlags::ReadOnly)
load an image file from a buffer
Definition: functions.hpp:126
ImageFlags
flags controlling image file opening and getting
Definition: types.hpp:68
std::underlying_type< ImageFlags >::type ImageFlagsBase
Definition: types.hpp:76
bool is_hdf5_file(const fs::path &path)
check if the file reference by the path is an HDF5 file
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
std::underlying_type< AccessFlags >::type AccessFlagsBase
Definition: types.hpp:52
top-level namespace of the entire library
Definition: attribute.hpp:45
#define DLL_EXPORT
Definition: windows.hpp:29