HighFive 3.1.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#include "h5f_wrapper.hpp"
18
19namespace HighFive {
20
21namespace { // unnamed
22
23// libhdf5 uses a preprocessor trick on their oflags
24// we can not declare them constant without a mapper
25inline unsigned convert_open_flag(File::AccessMode openFlags) {
26 unsigned res_open = 0;
27 if (any(openFlags & File::ReadOnly))
28 res_open |= H5F_ACC_RDONLY;
29 if (any(openFlags & File::ReadWrite))
30 res_open |= H5F_ACC_RDWR;
31 if (any(openFlags & File::Create))
32 res_open |= H5F_ACC_CREAT;
33 if (any(openFlags & File::Truncate))
34 res_open |= H5F_ACC_TRUNC;
35 if (any(openFlags & File::Excl))
36 res_open |= H5F_ACC_EXCL;
37#if H5_VERSION_GE(1, 10, 0)
38 if (any(openFlags & File::ReadSWMR))
39 res_open |= H5F_ACC_SWMR_READ | H5F_ACC_RDONLY;
40 if (any(openFlags & File::WriteSWMR))
41 res_open |= H5F_ACC_SWMR_WRITE | H5F_ACC_RDWR;
42#else
43 if (any(openFlags & (File::ReadSWMR | File::WriteSWMR)))
44 throw FileException("Your HDF5 library is too old for SWMR mode, you need at least 1.10");
45#endif
46 return res_open;
47}
48} // namespace
49
50inline File::File(const std::string& filename,
51 AccessMode openFlags,
52 const FileAccessProps& fileAccessProps)
53 : File(filename, openFlags, FileCreateProps::Default(), fileAccessProps) {}
54
55
56inline File::File(const std::string& filename,
57 AccessMode access_mode,
58 const FileCreateProps& fileCreateProps,
59 const FileAccessProps& fileAccessProps) {
60 unsigned openFlags = convert_open_flag(access_mode);
61
62 unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
63 unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
64#if H5_VERSION_GE(1, 10, 0)
65 openMode |= openFlags & (H5F_ACC_SWMR_READ | H5F_ACC_SWMR_WRITE);
66#endif
67 bool mustCreate = createMode > 0;
68 bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
69
70 // open is default. It's skipped only if flags require creation
71 // If open fails it will try create() if H5F_ACC_CREAT is set
72 if (!mustCreate) {
73 // Silence open errors if create is allowed
74 std::unique_ptr<SilenceHDF5> silencer;
75 if (openOrCreate) {
76 silencer = std::make_unique<SilenceHDF5>();
77 }
78
79 _hid = detail::nothrow::h5f_open(filename.c_str(), openMode, fileAccessProps.getId());
80
81 if (isValid()) {
82 return; // Done
83 }
84
85 if (openOrCreate) {
86 // Will attempt to create ensuring wont clobber any file
87 createMode = H5F_ACC_EXCL;
88 } else {
89 HDF5ErrMapper::ToException<FileException>(
90 std::string("Unable to open file " + filename));
91 }
92 }
93
94 auto fcpl = fileCreateProps.getId();
95 auto fapl = fileAccessProps.getId();
96 _hid = detail::h5f_create(filename.c_str(), createMode, fcpl, fapl);
97}
98
99inline const std::string& File::getName() const {
100 if (_filename.empty()) {
101 _filename = details::get_name([this](char* buffer, size_t length) {
102 return detail::h5f_get_name(getId(), buffer, length);
103 });
104 }
105 return _filename;
106}
107
108inline hsize_t File::getMetadataBlockSize() const {
109 auto fapl = getAccessPropertyList();
110 return MetadataBlockSize(fapl).getSize();
111}
112
113inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {
114 auto fapl = getAccessPropertyList();
115 auto fileVer = FileVersionBounds(fapl);
116 return fileVer.getVersion();
117}
118
119#if H5_VERSION_GE(1, 10, 1)
120inline H5F_fspace_strategy_t File::getFileSpaceStrategy() const {
121 auto fcpl = getCreatePropertyList();
122 FileSpaceStrategy spaceStrategy(fcpl);
123 return spaceStrategy.getStrategy();
124}
125
126inline hsize_t File::getFileSpacePageSize() const {
127 auto fcpl = getCreatePropertyList();
128
129 if (getFileSpaceStrategy() != H5F_FSPACE_STRATEGY_PAGE) {
130 HDF5ErrMapper::ToException<FileException>(
131 std::string("Cannot obtain page size as paged allocation is not used."));
132 }
133
134 return FileSpacePageSize(fcpl).getPageSize();
135}
136#endif
137
138inline void File::flush() {
139 detail::h5f_flush(_hid, H5F_SCOPE_GLOBAL);
140}
141
142#if H5_VERSION_GE(1, 10, 0)
143inline void File::startSWMRWrite() {
144 detail::h5f_start_swmr_write(_hid);
145}
146#endif
147
148inline size_t File::getFileSize() const {
149 hsize_t sizeValue = 0;
150 detail::h5f_get_filesize(_hid, &sizeValue);
151 return static_cast<size_t>(sizeValue);
152}
153
154inline size_t File::getFreeSpace() const {
155 return static_cast<size_t>(detail::h5f_get_freespace(_hid));
156}
157
158} // namespace HighFive
File class.
Definition H5File.hpp:25
FileCreateProps getCreatePropertyList() const
Get the list of properties for creation of this file.
Definition H5File.hpp:137
size_t getFileSize() const
Get the size of this file in bytes.
Definition H5File_misc.hpp:148
static constexpr AccessMode Truncate
Definition H5File.hpp:55
static constexpr AccessMode Create
Definition H5File.hpp:58
std::pair< H5F_libver_t, H5F_libver_t > getVersionBounds() const
Returns the HDF5 version compatibility bounds.
Definition H5File_misc.hpp:113
void flush()
flush
Definition H5File_misc.hpp:138
static constexpr AccessMode ReadOnly
Definition H5File.hpp:53
size_t getFreeSpace() const
Get the amount of tracked, unused space in bytes.
Definition H5File_misc.hpp:154
AccessMode
Definition H5File.hpp:29
static constexpr AccessMode WriteSWMR
Definition H5File.hpp:62
FileAccessProps getAccessPropertyList() const
Get the list of properties for accession of this file.
Definition H5File.hpp:142
hsize_t getMetadataBlockSize() const
Returns the block size for metadata in bytes.
Definition H5File_misc.hpp:108
static constexpr AccessMode Excl
Definition H5File.hpp:56
File()=default
const std::string & getName() const
Return the name of the file.
Definition H5File_misc.hpp:99
static constexpr AccessMode ReadWrite
Definition H5File.hpp:54
static constexpr AccessMode ReadSWMR
Definition H5File.hpp:61
Configure the version bounds for the file.
Definition H5PropertyList.hpp:317
Configure the metadata block size to use writing to files.
Definition H5PropertyList.hpp:337
hsize_t getSize() const
Definition H5PropertyList_misc.hpp:253
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:75
bool isValid() const noexcept
isValid
Definition H5Object_misc.hpp:71
hid_t _hid
Definition H5Object.hpp:98
HDF5 property Lists.
Definition H5PropertyList.hpp:138
Definition assert_compatible_spaces.hpp:15
bool any(File::AccessMode mode)
Definition H5File.hpp:203