sigmaN
sigmaN

Reputation: 197

boost.msm how to call current state method

I have to implement some logic with public interface(API).

int api_open(); 
int api_1(int a, int b); 
int api_2(int x, int y); 
...

Returning values and "meaning" of each API call depends on some state. Rule example: if you make any api call before successfull api_open() call then error should be returned. I see two states here: initial state and opened state. In initial state all api calls should return error except api_open(). In opened state api_1() and api_2() implementing some actions.

Usually i would implement this as a state pattern with public interface and states implemented in concrete types. Example: https://www.robertlarsononline.com/2017/05/11/state-pattern-using-cplusplus/

Then i thought.. if each boost.msm state is like this

//opened state 
struct opened : public msm::front::state<api_impl> { 
virtual int api_open(){} 
virtual int api_1(int a, int b){}
virtual int api_2(int x, int y){} 
};
...
typedef msm::back::state_machine<api_> api_fsm;

Then everything i need is to access current state from state machine... and call api methods.

api_impl* impl = api_fsm.get_state_by_id(*api_fsm.current_state());

But quote fom doc: current_state Returns the ids of currently active states. You will typically need it only for debugging or logging purposes.

It feels like i am trying to missuse boost.msm library.... I think i am trying to mix classical oop state pattern with msm. This is not good....

May be i should pass each API call as an event(with data, representing API call parameters) to the state machine and somehow get return values from it...how?

It feels like sending data to state machine and getting it back is not obvious. Or may be i am not getting the point?

So main question is: What is the proper way of solving my problem with boost.msm?

Upvotes: 0

Views: 337

Answers (0)

Related Questions