From 41bd2d26ef7609d853645d1b39dde6ebde0804fd Mon Sep 17 00:00:00 2001
From: Norihiro Watanabe <norihiro.watanabe@aist.go.jp>
Date: Wed, 30 Jan 2019 23:46:30 +0900
Subject: [PATCH] [PL] remove mutable member variables in Parameter classes

---
 ProcessLib/Parameter/ConstantParameter.h    |  2 +-
 ProcessLib/Parameter/CurveScaledParameter.h |  9 ++++-----
 ProcessLib/Parameter/GroupBasedParameter.h  |  2 +-
 ProcessLib/Parameter/MeshElementParameter.h | 11 +++++------
 ProcessLib/Parameter/MeshNodeParameter.h    | 11 +++++------
 ProcessLib/Parameter/Parameter.h            |  2 +-
 6 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/ProcessLib/Parameter/ConstantParameter.h b/ProcessLib/Parameter/ConstantParameter.h
index 607f70d570a..1e62a24c9ad 100644
--- a/ProcessLib/Parameter/ConstantParameter.h
+++ b/ProcessLib/Parameter/ConstantParameter.h
@@ -40,7 +40,7 @@ struct ConstantParameter final : public Parameter<T>
         return static_cast<int>(_values.size());
     }
 
-    std::vector<T> const& operator()(
+    std::vector<T> operator()(
         double const /*t*/, SpatialPosition const& /*pos*/) const override
     {
         return _values;
diff --git a/ProcessLib/Parameter/CurveScaledParameter.h b/ProcessLib/Parameter/CurveScaledParameter.h
index c44a5ddb54e..93441d29873 100644
--- a/ProcessLib/Parameter/CurveScaledParameter.h
+++ b/ProcessLib/Parameter/CurveScaledParameter.h
@@ -35,7 +35,6 @@ struct CurveScaledParameter final : public Parameter<T> {
     {
         _parameter =
             &findParameter<T>(_referenced_parameter_name, parameters, 0);
-        _cache.resize(_parameter->getNumberOfComponents());
     }
 
     int getNumberOfComponents() const override
@@ -43,24 +42,24 @@ struct CurveScaledParameter final : public Parameter<T> {
         return _parameter->getNumberOfComponents();
     }
 
-    std::vector<T> const& operator()(double const t,
+    std::vector<T> operator()(double const t,
                                      SpatialPosition const& pos) const override
     {
         auto const& tup = (*_parameter)(t, pos);
         auto const scaling = _curve.getValue(t);
 
         auto const num_comp = _parameter->getNumberOfComponents();
+        std::vector<T> cache(num_comp);
         for (int c = 0; c < num_comp; ++c)
         {
-            _cache[c] = scaling * tup[c];
+            cache[c] = scaling * tup[c];
         }
-        return _cache;
+        return cache;
     }
 
 private:
     MathLib::PiecewiseLinearInterpolation const& _curve;
     Parameter<T> const* _parameter;
-    mutable std::vector<T> _cache;
     std::string const _referenced_parameter_name;
 };
 
diff --git a/ProcessLib/Parameter/GroupBasedParameter.h b/ProcessLib/Parameter/GroupBasedParameter.h
index ab030026ee7..092a11e704a 100644
--- a/ProcessLib/Parameter/GroupBasedParameter.h
+++ b/ProcessLib/Parameter/GroupBasedParameter.h
@@ -58,7 +58,7 @@ struct GroupBasedParameter final
                    : static_cast<int>(_vec_values.front().size());
     }
 
-    std::vector<T> const& operator()(double const /*t*/,
+    std::vector<T> operator()(double const /*t*/,
                                      SpatialPosition const& pos) const override
     {
         auto const item_id = getMeshItemID(pos, type<MeshItemType>());
diff --git a/ProcessLib/Parameter/MeshElementParameter.h b/ProcessLib/Parameter/MeshElementParameter.h
index ef5b18af53d..9bb6de3e92c 100644
--- a/ProcessLib/Parameter/MeshElementParameter.h
+++ b/ProcessLib/Parameter/MeshElementParameter.h
@@ -25,8 +25,7 @@ struct MeshElementParameter final : public Parameter<T> {
     MeshElementParameter(std::string const& name_,
                          MeshLib::PropertyVector<T> const& property)
         : Parameter<T>(name_),
-          _property(property),
-          _cache(_property.getNumberOfComponents())
+          _property(property)
     {
     }
 
@@ -37,7 +36,7 @@ struct MeshElementParameter final : public Parameter<T> {
         return _property.getNumberOfComponents();
     }
 
-    std::vector<T> const& operator()(double const /*t*/,
+    std::vector<T> operator()(double const /*t*/,
                                      SpatialPosition const& pos) const override
     {
         auto const e = pos.getElementID();
@@ -48,11 +47,12 @@ struct MeshElementParameter final : public Parameter<T> {
                 "not specified.");
         }
         auto const num_comp = _property.getNumberOfComponents();
+        std::vector<T> cache(num_comp);
         for (int c = 0; c < num_comp; ++c)
         {
-            _cache[c] = _property.getComponent(*e, c);
+            cache[c] = _property.getComponent(*e, c);
         }
-        return _cache;
+        return cache;
     }
 
     Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> getNodalValuesOnElement(
@@ -78,7 +78,6 @@ struct MeshElementParameter final : public Parameter<T> {
 
 private:
     MeshLib::PropertyVector<T> const& _property;
-    mutable std::vector<T> _cache;
 };
 
 std::unique_ptr<ParameterBase> createMeshElementParameter(
diff --git a/ProcessLib/Parameter/MeshNodeParameter.h b/ProcessLib/Parameter/MeshNodeParameter.h
index 338383ee4bf..bda0426bda4 100644
--- a/ProcessLib/Parameter/MeshNodeParameter.h
+++ b/ProcessLib/Parameter/MeshNodeParameter.h
@@ -29,8 +29,7 @@ struct MeshNodeParameter final : public Parameter<T> {
     MeshNodeParameter(std::string const& name_,
                       MeshLib::PropertyVector<T> const& property)
         : Parameter<T>(name_),
-          _property(property),
-          _cache(_property.getNumberOfComponents())
+          _property(property)
     {
     }
 
@@ -41,7 +40,7 @@ struct MeshNodeParameter final : public Parameter<T> {
         return _property.getNumberOfComponents();
     }
 
-    std::vector<T> const& operator()(double const /*t*/,
+    std::vector<T> operator()(double const /*t*/,
                                      SpatialPosition const& pos) const override
     {
         auto const n = pos.getNodeID();
@@ -52,11 +51,12 @@ struct MeshNodeParameter final : public Parameter<T> {
                 "specified.");
         }
         auto const num_comp = _property.getNumberOfComponents();
+        std::vector<T> cache(num_comp);
         for (int c = 0; c < num_comp; ++c)
         {
-            _cache[c] = _property.getComponent(*n, c);
+            cache[c] = _property.getComponent(*n, c);
         }
-        return _cache;
+        return cache;
     }
 
     Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> getNodalValuesOnElement(
@@ -82,7 +82,6 @@ struct MeshNodeParameter final : public Parameter<T> {
 
 private:
     MeshLib::PropertyVector<T> const& _property;
-    mutable std::vector<T> _cache;
 };
 
 std::unique_ptr<ParameterBase> createMeshNodeParameter(
diff --git a/ProcessLib/Parameter/Parameter.h b/ProcessLib/Parameter/Parameter.h
index dd75ab61f64..e350d341550 100644
--- a/ProcessLib/Parameter/Parameter.h
+++ b/ProcessLib/Parameter/Parameter.h
@@ -77,7 +77,7 @@ struct Parameter : public ParameterBase
     virtual int getNumberOfComponents() const = 0;
 
     //! Returns the parameter value at the given time and position.
-    virtual std::vector<T> const& operator()(
+    virtual std::vector<T> operator()(
         double const t, SpatialPosition const& pos) const = 0;
 
     //! Returns a matrix of values for all nodes of the given element.
-- 
GitLab