ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
missing_placeholder.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_MISSING_PLACEHOLDER_HPP
2#define RITSUKO_HDF5_MISSING_PLACEHOLDER_HPP
3
4#include "H5Cpp.h"
5#include <string>
6
8#include "load_attribute.hpp"
9#include "validate_string.hpp"
10#include "get_1d_length.hpp"
11#include "get_name.hpp"
12
18namespace ritsuko {
19
20namespace hdf5 {
21
31inline void check_numeric_missing_placeholder_attribute(const H5::DataSet& dset, const H5::Attribute& attr, bool type_class_only = false) {
32 if (!is_scalar(attr)) {
33 throw std::runtime_error("expected the '" + get_name(attr) + "' attribute to be a scalar");
34 }
35 if (type_class_only) {
36 if (attr.getTypeClass() != dset.getTypeClass()) {
37 throw std::runtime_error("expected the '" + get_name(attr) + "' attribute to have the same type class as its dataset");
38 }
39 } else {
40 if (attr.getDataType() != dset.getDataType()) {
41 throw std::runtime_error("expected the '" + get_name(attr) + "' attribute to have the same type as its dataset");
42 }
43 }
44}
45
56template<typename Type_>
57std::optional<Type_> open_and_load_optional_numeric_missing_placeholder(const H5::DataSet& handle, const char* attr_name) {
58 if (!handle.attrExists(attr_name)) {
59 return {};
60 }
61 auto ahandle = handle.openAttribute(attr_name);
63 Type_ value;
64 ahandle.read(as_numeric_datatype<Type_>(), &value);
65 return value;
66}
67
71namespace internal {
72
73inline void check_string_missing_placeholder_attribute_preliminary(const H5::Attribute& attr) {
74 if (!is_scalar(attr)) {
75 throw std::runtime_error("expected the '" + get_name(attr) + "' attribute to be a scalar");
76 }
77 if (attr.getTypeClass() != H5T_STRING) {
78 throw std::runtime_error("expected the '" + get_name(attr) + "' attribute to have a string datatype");
79 }
80}
81
82}
94inline void check_string_missing_placeholder_attribute(const H5::Attribute& attr) {
95 internal::check_string_missing_placeholder_attribute_preliminary(attr);
97}
98
108inline std::optional<std::string> open_and_load_optional_string_missing_placeholder(const H5::DataSet& handle, const char* attr_name) {
109 if (!handle.attrExists(attr_name)) {
110 return {};
111 }
112 auto ahandle = handle.openAttribute(attr_name);
113 internal::check_string_missing_placeholder_attribute_preliminary(ahandle);
114 return load_scalar_string_attribute(ahandle);
115}
116
117}
118
119}
120
121#endif
Choose a HDF5 datatype.
Get the length of a 1-dimensional HDF5 dataset.
Get the name of a HDF5 object.
Load HDF5 attributes.
std::string get_name(const Handle_ &handle)
Definition get_name.hpp:24
const H5::PredType & as_numeric_datatype()
Definition as_numeric_datatype.hpp:26
bool is_scalar(const H5::DataSpace &space)
Definition get_1d_length.hpp:70
std::optional< Type_ > open_and_load_optional_numeric_missing_placeholder(const H5::DataSet &handle, const char *attr_name)
Definition missing_placeholder.hpp:57
void check_numeric_missing_placeholder_attribute(const H5::DataSet &dset, const H5::Attribute &attr, bool type_class_only=false)
Definition missing_placeholder.hpp:31
void validate_scalar_string_attribute(const H5::Attribute &attr)
Definition validate_string.hpp:149
std::optional< std::string > open_and_load_optional_string_missing_placeholder(const H5::DataSet &handle, const char *attr_name)
Definition missing_placeholder.hpp:108
std::string load_scalar_string_attribute(const H5::Attribute &attr)
Definition load_attribute.hpp:27
void check_string_missing_placeholder_attribute(const H5::Attribute &attr)
Definition missing_placeholder.hpp:94
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15
Helper functions to validate strings.