Skip to content

Commit 4789347

Browse files
committed
getVariable now returns an OptionalReference
1 parent 4c43dd9 commit 4789347

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

include/MGIS/Behaviour/Variable.hxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <string_view>
2121
#include "MGIS/Config.hxx"
2222
#include "MGIS/Context.hxx"
23+
#include "MGIS/Utilities/OptionalReference.hxx"
2324
#include "MGIS/Behaviour/Hypothesis.hxx"
2425

2526
namespace mgis::behaviour {
@@ -72,7 +73,7 @@ namespace mgis::behaviour {
7273
* \param[in] vs: variables
7374
* \param[in] n: name
7475
*/
75-
MGIS_EXPORT [[nodiscard]] std::optional<const Variable *> getVariable(
76+
MGIS_EXPORT OptionalReference<const Variable> getVariable(
7677
Context &,
7778
const std::vector<Variable> &,
7879
const std::string_view) noexcept;

src/MaterialStateManager.cxx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace mgis::behaviour {
182182
if (isInvalid(omp)) {
183183
return false;
184184
}
185-
const auto& mp = *(*omp);
185+
const auto& mp = *omp;
186186
if (mp.type != Variable::SCALAR) {
187187
return ctx.registerErrorMessage(
188188
"setMaterialProperty: "
@@ -231,7 +231,7 @@ namespace mgis::behaviour {
231231
if (isInvalid(omp)) {
232232
return false;
233233
}
234-
const auto& mp = *(*omp);
234+
const auto& mp = *omp;
235235
if (mp.type != Variable::SCALAR) {
236236
return ctx.registerErrorMessage(
237237
"setMaterialProperty: "
@@ -376,7 +376,7 @@ namespace mgis::behaviour {
376376
if (isInvalid(oesv)) {
377377
return false;
378378
}
379-
if ((*oesv)->type != Variable::SCALAR){
379+
if (oesv->type != Variable::SCALAR){
380380
return ctx.registerErrorMessage(
381381
"setExternalStateVariable: "
382382
"invalid external state variable "
@@ -395,10 +395,16 @@ namespace mgis::behaviour {
395395
const std::span<real>& v,
396396
const MaterialStateManager::StorageMode s,
397397
const MaterialStateManager::UpdatePolicy p) noexcept {
398-
const auto esv = getVariable(m.b.esvs, n);
399-
const auto vs = getVariableSize(esv, m.b.hypothesis);
400-
if (((static_cast<mgis::size_type>(v.size()) != m.n * vs) &&
401-
(static_cast<mgis::size_type>(v.size()) != vs))) {
398+
const auto oesv = getVariable(ctx, m.b.esvs, n);
399+
if (isInvalid(oesv)) {
400+
return false;
401+
}
402+
const auto ovs = getVariableSize(ctx, *oesv, m.b.hypothesis);
403+
if (isInvalid(ovs)) {
404+
return false;
405+
}
406+
if (((static_cast<mgis::size_type>(v.size()) != m.n * (*ovs)) &&
407+
(static_cast<mgis::size_type>(v.size()) != (*ovs)))) {
402408
return ctx.registerErrorMessage(
403409
"setExternalStateVariable: invalid number of values");
404410
}

src/Variable.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ namespace mgis::behaviour {
330330
return *p;
331331
} // end of getVariable
332332

333-
std::optional<const Variable *> getVariable(
333+
OptionalReference<const Variable> getVariable(
334334
Context &ctx,
335335
const std::vector<Variable> &vs,
336336
const std::string_view n) noexcept {
@@ -340,7 +340,7 @@ namespace mgis::behaviour {
340340
return ctx.registerErrorMessage("getVariable: no variable named '" +
341341
std::string(n) + "'");
342342
}
343-
return &(*p);
343+
return {&(*p)};
344344
} // end of getVariable
345345

346346
size_type getArraySize(const std::vector<Variable> &vs, const Hypothesis h) {

0 commit comments

Comments
 (0)