88
99#include < cmath>
1010#include < format>
11+ #include < optional>
1112#include < stdexcept>
1213#include < string_view>
1314#include < variant>
@@ -24,12 +25,11 @@ namespace GridKit
2425 /* *
2526 * @brief Reads typed parameters out of a model data container.
2627 *
27- * A load method stores a provided value in its target and returns true.
28- * An omitted parameter leaves the target untouched, so the target's
29- * initial value is the default. A require method rejects an omitted
30- * parameter. A value of the wrong type cannot produce a valid model, so
31- * it is rejected with std::invalid_argument naming the model and the
32- * parameter. Real parameters accept integer values and must be finite.
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.
3333 *
3434 * @tparam ModelDataT A model data container satisfying
3535 * PhasorDynamics::ModelData.
@@ -52,13 +52,13 @@ namespace GridKit
5252 {
5353 }
5454
55- // / Load an optional real parameter.
56- bool loadReal (ParametersT key, RealT& target ) const
55+ // / Read an optional real parameter.
56+ [[nodiscard]] std::optional<RealT> readReal (ParametersT key) const
5757 {
5858 const auto * value = find (key);
5959 if (value == nullptr )
6060 {
61- return false ;
61+ return std:: nullopt ;
6262 }
6363
6464 RealT real{};
@@ -80,57 +80,30 @@ namespace GridKit
8080 reject (key, " must be finite" );
8181 }
8282
83- target = real;
84- return true ;
83+ return real;
8584 }
8685
87- // / Load an optional non-negative real parameter.
88- bool loadNonNegative (ParametersT key, RealT& target ) const
86+ // / Read an optional non-negative real parameter.
87+ [[nodiscard]] std::optional<RealT> readNonNegative (ParametersT key) const
8988 {
90- if (!loadReal (key, target))
91- {
92- return false ;
93- }
94-
95- if (target < ZERO <RealT>)
89+ auto value = readReal (key);
90+ if (value && *value < ZERO <RealT>)
9691 {
9792 reject (key, " must be non-negative" );
9893 }
99- return true ;
100- }
101-
102- // / Load an optional boolean parameter.
103- bool loadBool (ParametersT key, bool & target) const
104- {
105- return loadExact (key, target, " must be boolean" );
106- }
107-
108- // / Load an optional integer parameter.
109- bool loadInteger (ParametersT key, IdxT& target) const
110- {
111- return loadExact (key, target, " must be an integer" );
94+ return value;
11295 }
11396
114- // / Read a real parameter that must be provided .
115- RealT requireReal (ParametersT key) const
97+ // / Read an optional boolean parameter .
98+ [[nodiscard]] std::optional< bool > readBool (ParametersT key) const
11699 {
117- RealT real{};
118- if (!loadReal (key, real))
119- {
120- reject (key, " is required" );
121- }
122- return real;
100+ return readExact<bool >(key, " must be boolean" );
123101 }
124102
125- // / Read a boolean parameter that must be provided .
126- bool requireBool (ParametersT key) const
103+ // / Read an optional integer parameter .
104+ [[nodiscard]] std::optional<IdxT> readInteger (ParametersT key) const
127105 {
128- bool flag{};
129- if (!loadBool (key, flag))
130- {
131- reject (key, " is required" );
132- }
133- return flag;
106+ return readExact<IdxT>(key, " must be an integer" );
134107 }
135108
136109 private:
@@ -148,14 +121,14 @@ namespace GridKit
148121 return &entry->second ;
149122 }
150123
151- // / Load an optional parameter that must hold exactly a T.
124+ // / Read an optional parameter that must hold exactly a T.
152125 template <typename T>
153- bool loadExact (ParametersT key, T& target , std::string_view reason) const
126+ [[nodiscard]] std::optional<T> readExact (ParametersT key, std::string_view reason) const
154127 {
155128 const auto * value = find (key);
156129 if (value == nullptr )
157130 {
158- return false ;
131+ return std:: nullopt ;
159132 }
160133
161134 const auto * exact_value = std::get_if<T>(value);
@@ -164,8 +137,7 @@ namespace GridKit
164137 reject (key, reason);
165138 }
166139
167- target = *exact_value;
168- return true ;
140+ return *exact_value;
169141 }
170142
171143 // / Reject the model data with a message naming the model and parameter.
0 commit comments