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