HighFive 3.0.0
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 return res_open;
38}
39} // namespace
40
41inline File::File(const std::string& filename,
42 AccessMode openFlags,
43 const FileAccessProps& fileAccessProps)
44 : File(filename, openFlags, FileCreateProps::Default(), fileAccessProps) {}
45
46
47inline File::File(const std::string& filename,
48 AccessMode access_mode,
49 const FileCreateProps& fileCreateProps,
50 const FileAccessProps& fileAccessProps) {
51 unsigned openFlags = convert_open_flag(access_mode);
52
53 unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
54 unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
55 bool mustCreate = createMode > 0;
56 bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
57
58 // open is default. It's skipped only if flags require creation
59 // If open fails it will try create() if H5F_ACC_CREAT is set
60 if (!mustCreate) {
61 // Silence open errors if create is allowed
62 std::unique_ptr<SilenceHDF5> silencer;
63 if (openOrCreate) {
64 silencer = std::make_unique<SilenceHDF5>();
65 }
66
67 _hid = detail::nothrow::h5f_open(filename.c_str(), openMode, fileAccessProps.getId());
68
69 if (isValid()) {
70 return; // Done
71 }
72
73 if (openOrCreate) {
74 // Will attempt to create ensuring wont clobber any file
75 createMode = H5F_ACC_EXCL;
76 } else {
77 HDF5ErrMapper::ToException<FileException>(
78 std::string("Unable to open file " + filename));
79 }
80 }
81
82 auto fcpl = fileCreateProps.getId();
83 auto fapl = fileAccessProps.getId();
84 _hid = detail::h5f_create(filename.c_str(), createMode, fcpl, fapl);
85}
86
87inline const std::string& File::getName() const {
88 if (_filename.empty()) {
89 _filename = details::get_name([this](char* buffer, size_t length) {
90 return detail::h5f_get_name(getId(), buffer, length);
91 });
92 }
93 return _filename;
94}
95
96inline hsize_t File::getMetadataBlockSize() const {
97 auto fapl = getAccessPropertyList();
98 return MetadataBlockSize(fapl).getSize();
99}
100
101inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {
102 auto fapl = getAccessPropertyList();
103 auto fileVer = FileVersionBounds(fapl);
104 return fileVer.getVersion();
105}
106
107#if H5_VERSION_GE(1, 10, 1)
108inline H5F_fspace_strategy_t File::getFileSpaceStrategy() const {
109 auto fcpl = getCreatePropertyList();
110 FileSpaceStrategy spaceStrategy(fcpl);
111 return spaceStrategy.getStrategy();
112}
113
114inline hsize_t File::getFileSpacePageSize() const {
115 auto fcpl = getCreatePropertyList();
116
117 if (getFileSpaceStrategy() != H5F_FSPACE_STRATEGY_PAGE) {
118 HDF5ErrMapper::ToException<FileException>(
119 std::string("Cannot obtain page size as paged allocation is not used."));
120 }
121
122 return FileSpacePageSize(fcpl).getPageSize();
123}
124#endif
125
126inline void File::flush() {
127 detail::h5f_flush(_hid, H5F_SCOPE_GLOBAL);
128}
129
130inline size_t File::getFileSize() const {
131 hsize_t sizeValue = 0;
132 detail::h5f_get_filesize(_hid, &sizeValue);
133 return static_cast<size_t>(sizeValue);
134}
135
136inline size_t File::getFreeSpace() const {
137 return static_cast<size_t>(detail::h5f_get_freespace(_hid));
138}
139
140} // namespace HighFive
File class.
Definition H5File.hpp:25
FileCreateProps getCreatePropertyList() const
Get the list of properties for creation of this file.
Definition H5File.hpp:126
size_t getFileSize() const
Get the size of this file in bytes.
Definition H5File_misc.hpp:130
static constexpr AccessMode Truncate
Definition H5File.hpp:51
static constexpr AccessMode Create
Definition H5File.hpp:54
std::pair< H5F_libver_t, H5F_libver_t > getVersionBounds() const
Returns the HDF5 version compatibility bounds.
Definition H5File_misc.hpp:101
void flush()
flush
Definition H5File_misc.hpp:126
static constexpr AccessMode ReadOnly
Definition H5File.hpp:49
size_t getFreeSpace() const
Get the amount of tracked, unused space in bytes.
Definition H5File_misc.hpp:136
AccessMode
Definition H5File.hpp:29
FileAccessProps getAccessPropertyList() const
Get the list of properties for accession of this file.
Definition H5File.hpp:131
hsize_t getMetadataBlockSize() const
Returns the block size for metadata in bytes.
Definition H5File_misc.hpp:96
static constexpr AccessMode Excl
Definition H5File.hpp:52
File()=default
const std::string & getName() const
Return the name of the file.
Definition H5File_misc.hpp:87
static constexpr AccessMode ReadWrite
Definition H5File.hpp:50
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:192