Skip to content

Commit

Permalink
try again
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWibking committed Jun 15, 2024
1 parent cc58f62 commit a1f7b52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/interface/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,28 @@ class Params {
///
/// Throws an error if the key is already in use
template <typename T>
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<Params::base_t>(std::move(value));
myTypes_.emplace(make_pair(key, std::type_index(typeid(value))));
myParams_[key] = std::unique_ptr<Params::base_t>(new object_t<T>(std::move(object)));
myTypes_.emplace(make_pair(key, std::type_index(typeid(object))));
myMutable_[key] = mutability;
}
template <typename T>
void Add(const std::string &key, T &&value, bool is_mutable = false) {
Add(key, std::forward<T>(value), static_cast<Mutability>(is_mutable));
void Add(const std::string &key, T &&object, bool is_mutable = false) {
Add(key, std::forward<T>(object), static_cast<Mutability>(is_mutable));
}

/// Updates existing object
/// Throws an error if the key is not already in use
template <typename T>
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<bool>(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<Params::base_t>(std::move(value));
myParams_[key] = std::unique_ptr<Params::base_t>(new object_t<T>(std::move(object)));
}

void reset() {
Expand Down Expand Up @@ -197,7 +197,7 @@ class Params {
template <typename T>
struct object_t : base_t {
std::unique_ptr<T> pValue;
explicit object_t(T val) : pValue(std::make_unique<T>(val)) {}
explicit object_t(T &&obj) : pValue(std::make_unique<T>(obj)) {}
~object_t() = default;
const void *address() { return reinterpret_cast<void *>(pValue.get()); }
};
Expand Down
16 changes: 8 additions & 8 deletions src/interface/state_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ class StateDescriptor {
}

template <typename T>
void AddParam(const std::string &key, T value, Params::Mutability mutability) {
params_.Add<T>(key, value, mutability);
void AddParam(const std::string &key, T &&object, Params::Mutability mutability) {
params_.Add<T>(key, std::forward<T>(object), mutability);
}

template <typename T>
void AddParam(const std::string &key, T value, bool is_mutable = false) {
params_.Add<T>(key, value, is_mutable);
void AddParam(const std::string &key, T &&object, bool is_mutable = false) {
params_.Add<T>(key, std::forward<T>(object), is_mutable);
}

template <typename T>
void UpdateParam(const std::string &key, T value) {
params_.Update<T>(key, value);
void UpdateParam(const std::string &key, T &&object) {
params_.Update<T>(key, std::forward<T>(object));
}

template <typename T>
Expand All @@ -150,8 +150,8 @@ class StateDescriptor {
// Set (if not set) and get simultaneously.
// infers type correctly.
template <typename T>
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<T>(object));
}

const std::type_index &ParamType(const std::string &key) const {
Expand Down

0 comments on commit a1f7b52

Please sign in to comment.