Control Libraries 7.4.0
Loading...
Searching...
No Matches
IOState.hpp
1#pragma once
2
3#include "state_representation/State.hpp"
4
5#include "state_representation/exceptions/IONotFoundException.hpp"
6#include "state_representation/exceptions/IncompatibleSizeException.hpp"
7#include "state_representation/exceptions/InvalidCastException.hpp"
8
9namespace state_representation {
10
11template<typename T>
12class IOState : public State {
13public:
17 unsigned int get_size() const;
18
23 const std::vector<std::string>& get_names() const;
24
31 unsigned int get_io_index(const std::string& io_name) const;
32
39 T get_value(const std::string& name) const;
40
47 T get_value(unsigned int io_index) const;
48
52 Eigen::Vector<T, Eigen::Dynamic> data() const;
53
57 Eigen::Array<T, Eigen::Dynamic, 1> array() const;
58
63 void set_names(unsigned int nb_ios);
64
69 void set_names(const std::vector<std::string>& names);
70
77 void set_value(T value, const std::string& name);
78
85 void set_value(T value, unsigned int io_index);
86
91 void set_data(const Eigen::Vector<T, Eigen::Dynamic>& data);
92
97 void set_data(const std::vector<T>& data);
98
103 bool is_incompatible(const State& state) const override;
104
109 std::vector<T> to_std_vector() const;
110
111protected:
115 IOState() = default;
116
122 explicit IOState(const std::string& name, unsigned int nb_ios);
123
129 IOState(const std::string& name, const std::vector<std::string>& io_names);
130
136 friend void swap(IOState& state1, IOState& state2) {
137 swap(static_cast<State&>(state1), static_cast<State&>(state2));
138 std::swap(state1.names_, state2.names_);
139 std::swap(state1.data_, state2.data_);
140 }
141
142 static void assert_index_in_range(unsigned int io_index, unsigned int size);
143
144 std::vector<std::string> names_;
145 Eigen::Vector<T, Eigen::Dynamic> data_;
146};
147
148template<typename T>
149IOState<T>::IOState(const std::string& name, unsigned int nb_ios) :
150 State(name),
151 names_(nb_ios) {
152 this->set_names(nb_ios);
153}
154
155template<typename T>
156IOState<T>::IOState(const std::string& name, const std::vector<std::string>& io_names) :
157 IOState<T>(name, io_names.size()) {
158 this->set_names(io_names);
159}
160
161template<typename T>
162unsigned int IOState<T>::get_size() const {
163 return this->names_.size();
164}
165
166template<typename T>
167const std::vector<std::string>& IOState<T>::get_names() const {
168 return this->names_;
169}
170
171template<typename T>
172unsigned int IOState<T>::get_io_index(const std::string& io_name) const {
173 auto finder = std::find(this->names_.begin(), this->names_.end(), io_name);
174 if (finder == this->names_.end()) {
175 throw exceptions::IONotFoundException("The IO with name '" + io_name + "' could not be found in the IO state.");
176 }
177 return std::distance(this->names_.begin(), finder);
178}
179
180template<typename T>
181T IOState<T>::get_value(const std::string& io_name) const {
182 return this->get_value(this->get_io_index(io_name));
183}
184
185template<typename T>
186T IOState<T>::get_value(unsigned int io_index) const {
187 this->assert_not_empty();
188 IOState<T>::assert_index_in_range(io_index, this->get_size());
189 return this->data_(io_index);
190}
191
192template<typename T>
193Eigen::Vector<T, Eigen::Dynamic> IOState<T>::data() const {
194 this->assert_not_empty();
195 return this->data_;
196}
197
198template<typename T>
199Eigen::Array<T, Eigen::Dynamic, 1> IOState<T>::array() const {
200 this->assert_not_empty();
201 return this->data_.array();
202}
203template<typename T>
204void IOState<T>::set_names(unsigned int nb_ios) {
205 if (this->get_size() != nb_ios) {
207 "Input number of IOs is of incorrect size, expected " + std::to_string(this->get_size()) + " got "
208 + std::to_string(nb_ios));
209 }
210 for (unsigned int i = 0; i < nb_ios; ++i) {
211 this->names_[i] = "io" + std::to_string(i);
212 }
213 this->reset_timestamp();
214}
215
216template<typename T>
217void IOState<T>::set_names(const std::vector<std::string>& names) {
218 if (this->get_size() != names.size()) {
220 "Input number of IOs is of incorrect size, expected " + std::to_string(this->get_size()) + " got "
221 + std::to_string(names.size()));
222 }
223 this->names_ = names;
224 this->reset_timestamp();
225}
226
227template<typename T>
228void IOState<T>::set_value(T value, const std::string& io_name) {
229 this->set_value(value, this->get_io_index(io_name));
230}
231
232template<typename T>
233void IOState<T>::set_value(T value, unsigned int io_index) {
234 IOState<T>::assert_index_in_range(io_index, this->get_size());
235 this->data_(io_index) = value;
236 this->set_empty(false);
237}
238
239template<typename T>
240void IOState<T>::set_data(const Eigen::Vector<T, Eigen::Dynamic>& data) {
241 if (data.size() != this->get_size()) {
243 "Input is of incorrect size, expected " + std::to_string(this->get_size()) + ", got "
244 + std::to_string(data.size()));
245 }
246 this->data_ = data;
247 this->set_empty(false);
248}
249
250template<typename T>
251bool IOState<T>::is_incompatible(const State& state) const {
252 try {
253 auto other = dynamic_cast<const IOState<T>&>(state);
254 if (this->names_.size() != other.names_.size()) {
255 return true;
256 }
257 for (unsigned int i = 0; i < this->names_.size(); ++i) {
258 if (this->names_[i] != other.names_[i]) {
259 return true;
260 }
261 }
262 return false;
263 } catch (const std::bad_cast& ex) {
264 throw exceptions::InvalidCastException(std::string("Could not cast the given object to an IOState: ") + ex.what());
265 }
266}
267
268template<typename T>
269void IOState<T>::assert_index_in_range(unsigned int io_index, unsigned int size) {
270 if (io_index > size) {
272 "Index '" + std::to_string(io_index) + "' is out of range for IO state with size" + std::to_string(size));
273 }
274}
275
276}// namespace state_representation
Eigen::Vector< T, Eigen::Dynamic > data_
IO values.
Definition IOState.hpp:145
void set_value(T value, unsigned int io_index)
Set the value of an IO by its index.
Definition IOState.hpp:233
IOState(const std::string &name, unsigned int nb_ios)
Constructor with name and number of IOs provided.
Definition IOState.hpp:149
void set_data(const Eigen::Vector< T, Eigen::Dynamic > &data)
Set the values of the IO state from a single Eigen vector.
Definition IOState.hpp:240
friend void swap(IOState &state1, IOState &state2)
Swap the values of the IO states.
Definition IOState.hpp:136
Eigen::Vector< T, Eigen::Dynamic > data() const
Returns the values of the IO state as an Eigen vector.
Definition IOState.hpp:193
T get_value(const std::string &name) const
Get the value of an IO by its name, if it exists.
Definition IOState.hpp:181
void set_names(unsigned int nb_ios)
Setter of the names from the number of IOs.
Definition IOState.hpp:204
unsigned int get_size() const
Getter of the size.
Definition IOState.hpp:162
IOState(const std::string &name, const std::vector< std::string > &io_names)
Constructor with name and list of IO names provided.
Definition IOState.hpp:156
std::vector< std::string > names_
names of the IOs
Definition IOState.hpp:144
Eigen::Array< T, Eigen::Dynamic, 1 > array() const
Returns the values of the IO state an Eigen array.
Definition IOState.hpp:199
void set_data(const std::vector< T > &data)
Set the values of the IO state from a single std vector.
std::vector< T > to_std_vector() const
Return the IO values as a std vector.
T get_value(unsigned int io_index) const
Get the value of an IO by its index, if it exists.
Definition IOState.hpp:186
void set_value(T value, const std::string &name)
Set the value of an IO by its name.
Definition IOState.hpp:228
const std::vector< std::string > & get_names() const
Getter of the names.
Definition IOState.hpp:167
IOState()=default
Empty constructor for an IO state.
unsigned int get_io_index(const std::string &io_name) const
Get IO index by the name of the IO, if it exists.
Definition IOState.hpp:172
void set_names(const std::vector< std::string > &names)
Setter of the names from a list of IO names.
Definition IOState.hpp:217
bool is_incompatible(const State &state) const override
Check if the IO group is incompatible for operations with the state given as argument.
Definition IOState.hpp:251
Abstract class to represent a state.
Definition State.hpp:25
Exception that is thrown when a IO name or index is out of range.
Core state variables and objects.