Skip to content

Common Base

Sergei Fedorov edited this page Nov 28, 2016 · 2 revisions

This is a draft document


There is often a situation when we want to handle some interface implementation differently based on current state of object and hide internal state dispatch from the user. To help achieve that the afsm library provides common base feature.

A state machine having tag common_base<> (or virtual_common_base<>) will publicly derive from template argument of the tag. In case of virtual_common_base - publicly and virtually. All nested states specified without qualification (struct my_state : state<my_state> {};) will also have the same tag, and nested state machines can be either specified with the same tag or template type alias inner_machine which implies the common base tag can be used.

struct common_base {
    virtual ~common_base() = default;
    virtual void do_a() = 0;
    virtual void do_b(int) = 0;
};

struct fsm_def : ::afsm::def::state_machine< fsm_def, ::afsm::def::tags::common_base<common_base> > {
    // Implicit common base tag
    struct state_a : state<state_a> {
        void do_a() override {}
        void do_b(int) override {}
    };
    // Explicit common base tag, won't compile without the tag
    struct state_b : ::afsm::def::state<state_b, ::afsm::def::tags::common_base<common_base>> {
        void do_a() override {}
        void do_b(int) override {}
    };
    // Implicit common base tag
    struct sub_machine : state_machine<sub_machine> {
        void do_a() override {}
        void do_b(int) override {}
    };
    // Explicit common base tag, won't compile without the tag
    struct sub_machine_a : ::afsm::def::state_machine<sub_machine_a, ::afsm::def::tags::common_base<common_base>> {
        void do_a() override {}
        void do_b(int) override {}
    };
    using initial_state = state_a;
        void do_a() override {}
        void do_b(int) override {}
};

TODO Obtaining current state upcasted to common base

TODO Implementing dispatch of common interface functions to current state

TODO Common base and orthogonal regions

Clone this wiki locally