Skip to content

Commit 59fea65

Browse files
committed
Add AbortingAttribute. Implement a noexcept overload of the getArraySize function
1 parent e4c38c4 commit 59fea65

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

bindings/python/src/Variable.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ void declareVariable(pybind11::module_& m) {
109109
const mgis::behaviour::Hypothesis>(
110110
&mgis::behaviour::getVariableSize));
111111
m.def("getVariableSize", getVariableSizeByString);
112-
m.def("getArraySize", &mgis::behaviour::getArraySize);
112+
m.def("getArraySize",
113+
pybind11::overload_cast<const std::vector<Variable>&,
114+
const mgis::behaviour::Hypothesis>(
115+
&mgis::behaviour::getArraySize));
113116
m.def("getVariableOffset", getVariableOffsetByString);
114117
m.def("getVariableTypeSymbolicRepresentation",
115118
&mgis::behaviour::getVariableTypeSymbolicRepresentation);

include/MGIS/Behaviour/Variable.hxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ namespace mgis::behaviour {
111111
*/
112112
MGIS_EXPORT [[nodiscard]] size_type getArraySize(
113113
const std::vector<Variable> &, const Hypothesis);
114+
/*!
115+
* \return the size of an array that may contain the values described by the
116+
* given array of variables
117+
*
118+
* \param[in, out] ctx: execution context
119+
* \param[in] vs: variables
120+
* \param[in] h: modelling hypothesis
121+
*/
122+
MGIS_EXPORT [[nodiscard]] std::optional<size_type> getArraySize(
123+
Context &, const std::vector<Variable> &, const Hypothesis) noexcept;
114124
/*!
115125
* \return the offset of the given variable for the given hypothesis
116126
* \param[in] vs: variables

include/MGIS/Config.hxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ namespace mgis::attributes {
9797
template <bool>
9898
struct ThrowingAttribute {};
9999

100+
/*!
101+
* \brief an attribute use to indicate that a method may abort the process in
102+
* case of error is the boolean value is true
103+
*/
104+
template <bool>
105+
struct AbortingAttribute {};
106+
100107
} // namespace mgis::attributes
101108

102109
namespace mgis {
@@ -107,6 +114,8 @@ namespace mgis {
107114
inline constexpr auto throwing = attributes::ThrowingAttribute<true>{};
108115
//
109116
inline constexpr auto not_throwing = attributes::ThrowingAttribute<false>{};
117+
//
118+
inline constexpr auto aborting = attributes::AbortingAttribute<true>{};
110119

111120
//! \brief a simple alias to the the default indexing type used by mgis
112121
using size_type = mgis_size_type;

src/Variable.cxx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,25 @@ namespace mgis::behaviour {
351351
return s;
352352
} // end of getArraySize
353353

354-
size_type getVariableOffset(const std::vector<Variable> &vs,
355-
const std::string_view n,
356-
const Hypothesis h) {
357-
auto o = size_type{};
354+
std::optional<size_type> getArraySize(Context &ctx,
355+
const std::vector<Variable> &vs,
356+
const Hypothesis h) noexcept {
357+
auto s = size_type{};
358358
for (const auto &v : vs) {
359-
if (v.name == n) {
360-
return o;
359+
const auto os2 = getVariableSize(ctx, v, h);
360+
if (isInvalid(os2)) {
361+
return {};
361362
}
362-
o += getVariableSize(v, h);
363+
s += *os2;
363364
}
364-
raise("getVariableOffset: no variable named '" + std::string(n) + "'");
365+
return s;
366+
} // end of getArraySize
367+
368+
size_type getVariableOffset(const std::vector<Variable> &vs,
369+
const std::string_view n,
370+
const Hypothesis h) {
371+
auto ctx = Context{};
372+
return getVariableOffset(ctx, vs, n, h) | ctx.getThrowingFailureHandler();
365373
} // end of getVariableOffset
366374

367375
std::optional<size_type> getVariableOffset(Context &ctx,

0 commit comments

Comments
 (0)