Control Libraries 7.4.0
Loading...
Searching...
No Matches
State.cpp
1#include "state_representation/State.hpp"
2#include "state_representation/exceptions/EmptyStateException.hpp"
3#include "state_representation/exceptions/NotImplementedException.hpp"
4
5namespace state_representation {
6
7State::State() : type_(StateType::STATE), empty_(true), timestamp_(std::chrono::steady_clock::now()) {}
8
9State::State(const std::string& name) :
10 type_(StateType::STATE), name_(name), empty_(true), timestamp_(std::chrono::steady_clock::now()) {}
11
12State::State(const State& state) :
13 std::enable_shared_from_this<State>(state),
14 type_(StateType::STATE),
15 name_(state.name_),
16 empty_(state.empty_),
17 timestamp_(state.timestamp_) {}
18
20 State tmp(state);
21 swap(*this, tmp);
22 return *this;
23}
24
25const StateType& State::get_type() const {
26 return this->type_;
27}
28
29const std::string& State::get_name() const {
30 return this->name_;
31}
32
33bool State::is_empty() const {
34 return this->empty_;
35}
36
37const std::chrono::time_point<std::chrono::steady_clock>& State::get_timestamp() const {
38 return this->timestamp_;
39}
40
41void State::set_type(const StateType& type) {
42 this->type_ = type;
43}
44
45void State::set_name(const std::string& name) {
46 this->name_ = name;
47 this->reset_timestamp();
48}
49
50void State::set_empty(bool empty) {
51 this->empty_ = empty;
52 this->reset_timestamp();
53}
54
56 if (this->empty_) {
57 throw exceptions::EmptyStateException(this->name_ + " state is empty");
58 }
59}
60
62 this->timestamp_ = std::chrono::steady_clock::now();
63}
64
65void State::set_data(const Eigen::VectorXd&) {
66 throw exceptions::NotImplementedException("set_data() is not implemented for the base State class");
67}
68
69void State::set_data(const std::vector<double>&) {
70 throw exceptions::NotImplementedException("set_data() is not implemented for the base State class");
71}
72
73void State::set_data(const Eigen::MatrixXd&) {
74 throw exceptions::NotImplementedException("set_data() is not implemented for the base State class");
75}
76
78 this->set_empty();
79}
80
81double State::get_age() const {
82 return static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(
83 std::chrono::steady_clock::now() - this->timestamp_
84 ).count()) / 1e9;
85}
86
87bool State::is_deprecated(double time_delay) const {
88 return this->get_age() >= time_delay;
89}
90
91bool State::is_incompatible(const State&) const {
92 return false;
93}
94
95State::operator bool() const noexcept {
96 return !this->empty_;
97}
98
99std::string State::to_string() const {
100 std::string prefix = this->is_empty() ? "Empty " : "";
101 return prefix + get_state_type_name(this->type_) + ": '" + this->get_name() + "'";
102}
103
104std::ostream& operator<<(std::ostream& os, const State& state) {
105 os << state.to_string();
106 return os;
107}
108}// namespace state_representation
Abstract class to represent a state.
Definition State.hpp:25
void reset_timestamp()
Reset the timestamp attribute to now.
Definition State.cpp:61
const std::string & get_name() const
Getter of the name attribute.
Definition State.cpp:29
virtual void set_name(const std::string &name)
Setter of the name attribute.
Definition State.cpp:45
State()
Empty constructor.
Definition State.cpp:7
const StateType & get_type() const
Getter of the type attribute.
Definition State.cpp:25
void set_type(const StateType &type)
Setter of the state type attribute.
Definition State.cpp:41
virtual std::string to_string() const
Convert the state to its string representation.
Definition State.cpp:99
const std::chrono::time_point< std::chrono::steady_clock > & get_timestamp() const
Getter of the timestamp attribute.
Definition State.cpp:37
virtual void reset()
Reset the object to a post-construction state.
Definition State.cpp:77
virtual bool is_incompatible(const State &state) const
Check if the state is incompatible for operations with the state given as argument.
Definition State.cpp:91
State & operator=(const State &state)
Copy assignment operator that has to be defined to the custom assignment operator.
Definition State.cpp:19
void assert_not_empty() const
Throw an exception if the state is empty.
Definition State.cpp:55
double get_age() const
Get the age of the state, i.e. the time since the last modification.
Definition State.cpp:81
bool is_deprecated(double time_delay) const
Check if the state is deprecated given a certain time delay.
Definition State.cpp:87
friend void swap(State &state1, State &state2)
Swap the values of the two states.
Definition State.hpp:181
virtual void set_data(const Eigen::VectorXd &data)
Set the data of the state from an Eigen vector.
Definition State.cpp:65
bool is_empty() const
Getter of the empty attribute.
Definition State.cpp:33
void set_empty(bool empty=true)
Setter of the empty attribute.
Definition State.cpp:50
Core state variables and objects.
std::ostream & operator<<(std::ostream &os, const AnalogIOState &state)
StateType
The class types inheriting from State.
Definition StateType.hpp:13