Control Libraries 7.4.0
Loading...
Searching...
No Matches
ParameterInterface.hpp
1#pragma once
2
3#include <memory>
4
5#include "state_representation/parameters/ParameterType.hpp"
6#include "state_representation/exceptions/InvalidParameterCastException.hpp"
7#include "state_representation/exceptions/InvalidPointerException.hpp"
8#include "state_representation/State.hpp"
9
10namespace state_representation {
11
12// forward declaration of derived Parameter class
13template<typename T>
14class Parameter;
15
16class ParameterInterface : public State {
17public:
25 const std::string& name, const ParameterType& type, const StateType& parameter_state_type = StateType::NONE
26 );
27
32 ParameterInterface(const ParameterInterface& parameter);
33
37 virtual ~ParameterInterface() = default;
38
46
61 template<typename T>
62 std::shared_ptr<Parameter<T>> get_parameter(bool validate_pointer = true) const;
63
72 template<typename T>
73 T get_parameter_value() const;
74
83 template<typename T>
84 void set_parameter_value(const T& value);
85
91
99
100private:
101 ParameterType parameter_type_;
102 StateType parameter_state_type_;
103};
104
105template<typename T>
106inline std::shared_ptr<Parameter<T>> ParameterInterface::get_parameter(bool validate_pointer) const {
107 std::shared_ptr<Parameter<T>> parameter_ptr;
108 try {
109 parameter_ptr = std::dynamic_pointer_cast<Parameter<T>>(std::const_pointer_cast<State>(shared_from_this()));
110 } catch (const std::exception&) {
111 if (validate_pointer) {
113 "Parameter interface \"" + get_name() + "\" is not managed by a valid pointer");
114 }
115 }
116 if (parameter_ptr == nullptr && validate_pointer) {
117 std::string type_name(typeid(T).name());
119 "Unable to cast parameter interface \"" + get_name() + "\" to a parameter pointer of requested type "
120 + type_name);
121 }
122 return parameter_ptr;
123}
124
125template<typename T>
127 return get_parameter<T>(true)->get_value();
128}
129
130template<typename T>
131inline void ParameterInterface::set_parameter_value(const T& value) {
132 get_parameter<T>(true)->set_value(value);
133}
134
135}// namespace state_representation
void set_parameter_value(const T &value)
Set the parameter value of a derived Parameter instance through the ParameterInterface pointer.
virtual ~ParameterInterface()=default
Default virtual destructor.
StateType get_parameter_state_type() const
Get the state type of the parameter.
std::shared_ptr< Parameter< T > > get_parameter(bool validate_pointer=true) const
Get a pointer to a derived Parameter instance from a ParameterInterface pointer.
ParameterInterface & operator=(const ParameterInterface &state)
Copy assignment operator that has to be defined to the custom assignment operator.
T get_parameter_value() const
Get the parameter value of a derived Parameter instance through the ParameterInterface pointer.
ParameterType get_parameter_type() const
Get the parameter type.
Abstract class to represent a state.
Definition State.hpp:25
const std::string & get_name() const
Getter of the name attribute.
Definition State.cpp:29
Core state variables and objects.
ParameterType
The parameter value types.
StateType
The class types inheriting from State.
Definition StateType.hpp:13