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
42class State {
43
44protected:
46 std::set<std::string> outcomes;
47
48private:
50 std::atomic_bool canceled{false};
52 std::atomic_bool running{false};
53
54public:
59 State(std::set<std::string> outcomes);
60
72 std::string operator()(std::shared_ptr<blackboard::Blackboard> blackboard);
73
83 virtual std::string
84 execute(std::shared_ptr<blackboard::Blackboard> blackboard) {
85 (void)blackboard; // Suppress unused parameter warning
86 return "";
87 }
88
94 virtual void cancel_state() {
95 YASMIN_LOG_INFO("Canceling state '%s'", this->to_string().c_str());
96 this->canceled.store(true);
97 }
98
103 bool is_canceled() const;
104
109 bool is_running() const;
110
115 std::set<std::string> const &get_outcomes();
116
124 virtual std::string to_string() {
125 std::string name = typeid(*this).name();
126
127#ifdef __GNUG__ // If using GCC/G++
128 int status;
129 // Demangle the name using GCC's demangling function
130 char *demangled =
131 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
132 if (status == 0) {
133 name = demangled;
134 }
135 free(demangled);
136#endif
137
138 return name; // Return the demangled class name
139 }
140};
141
142} // namespace yasmin
143
144#endif // YASMIN__STATE_HPP
Represents a state in a state machine.
Definition state.hpp:42
bool is_canceled() const
Checks if the state has been canceled.
Definition state.cpp:71
std::atomic_bool running
Indicates if the state is currently running.
Definition state.hpp:52
std::atomic_bool canceled
Indicates if the state has been canceled.
Definition state.hpp:50
virtual std::string execute(std::shared_ptr< blackboard::Blackboard > blackboard)
Executes the state's specific logic.
Definition state.hpp:84
std::set< std::string > const & get_outcomes()
Gets the set of possible outcomes for this state.
Definition state.cpp:75
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:46
State(std::set< std::string > outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
bool is_running() const
Checks if the state is currently running.
Definition state.cpp:73
virtual std::string to_string()
Converts the state to a string representation.
Definition state.hpp:124
virtual void cancel_state()
Cancels the current state execution.
Definition state.hpp:94
std::string operator()(std::shared_ptr< blackboard::Blackboard > blackboard)
Executes the state and returns the outcome.
Definition state.cpp:30
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:71
Definition blackboard.hpp:29