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 <string>

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

namespace yasmin {

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

class State {

protected:
  Outcomes outcomes;

private:
  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() = default;

  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 cancel_state() {
    YASMIN_LOG_INFO("Canceling state '%s'", this->to_string().c_str());
    this->set_status(StateStatus::CANCELED);
  }

  Outcomes const &get_outcomes() const noexcept;

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

} // namespace yasmin

#endif // YASMIN__STATE_HPP_