How to reuse code (a Guard) defined in a submachine in the parent SM (using boost-msm)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to reuse code (a Guard) defined in a submachine in the parent SM (using boost-msm)?



I've defined a guard in a sub-state machine and now I want to use the exact logic/guard in the parent SM (using boost-msm). using the same guard in the transition_table results in compilation error: unknown type name 'GuardSS'.



I've defined the guard (and its corresponding value) in the sub-machine, but it can be moved to the parent SM if it helps in solving the problem and allows me to reuse the guard.



A sample code can be found here.



How can I reuse the code used in the guard?



Code included:


#include <iostream>
#include "myfsm.h"

int main()

std::cout << "Testing boost::msm ..." << std::endl;
MyFsm fsm;
fsm.start();

fsm.process_event(Event12());

fsm.process_event(Event_ss12());

//guard valu changes
MyFsm_::State2 &state = fsm.get_state<MyFsm_::State2&>();
state.m_guardVal = true;

fsm.process_event(Event_ss12());

fsm.process_event(Event21());//?protect this transition using the same Guard



fsm.h:


#ifndef MYFSM
#define MYFSM

#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>

struct Event12;
struct Event21;
struct Event_ss12;
struct Event_ss21;

namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;

struct MyFsm_ : msmf::state_machine_def<MyFsm_>

struct State1 : msmf::state<>
template<class Event, class Fsm> void on_entry(const Event&, Fsm&) const std::cout << "State1::on_entry()" << std::endl;
template<class Event, class Fsm> void on_exit(const Event&, Fsm&) const std::cout << "State1::on_exit()" << std::endl;
;

struct State2_ : msmf::state_machine_def<State2_>
template <class Event,class Fsm> void on_entry(Event const&, Fsm&) const std::cout << "State2::on_entry()" << std::endl;
template <class Event,class Fsm> void on_exit(Event const&, Fsm&) const std::cout << "State1::on_exit()" << std::endl;

struct SubState1 : msmf::state<>
template <class Event,class Fsm> void on_entry(Event const&, Fsm&) const std::cout << "SubState1::on_entry()" << std::endl;
template <class Event,class Fsm> void on_exit(Event const&, Fsm&) const std::cout << "SubState1::on_exit()" << std::endl;
;
struct SubState2 : msmf::state<>
template <class Event,class Fsm> void on_entry(Event const&, Fsm&) const std::cout << "SubState2::on_entry()" << std::endl;
template <class Event,class Fsm> void on_exit(Event const&, Fsm&) const std::cout << "SubState2::on_exit()" << std::endl;
;
// Guards
struct GuardSS
template <class Event, class Fsm, class SourceState, class TargetState>
bool operator()(Event const&, Fsm& fsm, SourceState&, TargetState&)

if (fsm.m_guardVal == true)
std::cout << "Transition Approved by Guard n";
return true;

std::cout << "Transition Rejected by Guard n";
return false;

;
typedef SubState1 initial_state;

struct transition_table:mpl::vector<
// Start Event Next Action Guard
msmf::Row < SubState1, Event_ss12, SubState2, msmf::none, GuardSS >,
msmf::Row < SubState2, Event_ss21, SubState1, msmf::none, msmf::none >
> ;

bool m_guardVal=false;
;
typedef msm::back::state_machine<State2_> State2;

// Set initial state
typedef State1 initial_state;

// Transition table
struct transition_table:mpl::vector<
msmf::Row < State1, Event12, State2, msmf::none, msmf::none >,
msmf::Row < State2, Event21, State1, msmf::none, GuardSS/*msmf::none*/ >
>;

template<class Event, class Fsm>
void no_transition(Event const&, Fsm&, int state)
std::cout<<"no_transiton detected from state: "<< state << std::endl;


;
// Pick a back-end
typedef msm::back::state_machine<MyFsm_> MyFsm;


#endif // MYFSM









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard