Control Libraries 6.3.4
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 explicit State();
31
36 explicit State(const StateType& type);
37
44 explicit State(const StateType& type, const std::string& name, const bool& empty = true);
45
49 State(const State& state);
50
54 virtual ~State() = default;
55
61 friend void swap(State& state1, State& state2);
62
68 State& operator=(const State& state);
69
74 const StateType& get_type() const;
75
79 bool is_empty() const;
80
85 void set_empty(bool empty = true);
86
90 void set_filled();
91
95 const std::chrono::time_point<std::chrono::steady_clock>& get_timestamp() const;
96
101 void set_timestamp(const std::chrono::time_point<std::chrono::steady_clock>& timepoint);
102
106 void reset_timestamp();
107
111 const std::string& get_name() const;
112
116 virtual void set_name(const std::string& name);
117
122 template<typename DurationT>
123 bool is_deprecated(const std::chrono::duration<int64_t, DurationT>& time_delay);
124
129 virtual bool is_compatible(const State& state) const;
130
134 virtual void initialize();
135
140 virtual void set_data(const Eigen::VectorXd& data);
141
146 virtual void set_data(const std::vector<double>& data);
147
152 virtual void set_data(const Eigen::MatrixXd& data);
153
160 friend std::ostream& operator<<(std::ostream& os, const State& state);
161
166 explicit operator bool() const noexcept;
167
168protected:
173 void set_type(const StateType& type);
174
175private:
176 StateType type_;
177 std::string name_;
178 bool empty_;
179 std::chrono::time_point<std::chrono::steady_clock> timestamp_;
180};
181
182inline void swap(State& state1, State& state2) {
183 std::swap(state1.type_, state2.type_);
184 std::swap(state1.name_, state2.name_);
185 std::swap(state1.empty_, state2.empty_);
186 std::swap(state1.timestamp_, state2.timestamp_);
187}
188
189inline State& State::operator=(const State& state) {
190 State tmp(state);
191 swap(*this, tmp);
192 return *this;
193}
194
195template<typename DurationT>
196inline bool State::is_deprecated(const std::chrono::duration<int64_t, DurationT>& time_delay) {
197 return ((std::chrono::steady_clock::now() - this->timestamp_) > time_delay);
198}
199
200template<typename T>
201std::shared_ptr<State> make_shared_state(const T& state) {
202 return std::make_shared<T>(state);
203}
204
205}// 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:44
bool is_deprecated(const std::chrono::duration< int64_t, DurationT > &time_delay)
Check if the state is deprecated given a certain time delay.
Definition: State.hpp:196
virtual void initialize()
Initialize the State to a zero value.
Definition: State.cpp:61
const std::string & get_name() const
Getter of the name as const reference.
Definition: State.cpp:48
virtual void set_name(const std::string &name)
Setter of the name.
Definition: State.cpp:52
void set_timestamp(const std::chrono::time_point< std::chrono::steady_clock > &timepoint)
Setter of the timestamp attribute.
Definition: State.cpp:40
State()
Empty constructor.
Definition: State.cpp:5
const StateType & get_type() const
Getter of the type attribute.
Definition: State.cpp:19
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
Definition: State.cpp:31
void set_type(const StateType &type)
Override the state type.
Definition: State.cpp:89
virtual bool is_compatible(const State &state) const
Check if the state is compatible for operations with the state given as argument.
Definition: State.cpp:56
friend std::ostream & operator<<(std::ostream &os, const State &state)
Overload the ostream operator for printing.
Definition: State.cpp:77
const std::chrono::time_point< std::chrono::steady_clock > & get_timestamp() const
Getter of the timestamp attribute.
Definition: State.cpp:36
State & operator=(const State &state)
Copy assignment operator that have to be defined to the custom assignment operator.
Definition: State.hpp:189
virtual ~State()=default
Virtual destructor.
friend void swap(State &state1, State &state2)
Swap the values of the two States.
Definition: State.hpp:182
virtual void set_data(const Eigen::VectorXd &data)
Set the data of the state from a single Eigen vector.
Definition: State.cpp:65
bool is_empty() const
Getter of the empty attribute.
Definition: State.cpp:23
void set_empty(bool empty=true)
Setter of the empty attribute.
Definition: State.cpp:27
Core state variables and objects.
StateType
The class types inheriting from State.
Definition: StateType.hpp:13