Control Libraries 7.4.0
Loading...
Searching...
No Matches
Parameter.hpp
1#pragma once
2
3#include <memory>
4
5#include "state_representation/parameters/ParameterInterface.hpp"
6#include "state_representation/geometry/Ellipsoid.hpp"
7#include "state_representation/space/cartesian/CartesianPose.hpp"
8#include "state_representation/space/joint/JointPositions.hpp"
9
10namespace state_representation {
11
17template<typename T>
19public:
24 explicit Parameter(const std::string& name = "");
25
31 Parameter(const std::string& name, const T& value);
32
37 template<typename U>
38 explicit Parameter(const Parameter<U>& parameter);
39
43 virtual ~Parameter() = default;
44
48 template<typename U>
50
56 template<typename U>
57 U get_value() const;
58
63 const T& get_value() const;
64
70
75 virtual void set_value(const T& value);
76
80 void reset() override;
81
88 template<typename U>
89 friend std::ostream& operator<<(std::ostream& os, const Parameter<U>& parameter);
90
91private:
92 T value_;
93};
94
95template<typename T>
96template<typename U>
97Parameter<T>::Parameter(const Parameter<U>& parameter) : Parameter<T>(parameter.get_name()) {
98 if (parameter) {
99 this->set_value(static_cast<T>(parameter.get_value()));
100 }
101}
102
103template<typename T>
104template<typename U>
106 Parameter<T> temp(parameter);
107 *this = temp;
108 return *this;
109}
110
111template<typename T>
112template<typename U>
113inline U Parameter<T>::get_value() const {
114 this->assert_not_empty();
115 return static_cast<U>(this->value_);
116}
117
118template<typename T>
119inline const T& Parameter<T>::get_value() const {
120 this->assert_not_empty();
121 return this->value_;
122}
123
124template<typename T>
126 this->assert_not_empty();
127 return this->value_;
128}
129
130template<typename T>
131inline void Parameter<T>::set_value(const T& value) {
132 this->value_ = value;
133 this->set_empty(false);
134}
135
136template<typename T>
137inline void Parameter<T>::reset() {
138 this->State::reset();
139 this->value_ = T();
140}
141
148template<typename T>
149static std::shared_ptr<Parameter<T>> make_shared_parameter(const std::string& name) {
150 return std::make_shared<Parameter<T>>(name);
151}
152
160template<typename T>
161static std::shared_ptr<Parameter<T>> make_shared_parameter(const std::string& name, const T& param_value) {
162 return std::make_shared<Parameter<T>>(name, param_value);
163}
164
172[[maybe_unused]] static std::shared_ptr<ParameterInterface> make_shared_parameter_interface(
173 const std::string& name, const ParameterType& type, const StateType& parameter_state_type = StateType::NONE
174) {
175 switch (type) {
176 case ParameterType::BOOL:
177 return std::make_shared<Parameter<bool>>(name);
178 case ParameterType::BOOL_ARRAY:
179 return std::make_shared<Parameter<std::vector<bool>>>(name);
180 case ParameterType::INT:
181 return std::make_shared<Parameter<int>>(name);
182 case ParameterType::INT_ARRAY:
183 return std::make_shared<Parameter<std::vector<int>>>(name);
184 case ParameterType::DOUBLE:
185 return std::make_shared<Parameter<double>>(name);
186 case ParameterType::DOUBLE_ARRAY:
187 return std::make_shared<Parameter<std::vector<double>>>(name);
188 case ParameterType::STRING:
189 return std::make_shared<Parameter<std::string>>(name);
190 case ParameterType::STRING_ARRAY:
191 return std::make_shared<Parameter<std::vector<std::string>>>(name);
192 case ParameterType::STATE: {
193 switch (parameter_state_type) {
194 case StateType::CARTESIAN_STATE:
195 return std::make_shared<Parameter<CartesianState>>(name);
196 case StateType::CARTESIAN_POSE:
197 return std::make_shared<Parameter<CartesianPose>>(name);
198 case StateType::JOINT_STATE:
199 return std::make_shared<Parameter<JointState>>(name);
200 case StateType::JOINT_POSITIONS:
201 return std::make_shared<Parameter<JointPositions>>(name);
202 case StateType::GEOMETRY_ELLIPSOID:
203 return std::make_shared<Parameter<Ellipsoid>>(name);
204 case StateType::NONE:
205 throw exceptions::InvalidParameterException("No StateType provided.");
206 default:
207 throw exceptions::InvalidParameterException("This StateType is not supported for parameters.");
208 }
209 }
210 case ParameterType::VECTOR:
211 return std::make_shared<Parameter<Eigen::VectorXd>>(name);
212 case ParameterType::MATRIX:
213 return std::make_shared<Parameter<Eigen::MatrixXd>>(name);
214 default:
215 throw exceptions::InvalidParameterException("This ParameterType is not supported for parameters.");
216 }
217}
218}// namespace state_representation
Class to represent name-value pairs of different types.
Definition Parameter.hpp:18
Parameter(const std::string &name="")
Constructor with the name of the parameter.
void reset() override
Reset the object to a post-construction state.
friend std::ostream & operator<<(std::ostream &os, const Parameter< U > &parameter)
Overload the ostream operator for printing.
Parameter(const std::string &name, const T &value)
Constructor with a name and a value.
virtual ~Parameter()=default
Default virtual destructor.
U get_value() const
Getter of the value attribute.
const T & get_value() const
Getter of the value attribute.
Parameter(const Parameter< U > &parameter)
Copy constructor.
Definition Parameter.hpp:97
Parameter< T > & operator=(const Parameter< U > &parameter)
Conversion equality.
virtual void set_value(const T &value)
Setter of the value attribute.
T & get_value()
Getter of the value attribute.
virtual void reset()
Reset the object to a post-construction state.
Definition State.cpp:77
Core state variables and objects.
ParameterType
The parameter value types.
StateType
The class types inheriting from State.
Definition StateType.hpp:13