Control Libraries 7.4.0
Loading...
Searching...
No Matches
State.hpp
1#pragma once
2
3#include <cassert>
4#include <chrono>
5#include <iostream>
6#include <memory>
7#include <typeinfo>
8
9#include <eigen3/Eigen/Core>
10#include <eigen3/Eigen/Dense>
11
12#include "state_representation/StateType.hpp"
13#include "state_representation/MathTools.hpp"
14
19namespace state_representation {
20
25class State : public std::enable_shared_from_this<State> {
26public:
30 State();
31
36 explicit State(const std::string& name);
37
41 State(const State& state);
42
46 virtual ~State() = default;
47
53 friend void swap(State& state1, State& state2);
54
60 State& operator=(const State& state);
61
65 const StateType& get_type() const;
66
70 const std::string& get_name() const;
71
75 bool is_empty() const;
76
80 const std::chrono::time_point<std::chrono::steady_clock>& get_timestamp() const;
81
85 virtual void set_name(const std::string& name);
86
90 void reset_timestamp();
91
95 virtual void set_data(const Eigen::VectorXd& data);
96
100 virtual void set_data(const std::vector<double>& data);
101
105 virtual void set_data(const Eigen::MatrixXd& data);
106
110 double get_age() const;
111
116 virtual bool is_incompatible(const State& state) const;
117
122 bool is_deprecated(double time_delay) const;
123
128 template<typename DurationT>
129 bool is_deprecated(const std::chrono::duration<int64_t, DurationT>& time_delay) const;
130
134 virtual void reset();
135
140 explicit operator bool() const noexcept;
141
148 friend std::ostream& operator<<(std::ostream& os, const State& state);
149
150protected:
154 void set_type(const StateType& type);
155
161 void set_empty(bool empty = true);
162
167 void assert_not_empty() const;
168
172 virtual std::string to_string() const;
173
174private:
175 StateType type_;
176 std::string name_;
177 bool empty_;
178 std::chrono::time_point<std::chrono::steady_clock> timestamp_;
179};
180
181inline void swap(State& state1, State& state2) {
182 std::swap(state1.name_, state2.name_);
183 std::swap(state1.empty_, state2.empty_);
184 std::swap(state1.timestamp_, state2.timestamp_);
185}
186
187template<typename DurationT>
188inline bool State::is_deprecated(const std::chrono::duration<int64_t, DurationT>& time_delay) const {
189 return ((std::chrono::steady_clock::now() - this->timestamp_) > time_delay);
190}
191
192template<typename T>
193std::shared_ptr<State> make_shared_state(const T& state) {
194 return std::make_shared<T>(state);
195}
196}// 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
virtual ~State()=default
Virtual destructor.
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.
StateType
The class types inheriting from State.
Definition StateType.hpp:13