Control Libraries 7.4.0
Loading...
Searching...
No Matches
ParameterMap.hpp
1#pragma once
2
3#include <list>
4#include <map>
5#include <memory>
6
7#include "state_representation/exceptions/InvalidParameterException.hpp"
8#include "state_representation/parameters/Parameter.hpp"
9
10namespace state_representation {
11
12typedef std::list<std::shared_ptr<ParameterInterface>> ParameterInterfaceList;
13typedef std::map<std::string, std::shared_ptr<ParameterInterface>> ParameterInterfaceMap;
14
20public:
21
25 ParameterMap() = default;
26
31 explicit ParameterMap(const ParameterInterfaceList& parameters);
32
37 explicit ParameterMap(const ParameterInterfaceMap& parameters);
38
44 [[nodiscard]] std::shared_ptr<ParameterInterface> get_parameter(const std::string& name) const;
45
50 [[nodiscard]] ParameterInterfaceMap get_parameters() const;
51
58 template<typename T>
59 [[nodiscard]] T get_parameter_value(const std::string& name) const;
60
65 [[nodiscard]] ParameterInterfaceList get_parameter_list() const;
66
71 void set_parameter(const std::shared_ptr<ParameterInterface>& parameter);
72
77 void set_parameters(const ParameterInterfaceList& parameters);
78
83 void set_parameters(const ParameterInterfaceMap& parameters);
84
91 template<typename T>
92 void set_parameter_value(const std::string& name, const T& value);
93
99 void remove_parameter(const std::string& name);
100
101protected:
102
109 virtual void validate_and_set_parameter(const std::shared_ptr<ParameterInterface>& parameter);
110
116 void assert_parameter_valid(const std::shared_ptr<ParameterInterface>& parameter);
117
118 ParameterInterfaceMap parameters_;
119
120};
121
122template<typename T>
123inline T ParameterMap::get_parameter_value(const std::string& name) const {
124 return this->get_parameter(name)->get_parameter_value<T>();
125}
126
127template<typename T>
128inline void ParameterMap::set_parameter_value(const std::string& name, const T& value) {
129 this->validate_and_set_parameter(make_shared_parameter<T>(name, value));
130}
131
132}
A wrapper class to contain a map of Parameter pointers by name and provide robust access methods.
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.
T get_parameter_value(const std::string &name) const
Get a parameter value by its name.
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.
void set_parameter_value(const std::string &name, const T &value)
Set a parameter value by its name.
Core state variables and objects.