Control Libraries 6.3.4
Loading...
Searching...
No Matches
ParameterMap.cpp
1#include "state_representation/parameters/ParameterMap.hpp"
2
3namespace state_representation {
4
5ParameterMap::ParameterMap(const ParameterInterfaceList& parameters) {
6 this->set_parameters(parameters);
7}
8
9ParameterMap::ParameterMap(const ParameterInterfaceMap& parameters) {
10 this->set_parameters(parameters);
11}
12
13std::shared_ptr<ParameterInterface> ParameterMap::get_parameter(const std::string& name) const {
14 if (this->parameters_.find(name) == this->parameters_.cend()) {
15 throw exceptions::InvalidParameterException("Could not find a parameter named '" + name + "'.");
16 }
17 return this->parameters_.at(name);
18}
19
20ParameterInterfaceMap ParameterMap::get_parameters() const {
21 return this->parameters_;
22}
23
24ParameterInterfaceList ParameterMap::get_parameter_list() const {
25 ParameterInterfaceList param_list;
26 for (const auto& param_it: this->parameters_) {
27 param_list.template emplace_back(param_it.second);
28 }
29 return param_list;
30}
31
32void ParameterMap::set_parameter(const std::shared_ptr<ParameterInterface>& parameter) {
33 this->validate_and_set_parameter(parameter);
34}
35
36void ParameterMap::set_parameters(const ParameterInterfaceList& parameters) {
37 for (const auto& param: parameters) {
38 this->set_parameter(param);
39 }
40}
41
42void ParameterMap::set_parameters(const ParameterInterfaceMap& parameters) {
43 for (const auto& param_it: parameters) {
44 this->set_parameter(param_it.second);
45 }
46}
47
48void ParameterMap::assert_parameter_valid(const std::shared_ptr<ParameterInterface>& parameter) {
49 if (this->parameters_.at(parameter->get_name())->get_type() != parameter->get_type()) {
51 "Parameter '" + parameter->get_name() + "' exists, but has unexpected type."
52 );
53 }
54}
55
56void ParameterMap::validate_and_set_parameter(const std::shared_ptr<ParameterInterface>& parameter) {
57 this->parameters_.insert_or_assign(parameter->get_name(), parameter);
58}
59
60void ParameterMap::remove_parameter(const std::string& name) {
61 if (!this->parameters_.count(name)) {
62 throw exceptions::InvalidParameterException("Parameter '" + name + "' could not be found in the parameter map.");
63 }
64 this->parameters_.erase(name);
65}
66
67}
ParameterMap()=default
Empty constructor.
ParameterInterfaceMap get_parameters() const
Get a map of all the <name, parameter> pairs.
ParameterInterfaceList get_parameter_list() const
Get a list of all the parameters.
void assert_parameter_valid(const std::shared_ptr< ParameterInterface > &parameter)
Check if a parameter has the expected type, throw an exception otherwise.
virtual void validate_and_set_parameter(const std::shared_ptr< ParameterInterface > &parameter)
Validate and set a parameter in the map.
std::shared_ptr< ParameterInterface > get_parameter(const std::string &name) const
Get a parameter by its name.
void remove_parameter(const std::string &name)
Remove a parameter from the parameter map.
void set_parameters(const ParameterInterfaceList &parameters)
Set parameters from a list of parameters.
ParameterInterfaceMap parameters_
map of parameters by name
void set_parameter(const std::shared_ptr< ParameterInterface > &parameter)
Set a parameter.
Core state variables and objects.