3#include "state_representation/State.hpp"
5#include "state_representation/exceptions/IONotFoundException.hpp"
6#include "state_representation/exceptions/IncompatibleSizeException.hpp"
7#include "state_representation/exceptions/InvalidCastException.hpp"
52 Eigen::Vector<T, Eigen::Dynamic>
data()
const;
57 Eigen::Array<T, Eigen::Dynamic, 1>
array()
const;
69 void set_names(
const std::vector<std::string>& names);
122 explicit IOState(
const std::string& name,
unsigned int nb_ios);
129 IOState(
const std::string& name,
const std::vector<std::string>& io_names);
142 static void assert_index_in_range(
unsigned int io_index,
unsigned int size);
145 Eigen::Vector<T, Eigen::Dynamic>
data_;
157 IOState<T>(name, io_names.size()) {
163 return this->names_.size();
173 auto finder = std::find(this->names_.begin(), this->names_.end(), io_name);
174 if (finder == this->names_.end()) {
177 return std::distance(this->names_.begin(), finder);
182 return this->get_value(this->get_io_index(io_name));
187 this->assert_not_empty();
189 return this->data_(io_index);
194 this->assert_not_empty();
200 this->assert_not_empty();
201 return this->data_.array();
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));
210 for (
unsigned int i = 0; i < nb_ios; ++i) {
211 this->names_[i] =
"io" + std::to_string(i);
213 this->reset_timestamp();
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()));
223 this->names_ = names;
224 this->reset_timestamp();
229 this->set_value(value, this->get_io_index(io_name));
235 this->data_(io_index) = value;
236 this->set_empty(
false);
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()));
247 this->set_empty(
false);
253 auto other =
dynamic_cast<const IOState<T>&
>(state);
254 if (this->names_.size() != other.names_.size()) {
257 for (
unsigned int i = 0; i < this->names_.size(); ++i) {
258 if (this->names_[i] != other.names_[i]) {
263 }
catch (
const std::bad_cast& ex) {
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));
Eigen::Vector< T, Eigen::Dynamic > data_
IO values.
void set_value(T value, unsigned int io_index)
Set the value of an IO by its index.
IOState(const std::string &name, unsigned int nb_ios)
Constructor with name and number of IOs provided.
void set_data(const Eigen::Vector< T, Eigen::Dynamic > &data)
Set the values of the IO state from a single Eigen vector.
friend void swap(IOState &state1, IOState &state2)
Swap the values of the IO states.
Eigen::Vector< T, Eigen::Dynamic > data() const
Returns the values of the IO state as an Eigen vector.
T get_value(const std::string &name) const
Get the value of an IO by its name, if it exists.
void set_names(unsigned int nb_ios)
Setter of the names from the number of IOs.
unsigned int get_size() const
Getter of the size.
IOState(const std::string &name, const std::vector< std::string > &io_names)
Constructor with name and list of IO names provided.
std::vector< std::string > names_
names of the IOs
Eigen::Array< T, Eigen::Dynamic, 1 > array() const
Returns the values of the IO state an Eigen array.
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.
void set_value(T value, const std::string &name)
Set the value of an IO by its name.
const std::vector< std::string > & get_names() const
Getter of the names.
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.
void set_names(const std::vector< std::string > &names)
Setter of the names from a list of IO names.
bool is_incompatible(const State &state) const override
Check if the IO group is incompatible for operations with the state given as argument.
Abstract class to represent a state.
Exception that is thrown when a IO name or index is out of range.
Core state variables and objects.