C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
state.hpp
Go to the documentation of this file.
1// Copyright (C) 2023 Miguel Ángel González Santamarta
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN__STATE_HPP_
17#define YASMIN__STATE_HPP_
18
19#include <atomic>
20#include <string>
21
22#include "yasmin/blackboard.hpp"
23#include "yasmin/logs.hpp"
24
25namespace yasmin {
26
37
46class State {
47
48protected:
51
52private:
54 std::atomic<StateStatus> status{StateStatus::IDLE};
55
60 void set_status(StateStatus new_status);
61
66 StateStatus get_status() const;
67
68public:
73
74
78 State(const Outcomes &outcomes);
79
83 virtual ~State() = default;
84
89 bool is_idle() const noexcept;
90
95 bool is_running() const noexcept;
96
101 bool is_canceled() const noexcept;
102
107 bool is_completed() const noexcept;
108
120 std::string operator()(Blackboard::SharedPtr blackboard);
121
131 virtual std::string execute(Blackboard::SharedPtr blackboard) {
132 (void)blackboard; // Suppress unused parameter warning
133 return "";
134 }
135
141 virtual void cancel_state() {
142 YASMIN_LOG_INFO("Canceling state '%s'", this->to_string().c_str());
144 }
145
150 Outcomes const &get_outcomes() const noexcept;
151
159 virtual std::string to_string() const;
160};
161
162} // namespace yasmin
163
164#endif // YASMIN__STATE_HPP_
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:64
Represents a state in a state machine.
Definition state.hpp:46
bool is_running() const noexcept
Checks if the state is currently running.
Definition state.cpp:48
StateStatus get_status() const
Gets the current status of the state.
Definition state.cpp:42
Outcomes outcomes
The possible outcomes of this state.
Definition state.hpp:50
bool is_idle() const noexcept
Checks if the state is idle.
Definition state.cpp:44
virtual ~State()=default
Virtual destructor for proper polymorphic destruction.
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:32
virtual std::string execute(Blackboard::SharedPtr blackboard)
Executes the state's specific logic.
Definition state.hpp:131
std::atomic< StateStatus > status
Current status of the state.
Definition state.hpp:54
bool is_canceled() const noexcept
Checks if the state has been canceled.
Definition state.cpp:52
void set_status(StateStatus new_status)
Sets the current status of the state.
Definition state.cpp:38
bool is_completed() const noexcept
Checks if the state has completed execution.
Definition state.cpp:56
virtual std::string to_string() const
Converts the state to a string representation.
Definition state.cpp:108
Outcomes const & get_outcomes() const noexcept
Gets the set of possible outcomes for this state.
Definition state.cpp:106
virtual void cancel_state()
Cancels the current state execution.
Definition state.hpp:141
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
Definition blackboard.hpp:31
StringSet Outcomes
Set of possible outcomes for states.
Definition types.hpp:77
StateStatus
Enumeration representing the current status of a state.
Definition state.hpp:31
@ RUNNING
State is currently executing.
Definition state.hpp:33
@ COMPLETED
State execution has completed successfully.
Definition state.hpp:35
@ IDLE
State is idle and ready to execute.
Definition state.hpp:32
@ CANCELED
State execution has been canceled.
Definition state.hpp:34
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52