h5cpp  0.4.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>
34 #include <h5cpp/core/windows.hpp>
38 #include <h5cpp/error/error.hpp>
39 
40 
41 namespace hdf5 {
42 namespace file {
43 
57 DLL_EXPORT File create(const fs::path &path,
59  const property::FileCreationList &fcpl = property::FileCreationList(),
60  const property::FileAccessList &fapl = property::FileAccessList());
61 DLL_EXPORT File create(const fs::path &path,
62  AccessFlagsBase flags,
63  const property::FileCreationList &fcpl = property::FileCreationList(),
64  const property::FileAccessList &fapl = property::FileAccessList());
65 
77 DLL_EXPORT File open(const fs::path &path,
79  const property::FileAccessList &fapl = property::FileAccessList());
80 DLL_EXPORT File open(const fs::path &path,
81  AccessFlagsBase flags,
82  const property::FileAccessList &fapl = property::FileAccessList());
83 
91 DLL_EXPORT bool is_hdf5_file(const fs::path &path);
92 
103 template<typename T>
104 File from_buffer(T &data,
106 template<typename T>
107 File from_buffer(T &data,
108  ImageFlagsBase flags);
109 
110 template<typename T>
111 File from_buffer(const T &data,
113 template<typename T>
114 File from_buffer(const T &data,
115  ImageFlagsBase flags);
116 
117 
118 
119 template<typename T>
120 File from_buffer(T &data, ImageFlags flags)
121 {
122  return from_buffer(data, static_cast<ImageFlagsBase>(flags));
123 }
124 
125 template<typename T>
126 File from_buffer(const 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, ImageFlagsBase flags)
133 {
134  if((flags & static_cast<AccessFlagsBase>(ImageFlags::READWRITE)) &&
135  (flags & static_cast<AccessFlagsBase>(ImageFlags::DONT_COPY)))
136  throw std::runtime_error("Invalid ImageFlags for const buffer");
137  return from_buffer(const_cast<T&>(data), static_cast<ImageFlagsBase>(flags));
138 }
139 
140 template<typename T>
142 {
143  auto memory_space = hdf5::dataspace::create(data);
144  auto memory_type = hdf5::datatype::create(data);
145  size_t databytesize = memory_space.size() * memory_type.size();
146  hid_t fid = 0;
147  if(memory_type.get_class() == datatype::Class::INTEGER)
148  {
149 
150  fid = H5LTopen_file_image(dataspace::ptr(data), databytesize, flags);
151  if (fid < 0)
152  {
153  std::stringstream ss;
154  ss << "Failure opening file image";
156  }
157  return File(ObjectHandle(fid));
158  }
159  else
160  {
161  std::stringstream ss;
162  ss<<"Failure to open non-integer buffer";
164  }
165  return File(hdf5::ObjectHandle(fid));
166 }
167 
168 } // namespace file
169 } // namespace hdf5
170 
171 
172 
173 
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
bool is_hdf5_file(const fs::path &path)
check if the file reference by the path is an HDF5 file
Wrapper for hid_t object identifiers.
Definition: object_handle.hpp:66
File from_buffer(T &data, ImageFlags flags=ImageFlags::READONLY)
load an image file from a buffer
Definition: functions.hpp:120
ImageFlags
flags controlling image file opening and getting
Definition: types.hpp:59
std::underlying_type< ImageFlags >::type ImageFlagsBase
Definition: types.hpp:68
Definition: attribute.hpp:43
std::underlying_type< AccessFlags >::type AccessFlagsBase
Definition: types.hpp:48
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
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
Definition: file.hpp:50
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:37