From cc58f6262e6933231bda9e1a3d5329e3d9d979c7 Mon Sep 17 00:00:00 2001 From: Ben Wibking Date: Sat, 15 Jun 2024 11:53:08 -0400 Subject: [PATCH 1/2] move params --- src/interface/params.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/interface/params.hpp b/src/interface/params.hpp index 4b2f4a09d0cd..3fd445b70040 100644 --- a/src/interface/params.hpp +++ b/src/interface/params.hpp @@ -49,28 +49,28 @@ class Params { /// /// Throws an error if the key is already in use template - void Add(const std::string &key, T value, Mutability mutability) { + void Add(const std::string &key, T &value, Mutability mutability) { PARTHENON_REQUIRE_THROWS(!(hasKey(key)), "Key " + key + " already exists"); - myParams_[key] = std::unique_ptr(new object_t(value)); + myParams_[key] = std::unique_ptr(std::move(value)); myTypes_.emplace(make_pair(key, std::type_index(typeid(value)))); myMutable_[key] = mutability; } template - void Add(const std::string &key, T value, bool is_mutable = false) { - Add(key, value, static_cast(is_mutable)); + void Add(const std::string &key, T &&value, bool is_mutable = false) { + Add(key, std::forward(value), static_cast(is_mutable)); } /// Updates existing object /// Throws an error if the key is not already in use template - void Update(const std::string &key, T value) { + void Update(const std::string &key, T &value) { PARTHENON_REQUIRE_THROWS((hasKey(key)), "Key " + key + "missing."); // immutable casts to false all others cast to true PARTHENON_REQUIRE_THROWS(static_cast(myMutable_.at(key)), "Parameter " + key + " must be marked as mutable"); PARTHENON_REQUIRE_THROWS(myTypes_.at(key) == std::type_index(typeid(T)), "WRONG TYPE FOR KEY '" + key + "'"); - myParams_[key] = std::unique_ptr(new object_t(value)); + myParams_[key] = std::unique_ptr(std::move(value)); } void reset() { From a1f7b52e70a885b789b457c1d440195b9adaefa2 Mon Sep 17 00:00:00 2001 From: Ben Wibking Date: Sat, 15 Jun 2024 12:22:46 -0400 Subject: [PATCH 2/2] try again --- src/interface/params.hpp | 16 ++++++++-------- src/interface/state_descriptor.hpp | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/interface/params.hpp b/src/interface/params.hpp index 3fd445b70040..989b70870668 100644 --- a/src/interface/params.hpp +++ b/src/interface/params.hpp @@ -49,28 +49,28 @@ class Params { /// /// Throws an error if the key is already in use template - void Add(const std::string &key, T &value, Mutability mutability) { + void Add(const std::string &key, T &&object, Mutability mutability) { PARTHENON_REQUIRE_THROWS(!(hasKey(key)), "Key " + key + " already exists"); - myParams_[key] = std::unique_ptr(std::move(value)); - myTypes_.emplace(make_pair(key, std::type_index(typeid(value)))); + myParams_[key] = std::unique_ptr(new object_t(std::move(object))); + myTypes_.emplace(make_pair(key, std::type_index(typeid(object)))); myMutable_[key] = mutability; } template - void Add(const std::string &key, T &&value, bool is_mutable = false) { - Add(key, std::forward(value), static_cast(is_mutable)); + void Add(const std::string &key, T &&object, bool is_mutable = false) { + Add(key, std::forward(object), static_cast(is_mutable)); } /// Updates existing object /// Throws an error if the key is not already in use template - void Update(const std::string &key, T &value) { + void Update(const std::string &key, T &&object) { PARTHENON_REQUIRE_THROWS((hasKey(key)), "Key " + key + "missing."); // immutable casts to false all others cast to true PARTHENON_REQUIRE_THROWS(static_cast(myMutable_.at(key)), "Parameter " + key + " must be marked as mutable"); PARTHENON_REQUIRE_THROWS(myTypes_.at(key) == std::type_index(typeid(T)), "WRONG TYPE FOR KEY '" + key + "'"); - myParams_[key] = std::unique_ptr(std::move(value)); + myParams_[key] = std::unique_ptr(new object_t(std::move(object))); } void reset() { @@ -197,7 +197,7 @@ class Params { template struct object_t : base_t { std::unique_ptr pValue; - explicit object_t(T val) : pValue(std::make_unique(val)) {} + explicit object_t(T &&obj) : pValue(std::make_unique(obj)) {} ~object_t() = default; const void *address() { return reinterpret_cast(pValue.get()); } }; diff --git a/src/interface/state_descriptor.hpp b/src/interface/state_descriptor.hpp index d81c614ccd32..bd4af49ad264 100644 --- a/src/interface/state_descriptor.hpp +++ b/src/interface/state_descriptor.hpp @@ -123,18 +123,18 @@ class StateDescriptor { } template - void AddParam(const std::string &key, T value, Params::Mutability mutability) { - params_.Add(key, value, mutability); + void AddParam(const std::string &key, T &&object, Params::Mutability mutability) { + params_.Add(key, std::forward(object), mutability); } template - void AddParam(const std::string &key, T value, bool is_mutable = false) { - params_.Add(key, value, is_mutable); + void AddParam(const std::string &key, T &&object, bool is_mutable = false) { + params_.Add(key, std::forward(object), is_mutable); } template - void UpdateParam(const std::string &key, T value) { - params_.Update(key, value); + void UpdateParam(const std::string &key, T &&object) { + params_.Update(key, std::forward(object)); } template @@ -150,8 +150,8 @@ class StateDescriptor { // Set (if not set) and get simultaneously. // infers type correctly. template - const T &Param(const std::string &key, T value) const { - return params_.Get(key, value); + const T &Param(const std::string &key, T &&object) const { + return params_.Get(key, std::forward(object)); } const std::type_index &ParamType(const std::string &key) const {