chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
constant_array.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_CONSTANT_ARRAY_HPP
2#define CHIHAYA_CONSTANT_ARRAY_HPP
3
9#include "H5Cpp.h"
10#include "ritsuko/ritsuko.hpp"
11#include "ritsuko/hdf5/hdf5.hpp"
12
13#include <vector>
14#include <cstdint>
15#include <stdexcept>
16
17namespace chihaya {
18
23namespace constant_array {
24
33inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, [[maybe_unused]] Options& options) {
34 ArrayDetails output;
35
36 {
37 auto dhandle = ritsuko::hdf5::open_dataset(handle, "dimensions");
38 size_t size = ritsuko::hdf5::get_1d_length(dhandle, false);
39 if (size == 0) {
40 throw std::runtime_error("'dimensions' should have non-zero length");
41 }
42
43 if (version.lt(1, 1, 0)) {
44 if (dhandle.getTypeClass() != H5T_INTEGER) {
45 throw std::runtime_error("'dimensions' should be integer");
46 }
47 std::vector<int> dims_tmp(size);
48 dhandle.read(dims_tmp.data(), H5::PredType::NATIVE_INT);
49 for (auto d : dims_tmp) {
50 if (d < 0) {
51 throw std::runtime_error("'dimensions' should contain non-negative values");
52 }
53 }
54 output.dimensions.insert(output.dimensions.end(), dims_tmp.begin(), dims_tmp.end());
55
56 } else {
57 if (ritsuko::hdf5::exceeds_integer_limit(dhandle, 64, false)) {
58 throw std::runtime_error("datatype of 'dimensions' should fit inside a 64-bit unsigned integer");
59 }
60 std::vector<uint64_t> dims(size);
61 dhandle.read(dims.data(), H5::PredType::NATIVE_UINT64);
62 output.dimensions.insert(output.dimensions.end(), dims.begin(), dims.end());
63 }
64 }
65
66 {
67 auto vhandle = ritsuko::hdf5::open_dataset(handle, "value");
68 if (!ritsuko::hdf5::is_scalar(vhandle)) {
69 throw std::runtime_error("'value' should be a scalar");
70 }
71
72 try {
73 if (version.lt(1, 1, 0)) {
74 output.type = internal_type::translate_type_0_0(vhandle.getTypeClass());
75 } else {
76 auto type = ritsuko::hdf5::open_and_load_scalar_string_attribute(vhandle, "type");
77 output.type = internal_type::translate_type_1_1(type);
78 internal_type::check_type_1_1(vhandle, output.type);
79 }
80
81 if (!options.details_only) {
82 internal_misc::validate_missing_placeholder(vhandle, version);
83 }
84
85 if (vhandle.getTypeClass() == H5T_STRING) {
86 ritsuko::hdf5::validate_scalar_string_dataset(vhandle);
87 }
88
89 } catch (std::exception& e) {
90 throw std::runtime_error("failed to validate 'value'; " + std::string(e.what()));
91 }
92 }
93
94 return output;
95}
96
97}
98
99}
100
101#endif
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: constant_array.hpp:33
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
Details about an array.
Definition: utils_public.hpp:36
std::vector< size_t > dimensions
Definition: utils_public.hpp:56
ArrayType type
Definition: utils_public.hpp:50
Validation options.
Definition: utils_public.hpp:66