hops
H5File_misc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #ifndef H5FILE_MISC_HPP
10 #define H5FILE_MISC_HPP
11 
12 #include <string>
13 
14 #include <H5Fpublic.h>
15 
16 #include "../H5Utility.hpp"
17 
18 namespace HighFive {
19 
20 namespace { // unnamed
21 
22 // libhdf5 uses a preprocessor trick on their oflags
23 // we can not declare them constant without a mapper
24 inline unsigned convert_open_flag(unsigned openFlags) {
25  unsigned res_open = 0;
26  if (openFlags & File::ReadOnly)
27  res_open |= H5F_ACC_RDONLY;
28  if (openFlags & File::ReadWrite)
29  res_open |= H5F_ACC_RDWR;
30  if (openFlags & File::Create)
31  res_open |= H5F_ACC_CREAT;
32  if (openFlags & File::Truncate)
33  res_open |= H5F_ACC_TRUNC;
34  if (openFlags & File::Excl)
35  res_open |= H5F_ACC_EXCL;
36  return res_open;
37 }
38 } // namespace
39 
40 
41 inline File::File(const std::string& filename, unsigned openFlags,
42  const FileAccessProps& fileAccessProps)
43  : _filename(filename) {
44 
45  openFlags = convert_open_flag(openFlags);
46 
47  unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
48  unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
49  bool mustCreate = createMode > 0;
50  bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
51 
52  // open is default. It's skipped only if flags require creation
53  // If open fails it will try create() if H5F_ACC_CREAT is set
54  if (!mustCreate) {
55  // Silence open errors if create is allowed
56  std::unique_ptr<SilenceHDF5> silencer;
57  if (openOrCreate) silencer.reset(new SilenceHDF5());
58 
59  _hid = H5Fopen(_filename.c_str(), openMode, fileAccessProps.getId());
60 
61  if (isValid()) return; // Done
62 
63  if (openOrCreate) {
64  // Will attempt to create ensuring wont clobber any file
65  createMode = H5F_ACC_EXCL;
66  } else {
67  HDF5ErrMapper::ToException<FileException>(
68  std::string("Unable to open file " + _filename));
69  }
70  }
71 
72  if ((_hid = H5Fcreate(_filename.c_str(), createMode, H5P_DEFAULT,
73  fileAccessProps.getId())) < 0) {
74  HDF5ErrMapper::ToException<FileException>(
75  std::string("Unable to create file " + _filename));
76  }
77 }
78 
79 inline const std::string& File::getName() const noexcept {
80  return _filename;
81 }
82 
83 inline void File::flush() {
84  if (H5Fflush(_hid, H5F_SCOPE_GLOBAL) < 0) {
85  HDF5ErrMapper::ToException<FileException>(
86  std::string("Unable to flush file " + _filename));
87  }
88 }
89 
90 } // namespace HighFive
91 
92 #endif // H5FILE_MISC_HPP
HighFive::Object::isValid
bool isValid() const noexcept
isValid
Definition: H5Object_misc.hpp:51
HighFive::File::File
File(const std::string &filename, unsigned openFlags=ReadOnly, const FileAccessProps &fileAccessProps=FileDriver())
File.
Definition: H5File_misc.hpp:41
HighFive::SilenceHDF5
Utility class to disable HDF5 stack printing inside a scope.
Definition: H5Utility.hpp:20
HighFive::File::Excl
@ Excl
Open flag: Open will fail if file already exist.
Definition: H5File.hpp:39
HighFive::PropertyList
Base HDF5 property List.
Definition: H5_definitions.hpp:48
HighFive::File::Create
@ Create
Open flag: Create non existing file.
Definition: H5File.hpp:43
HighFive::File::Truncate
@ Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:37
HighFive::PropertyList::getId
hid_t getId() const
Definition: H5PropertyList.hpp:56
HighFive::File::ReadWrite
@ ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:35
HighFive::Object::_hid
hid_t _hid
Definition: H5Object.hpp:81
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
HighFive::File::ReadOnly
@ ReadOnly
Open flag: Read only access.
Definition: H5File.hpp:33
HighFive::File::flush
void flush()
flush
Definition: H5File_misc.hpp:83
HighFive::File::getName
const std::string & getName() const noexcept
Return the name of the file.
Definition: H5File_misc.hpp:79
HighFive
Definition: H5_definitions.hpp:15