Program Listing for File state.hpp

Return to documentation for file (include/yasmin/state.hpp)

// 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 <https://www.gnu.org/licenses/>.

#ifndef YASMIN__STATE_HPP_
#define YASMIN__STATE_HPP_

#include <atomic>
#include <functional>
#include <string>
#include <unordered_map>

#include "yasmin/blackboard.hpp"
#include "yasmin/blackboard_key_info.hpp"
#include "yasmin/logs.hpp"

namespace yasmin {

enum class StateStatus {
  IDLE,
  RUNNING,
  CANCELED,
  COMPLETED
};

class State {

protected:
  Outcomes outcomes;

private:
  StateMetadata &get_metadata_ref() const;

  std::atomic<StateStatus> status{StateStatus::IDLE};

  void set_status(StateStatus new_status);

  StateStatus get_status() const;

public:
  YASMIN_PTR_ALIASES(State)


  State(const Outcomes &outcomes);

  virtual ~State();

  Blackboard::SharedPtr get_parameters_blackboard() const;

  bool is_idle() const noexcept;

  bool is_running() const noexcept;

  bool is_canceled() const noexcept;

  bool is_completed() const noexcept;

  std::string operator()(Blackboard::SharedPtr blackboard);

  virtual std::string execute(Blackboard::SharedPtr blackboard) {
    (void)blackboard; // Suppress unused parameter warning
    return "";
  }

  virtual void configure() {}

  virtual void cancel_state() {
    YASMIN_LOG_INFO("Canceling state '%s'", this->to_string().c_str());
    this->set_status(StateStatus::CANCELED);
  }

  Outcomes const &get_outcomes() const noexcept;

  void set_description(const std::string &description);

  const std::string &get_description() const;

  void set_outcome_description(const std::string &outcome,
                               const std::string &description);

  const std::string &get_outcome_description(const std::string &outcome) const;

  const std::unordered_map<std::string, std::string> &
  get_outcome_descriptions() const;

  void add_input_key(const BlackboardKeyInfo &key_info);

  void add_input_key(const std::string &key_name);

  void add_input_key(const std::string &key_name,
                     const std::string &description);

  template <typename T>
  void add_input_key(const std::string &key_name,
                     const std::string &description, T default_value) {
    this->add_input_key(
        BlackboardKeyInfo(key_name, description, default_value));
  }

  void add_output_key(const BlackboardKeyInfo &key_info);

  void add_output_key(const std::string &key_name);

  void add_output_key(const std::string &key_name,
                      const std::string &description);

  const std::vector<BlackboardKeyInfo> &get_input_keys() const;

  const std::vector<BlackboardKeyInfo> &get_output_keys() const;

  void declare_parameter(const BlackboardKeyInfo &parameter_info);

  void declare_parameter(const std::string &parameter_name);

  void declare_parameter(const std::string &parameter_name,
                         const std::string &description);

  template <typename T>
  void declare_parameter(const std::string &parameter_name,
                         const std::string &description, T default_value) {
    this->declare_parameter(
        BlackboardKeyInfo(parameter_name, description, default_value));
  }

  bool has_parameter(const std::string &parameter_name) const;

  template <typename T>
  T get_parameter(const std::string &parameter_name) const {
    return this->get_parameters_blackboard()->get<T>(parameter_name);
  }

  template <typename T>
  void set_parameter(const std::string &parameter_name, T value) {
    this->get_parameters_blackboard()->set<T>(parameter_name, value);
  }

  bool is_parameter_declared(const std::string &parameter_name) const;

  void copy_parameter_from(const State &source_state,
                           const std::string &source_parameter_name,
                           const std::string &target_parameter_name);

  const std::vector<BlackboardKeyInfo> &get_parameters() const;

  const StateMetadata &get_metadata() const;

  virtual std::string to_string() const;
};

} // namespace yasmin

#endif // YASMIN__STATE_HPP_