.. _program_listing_file_include_yasmin_state_machine.hpp: Program Listing for File state_machine.hpp ========================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/yasmin/state_machine.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright (C) 2023 Miguel Ángel González Santamarta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #ifndef YASMIN__STATE_MACHINE_HPP #define YASMIN__STATE_MACHINE_HPP #include #include #include #include #include #include #include #include #include "yasmin/blackboard/blackboard.hpp" #include "yasmin/state.hpp" namespace yasmin { class StateMachine : public State { using StartCallbackType = std::function, const std::string &, const std::vector &)>; using TransitionCallbackType = std::function, const std::string &, const std::string &, const std::string &, const std::vector &)>; using EndCallbackType = std::function, const std::string &, const std::vector &)>; public: StateMachine(std::set outcomes); void add_state(std::string name, std::shared_ptr state, std::map transitions = {}, std::map remapping = {}); void set_start_state(std::string state_name); std::string get_start_state(); std::map> const &get_states(); std::map> const & get_transitions(); std::string get_current_state(); void add_start_cb(StartCallbackType cb, std::vector args = {}); void add_transition_cb(TransitionCallbackType cb, std::vector args = {}); void add_end_cb(EndCallbackType cb, std::vector args = {}); void call_start_cbs(std::shared_ptr blackboard, const std::string &start_state); void call_transition_cbs( std::shared_ptr blackboard, const std::string &from_state, const std::string &to_state, const std::string &outcome); void call_end_cbs(std::shared_ptr blackboard, const std::string &outcome); void validate(bool strict_mode = false); std::string execute(std::shared_ptr blackboard) override; std::string execute(); std::string operator()(); using State::operator(); void cancel_state() override; std::string to_string(); private: std::map> states; std::map> transitions; std::map> remappings; std::string start_state; std::string current_state; std::unique_ptr current_state_mutex; std::atomic_bool validated{false}; std::vector>> start_cbs; std::vector>> transition_cbs; std::vector>> end_cbs; }; } // namespace yasmin #endif // YASMIN__STATE_MACHINE_HPP