HighFive 2.7.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
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#pragma once
10
11#include <string>
12
13#include <H5Fpublic.h>
14
15#include "../H5Utility.hpp"
16#include "H5Utils.hpp"
17
18namespace HighFive {
19
20namespace { // unnamed
21
22// libhdf5 uses a preprocessor trick on their oflags
23// we can not declare them constant without a mapper
24inline 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
40inline File::File(const std::string& filename,
41 unsigned openFlags,
42 const FileAccessProps& fileAccessProps)
43 : File(filename, openFlags, FileCreateProps::Default(), fileAccessProps) {}
44
45
46inline File::File(const std::string& filename,
47 unsigned openFlags,
48 const FileCreateProps& fileCreateProps,
49 const FileAccessProps& fileAccessProps) {
50 openFlags = convert_open_flag(openFlags);
51
52 unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
53 unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
54 bool mustCreate = createMode > 0;
55 bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
56
57 // open is default. It's skipped only if flags require creation
58 // If open fails it will try create() if H5F_ACC_CREAT is set
59 if (!mustCreate) {
60 // Silence open errors if create is allowed
61 std::unique_ptr<SilenceHDF5> silencer;
62 if (openOrCreate)
63 silencer.reset(new SilenceHDF5());
64
65 _hid = H5Fopen(filename.c_str(), openMode, fileAccessProps.getId());
66
67 if (isValid())
68 return; // Done
69
70 if (openOrCreate) {
71 // Will attempt to create ensuring wont clobber any file
72 createMode = H5F_ACC_EXCL;
73 } else {
75 std::string("Unable to open file " + filename));
76 }
77 }
78
79 auto fcpl = fileCreateProps.getId();
80 auto fapl = fileAccessProps.getId();
81 if ((_hid = H5Fcreate(filename.c_str(), createMode, fcpl, fapl)) < 0) {
82 HDF5ErrMapper::ToException<FileException>(std::string("Unable to create file " + filename));
83 }
84}
85
86inline const std::string& File::getName() const noexcept {
87 if (_filename.empty()) {
88 _filename = details::get_name(
89 [this](char* buffer, size_t length) { return H5Fget_name(getId(), buffer, length); });
90 }
91 return _filename;
92}
93
94inline hsize_t File::getMetadataBlockSize() const {
95 auto fapl = getAccessPropertyList();
96 return MetadataBlockSize(fapl).getSize();
97}
98
99inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {
100 auto fapl = getAccessPropertyList();
101 auto fileVer = FileVersionBounds(fapl);
102 return fileVer.getVersion();
103}
104
105#if H5_VERSION_GE(1, 10, 1)
106inline H5F_fspace_strategy_t File::getFileSpaceStrategy() const {
107 auto fcpl = getCreatePropertyList();
108 FileSpaceStrategy spaceStrategy(fcpl);
109 return spaceStrategy.getStrategy();
110}
111
112inline hsize_t File::getFileSpacePageSize() const {
113 auto fcpl = getCreatePropertyList();
114
115 if (getFileSpaceStrategy() != H5F_FSPACE_STRATEGY_PAGE) {
117 std::string("Cannot obtain page size as paged allocation is not used."));
118 }
119
120 return FileSpacePageSize(fcpl).getPageSize();
121}
122#endif
123
124inline void File::flush() {
125 if (H5Fflush(_hid, H5F_SCOPE_GLOBAL) < 0) {
126 HDF5ErrMapper::ToException<FileException>(std::string("Unable to flush file " + getName()));
127 }
128}
129
130} // namespace HighFive
File class.
Definition H5File.hpp:24
FileCreateProps getCreatePropertyList() const
Get the list of properties for creation of this file.
Definition H5File.hpp:104
std::pair< H5F_libver_t, H5F_libver_t > getVersionBounds() const
Returns the HDF5 version compatibility bounds.
Definition H5File_misc.hpp:99
void flush()
flush
Definition H5File_misc.hpp:124
FileAccessProps getAccessPropertyList() const
Get the list of properties for accession of this file.
Definition H5File.hpp:109
hsize_t getMetadataBlockSize() const
Returns the block size for metadata in bytes.
Definition H5File_misc.hpp:94
@ Truncate
Open flag: Truncate a file if already existing.
Definition H5File.hpp:34
@ Create
Open flag: Create non existing file.
Definition H5File.hpp:40
@ ReadOnly
Open flag: Read only access.
Definition H5File.hpp:30
@ ReadWrite
Open flag: Read Write access.
Definition H5File.hpp:32
@ Excl
Open flag: Open will fail if file already exist.
Definition H5File.hpp:36
File()=default
const std::string & getName() const noexcept
Return the name of the file.
Definition H5File_misc.hpp:86
Configure the version bounds for the file.
Definition H5PropertyList.hpp:246
Configure the metadata block size to use writing to files.
Definition H5PropertyList.hpp:266
hsize_t getSize() const
Definition H5PropertyList_misc.hpp:287
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:65
bool isValid() const noexcept
isValid
Definition H5Object_misc.hpp:61
hid_t _hid
Definition H5Object.hpp:105
HDF5 property Lists.
Definition H5PropertyList.hpp:79
Utility class to disable HDF5 stack printing inside a scope.
Definition H5Utility.hpp:24
Definition H5_definitions.hpp:15
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:42