Program Listing for File state_entry_action.hpp

Return to documentation for file (rcppsw/patterns/fsm/state_entry_action.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <cassert>
#include "rcppsw/rcppsw.hpp"
#include "rcppsw/patterns/fsm/event.hpp"
#include "rcppsw/patterns/fsm/state_entry.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::patterns::fsm {
class base_fsm;

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
template <class SM, void (SM::*Func)(void)>
class state_entry_action0 : public state_entry {
 public:
  ~state_entry_action0(void) override = default;
  void invoke_entry_action(base_fsm* sm,
                           RCPPSW_UNUSED const event_data*) const override {
    auto* derived_fsm = static_cast<SM*>(sm);
    (derived_fsm->*Func)();
  }
};

template <class SM, class Event, void (SM::*Func)(const Event*)>
class state_entry_action1 : public state_entry {
 public:
  ~state_entry_action1(void) override = default;

  void invoke_entry_action(base_fsm* sm, const event_data* data) const override {
    auto* derived_fsm = static_cast<SM*>(sm);
    const Event* derived_event = NULL;
    if (nullptr != data) {
      derived_event = dynamic_cast<const Event*>(data);
      assert(derived_event);
    }
    (derived_fsm->*Func)(derived_event);
  }
};

} /* namespace rcppsw::patterns::fsm */