Control Libraries 7.4.0
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 try {
50 if (this->parameters_.at(parameter->get_name())->get_parameter_type() != parameter->get_parameter_type()) {
52 "Parameter '" + parameter->get_name() + "' exists, but has unexpected type.");
53 }
54 if (parameter->get_parameter_type() == ParameterType::STATE && parameter->get_parameter_state_type()
55 != this->parameters_.at(parameter->get_name())->get_parameter_state_type()) {
57 "Parameter '" + parameter->get_name() + "' exists, but has unexpected state type.");
58 }
59 } catch (const std::out_of_range&) {
60 throw exceptions::InvalidParameterException("Parameter '" + parameter->get_name() + "' doesn't exist.");
61 }
62}
63
64void ParameterMap::validate_and_set_parameter(const std::shared_ptr<ParameterInterface>& parameter) {
65 this->parameters_.insert_or_assign(parameter->get_name(), parameter);
66}
67
68void ParameterMap::remove_parameter(const std::string& name) {
69 if (!this->parameters_.count(name)) {
70 throw exceptions::InvalidParameterException("Parameter '" + name + "' could not be found in the parameter map.");
71 }
72 this->parameters_.erase(name);
73}
74
75}
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 exists and 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.