Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Take ownership of Params #1115

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>(new object_t<T>(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, 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>(new object_t<T>(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
Loading