ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
open.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_OPEN_HPP
2#define RITSUKO_HDF5_OPEN_HPP
3
4#include "H5Cpp.h"
5
6#include <filesystem>
7#include <stdexcept>
8#include <string>
9
15namespace ritsuko {
16
17namespace hdf5 {
18
25template<class Path_>
26inline H5::H5File open_file(const Path_& path) try {
27 if (!std::filesystem::exists(path)) {
28 throw std::runtime_error("no file exists");
29 }
30
31 if constexpr(std::is_same<Path_, std::filesystem::path>::value) {
32 if constexpr(std::is_same<typename Path_::value_type, char>::value) {
33 // Avoid copy on POSIX...
34 return H5::H5File(path.c_str(), H5F_ACC_RDONLY);
35 } else {
36 // But still get it to work on Windows...
37 return H5::H5File(path.string(), H5F_ACC_RDONLY);
38 }
39 } else {
40 return H5::H5File(path, H5F_ACC_RDONLY);
41 }
42} catch (H5::Exception& e) {
43 std::string message = "failed to open the HDF5 file at '";
44 if constexpr(std::is_same<Path_, std::filesystem::path>::value) {
45 message += path.string();
46 } else {
47 message += path;
48 }
49 message += "'; " + e.getDetailMsg();
50 throw std::runtime_error(message);
51}
52
59inline H5::Group open_group(const H5::Group& handle, const char* name) {
60 if (!handle.exists(name) || handle.childObjType(name) != H5O_TYPE_GROUP) {
61 throw std::runtime_error("expected a group at '" + std::string(name) + "'");
62 }
63 return handle.openGroup(name);
64}
65
72inline H5::DataSet open_dataset(const H5::Group& handle, const char* name) {
73 if (!handle.exists(name) || handle.childObjType(name) != H5O_TYPE_DATASET) {
74 throw std::runtime_error("expected a dataset at '" + std::string(name) + "'");
75 }
76 return handle.openDataSet(name);
77}
78
87template<class Object_>
88H5::Attribute open_attribute(const Object_& handle, const char* name) {
89 if (!handle.attrExists(name)) {
90 throw std::runtime_error("expected an attribute at '" + std::string(name) + "'");
91 }
92 return handle.openAttribute(name);
93}
94
95}
96
97}
98
99#endif
H5::Attribute open_attribute(const Object_ &handle, const char *name)
Definition open.hpp:88
H5::H5File open_file(const Path_ &path)
Definition open.hpp:26
H5::DataSet open_dataset(const H5::Group &handle, const char *name)
Definition open.hpp:72
H5::Group open_group(const H5::Group &handle, const char *name)
Definition open.hpp:59
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15