chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
unary_logic.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_UNARY_LOGIC_HPP
2#define CHIHAYA_UNARY_LOGIC_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6
7#include <stdexcept>
8#include <exception>
9#include <string>
10
11#include "utils_public.hpp"
12#include "utils_misc.hpp"
13#include "utils_type.hpp"
14#include "utils_nary.hpp"
15
22namespace chihaya {
23
36inline ArrayDetails validate_unary_logic(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
37 ArrayDetails seed_details;
38 try {
39 auto shandle = group.openGroup("seed");
40 seed_details = validate_numeric_seed(shandle, version, options);
41 } catch (...) {
42 std::throw_with_nested(std::runtime_error("failed to validate 'seed'"));
43 }
44
45 if (!options.details_only) {
46 std::string method;
47 try {
48 auto mhandle = group.openDataSet("method");
49 method = read_scalar_string_dataset(mhandle);
50 if (method != "!" && method != "&&" && method != "||") {
51 throw std::runtime_error("unrecognized operation '" + method + "'");
52 }
53 } catch (...) {
54 std::throw_with_nested(std::runtime_error("failed to validate 'method'"));
55 }
56
57 // Checking the sidedness.
58 if (method != "!") {
59 std::string side;
60 try {
61 auto shandle = group.openDataSet("side");
62 auto side = read_scalar_string_dataset(shandle);
63 if (side != "left" && side != "right") {
64 throw std::runtime_error("expected 'left' or 'right' for operation '" + method + "'");
65 }
66 } catch (...) {
67 std::throw_with_nested(std::runtime_error("failed to validate 'side'"));
68 }
69
70 enum Failure { VALUE, ALONG };
71 Failure who_failed = VALUE;
72 try {
73 auto vhandle = group.openDataSet("value");
74
75 ArrayType val_type;
76 if (version.lt(1, 1, 0)) {
77 val_type = translate_type_0_99(vhandle.getTypeClass());
78 } else {
79 try {
80 auto thandle = vhandle.openAttribute("type");
81 auto type = read_scalar_string_attribute(thandle);
82 val_type = translate_type_1_1(type);
83 check_type_1_1(vhandle, val_type);
84 } catch (...) {
85 std::throw_with_nested(std::runtime_error("failed to validate the 'type' attribute"));
86 }
87 }
88 if (val_type == STRING) {
89 throw std::runtime_error("dataset should be integer, float or boolean");
90 }
91
92 validate_missing_placeholder(vhandle, version);
93
94 auto vspace = vhandle.getSpace();
95 const auto ndims = vspace.getSimpleExtentNdims();
96 if (ndims == 0) {
97 // scalar operation.
98 } else if (ndims == 1) {
99 hsize_t extent;
100 vspace.getSimpleExtentDims(&extent);
101 who_failed = ALONG;
102 auto ahandle = group.openDataSet("along");
103 check_unary_along(ahandle, version, seed_details.dimensions, extent);
104 who_failed = VALUE;
105 } else {
106 throw std::runtime_error("dataset should be scalar or 1-dimensional");
107 }
108
109 } catch (...) {
110 std::string desc;
111 switch (who_failed) {
112 case VALUE: desc = "value"; break;
113 case ALONG: desc = "along"; break;
114 }
115 std::throw_with_nested(std::runtime_error("failed to validate '" + desc + "'"));
116 }
117 }
118 }
119
120 seed_details.type = BOOLEAN;
121 return seed_details;
122}
123
124}
125
126#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate_unary_logic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_logic.hpp:36
ArrayType
Definition utils_public.hpp:29
Details about an array.
Definition utils_public.hpp:38
std::vector< std::size_t > dimensions
Definition utils_public.hpp:58
ArrayType type
Definition utils_public.hpp:52
Options for validate().
Definition utils_public.hpp:79
bool details_only
Definition utils_public.hpp:84
Various public utilities.