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 <memory>
21#include <set>
22#include <string>
23#include <vector>
24
25#ifdef __GNUG__ // If using GCC/G++
26#include <cxxabi.h> // For abi::__cxa_demangle
27#endif
28
30#include "yasmin/logs.hpp"
31
32namespace yasmin {
33
44
53class State {
54
55protected:
57 std::set<std::string> outcomes;
58
59private:
61 std::atomic<StateStatus> status{StateStatus::IDLE};
62
63public:
68 State(const std::set<std::string> &outcomes);
69
74 StateStatus get_status() const;
75
80 bool is_idle() const;
81
86 bool is_running() const;
87
92 bool is_canceled() const;
93
98 bool is_completed() const;
99
111 std::string operator()(std::shared_ptr<blackboard::Blackboard> blackboard);
112
122 virtual std::string
123 execute(std::shared_ptr<blackboard::Blackboard> blackboard) {
124 (void)blackboard; // Suppress unused parameter warning
125 return "";
126 }
127
133 virtual void cancel_state() {
134 YASMIN_LOG_INFO("Canceling state '%s'", this->to_string().c_str());
135 this->status.store(StateStatus::CANCELED);
136 }
137
142 std::set<std::string> const &get_outcomes();
143
151 virtual std::string to_string() {
152 std::string name = typeid(*this).name();
153
154#ifdef __GNUG__ // If using GCC/G++
155 int status;
156 // Demangle the name using GCC's demangling function
157 char *demangled =
158 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
159 if (status == 0) {
160 name = demangled;
161 }
162 free(demangled);
163#endif
164
165 return name; // Return the demangled class name
166 }
167};
168
169} // namespace yasmin
170
171#endif // YASMIN__STATE_HPP
StateStatus get_status() const
Gets the current status of the state.
Definition state.cpp:33
bool is_canceled() const
Checks if the state has been canceled.
Definition state.cpp:41
bool is_completed() const
Checks if the state has completed execution.
Definition state.cpp:45
std::atomic< StateStatus > status
Current status of the state.
Definition state.hpp:61
State(const std::set< std::string > &outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
virtual std::string execute(std::shared_ptr< blackboard::Blackboard > blackboard)
Executes the state's specific logic.
Definition state.hpp:123
std::set< std::string > const & get_outcomes()
Gets the set of possible outcomes for this state.
Definition state.cpp:97
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:57
bool is_running() const
Checks if the state is currently running.
Definition state.cpp:37
virtual std::string to_string()
Converts the state to a string representation.
Definition state.hpp:151
virtual void cancel_state()
Cancels the current state execution.
Definition state.hpp:133
bool is_idle() const
Checks if the state is idle.
Definition state.cpp:35
std::string operator()(std::shared_ptr< blackboard::Blackboard > blackboard)
Executes the state and returns the outcome.
Definition state.cpp:50
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
Definition blackboard.hpp:30
Definition blackboard.hpp:29
StateStatus
Enumeration representing the current status of a state.
Definition state.hpp:38
@ RUNNING
State is currently executing.
Definition state.hpp:40
@ COMPLETED
State execution has completed successfully.
Definition state.hpp:42
@ IDLE
State is idle and ready to execute.
Definition state.hpp:39
@ CANCELED
State execution has been canceled.
Definition state.hpp:41