|
| 1 | +/** |
| 2 | + * @file ParameterReader.hpp |
| 3 | + * @author Luke Lowery (lukel@tamu.edu) |
| 4 | + * @brief Typed parameter loading from model data containers. |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <cmath> |
| 10 | +#include <format> |
| 11 | +#include <optional> |
| 12 | +#include <stdexcept> |
| 13 | +#include <string_view> |
| 14 | +#include <variant> |
| 15 | + |
| 16 | +#include <magic_enum/magic_enum.hpp> |
| 17 | + |
| 18 | +#include <GridKit/Constants.hpp> |
| 19 | +#include <GridKit/Model/PhasorDynamics/ModelData.hpp> |
| 20 | + |
| 21 | +namespace GridKit |
| 22 | +{ |
| 23 | + namespace Model |
| 24 | + { |
| 25 | + /** |
| 26 | + * @brief Reads typed parameters out of a model data container. |
| 27 | + * |
| 28 | + * A read method returns the provided value or std::nullopt when the |
| 29 | + * parameter is omitted. The model chooses a default or requires a value. |
| 30 | + * Invalid values are rejected with std::invalid_argument naming the |
| 31 | + * model and parameter. Real parameters accept integer values and must |
| 32 | + * be finite. |
| 33 | + * |
| 34 | + * @tparam ModelDataT A model data container satisfying |
| 35 | + * PhasorDynamics::ModelData. |
| 36 | + */ |
| 37 | + template <PhasorDynamics::ModelData ModelDataT> |
| 38 | + class ParameterReader |
| 39 | + { |
| 40 | + public: |
| 41 | + using RealT = typename ModelDataT::RealT; |
| 42 | + using IdxT = typename ModelDataT::IdxT; |
| 43 | + using ParametersT = typename ModelDataT::Parameters; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param[in] data Model data container to read from. |
| 47 | + * @param[in] model Model name used in rejection messages. |
| 48 | + */ |
| 49 | + ParameterReader(const ModelDataT& data, const char* model) |
| 50 | + : parameters_(data.parameters), |
| 51 | + model_(model) |
| 52 | + { |
| 53 | + } |
| 54 | + |
| 55 | + /// Read an optional real parameter. |
| 56 | + [[nodiscard]] std::optional<RealT> readReal(ParametersT key) const |
| 57 | + { |
| 58 | + const auto* value = find(key); |
| 59 | + if (value == nullptr) |
| 60 | + { |
| 61 | + return std::nullopt; |
| 62 | + } |
| 63 | + |
| 64 | + RealT real{}; |
| 65 | + if (const auto* real_value = std::get_if<RealT>(value)) |
| 66 | + { |
| 67 | + real = *real_value; |
| 68 | + } |
| 69 | + else if (const auto* integer_value = std::get_if<IdxT>(value)) |
| 70 | + { |
| 71 | + real = static_cast<RealT>(*integer_value); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + reject(key, "must be numeric"); |
| 76 | + } |
| 77 | + |
| 78 | + if (!std::isfinite(real)) |
| 79 | + { |
| 80 | + reject(key, "must be finite"); |
| 81 | + } |
| 82 | + |
| 83 | + return real; |
| 84 | + } |
| 85 | + |
| 86 | + /// Read an optional non-negative real parameter. |
| 87 | + [[nodiscard]] std::optional<RealT> readNonNegative(ParametersT key) const |
| 88 | + { |
| 89 | + auto value = readReal(key); |
| 90 | + if (value && *value < ZERO<RealT>) |
| 91 | + { |
| 92 | + reject(key, "must be non-negative"); |
| 93 | + } |
| 94 | + return value; |
| 95 | + } |
| 96 | + |
| 97 | + /// Read an optional boolean parameter. |
| 98 | + [[nodiscard]] std::optional<bool> readBool(ParametersT key) const |
| 99 | + { |
| 100 | + return readExact<bool>(key, "must be boolean"); |
| 101 | + } |
| 102 | + |
| 103 | + /// Read an optional integer parameter. |
| 104 | + [[nodiscard]] std::optional<IdxT> readInteger(ParametersT key) const |
| 105 | + { |
| 106 | + return readExact<IdxT>(key, "must be an integer"); |
| 107 | + } |
| 108 | + |
| 109 | + private: |
| 110 | + using ParameterMapT = decltype(ModelDataT::parameters); |
| 111 | + using ValueT = typename ParameterMapT::mapped_type; |
| 112 | + |
| 113 | + /// The provided value, or nullptr when the parameter is omitted. |
| 114 | + const ValueT* find(ParametersT key) const |
| 115 | + { |
| 116 | + const auto entry = parameters_.find(key); |
| 117 | + if (entry == parameters_.end()) |
| 118 | + { |
| 119 | + return nullptr; |
| 120 | + } |
| 121 | + return &entry->second; |
| 122 | + } |
| 123 | + |
| 124 | + /// Read an optional parameter that must hold exactly a T. |
| 125 | + template <typename T> |
| 126 | + [[nodiscard]] std::optional<T> readExact(ParametersT key, std::string_view reason) const |
| 127 | + { |
| 128 | + const auto* value = find(key); |
| 129 | + if (value == nullptr) |
| 130 | + { |
| 131 | + return std::nullopt; |
| 132 | + } |
| 133 | + |
| 134 | + const auto* exact_value = std::get_if<T>(value); |
| 135 | + if (exact_value == nullptr) |
| 136 | + { |
| 137 | + reject(key, reason); |
| 138 | + } |
| 139 | + |
| 140 | + return *exact_value; |
| 141 | + } |
| 142 | + |
| 143 | + /// Reject the model data with a message naming the model and parameter. |
| 144 | + [[noreturn]] void reject(ParametersT key, std::string_view reason) const |
| 145 | + { |
| 146 | + throw std::invalid_argument( |
| 147 | + std::format("{}: parameter '{}' {}", model_, magic_enum::enum_name(key), reason)); |
| 148 | + } |
| 149 | + |
| 150 | + const ParameterMapT& parameters_; |
| 151 | + const char* model_; |
| 152 | + }; |
| 153 | + } // namespace Model |
| 154 | +} // namespace GridKit |
0 commit comments