我正在实现一个用于运行有限状态机的非常基本的基类。这是我所做的...

/**
 * @class FSM
 * @brief This class is a base class for an object that wants to run a Finite State Machine.
 * That object, let's say States, would need to inherit from FSM<States>,
 * and its methods would then be available as state actions of the state machine.
 */
template<typename States>
class FSM
{
    public :
        /**
         * @typedef StateAction is an alias for a pointer to a method of States, only callable that can be a state of the FSM
         */
        typedef void(States::*StateAction)(void);

        /**
         * @fn FSM(States* states, StateAction initial_state_action = nullptr)
         * @brief Constructs a FSM object whose actions are of type StateAction
         */
        FSM(States* states, StateAction initial_state_action = nullptr)
        :
            m_states(states),
            m_state_action(initial_state_action),
            m_is_entering_state(true)
        {}

        /**
         * @fn bool update()
         * @brief Performs an iteration of the FSM
         * @returns true if and only if the FSM has a state to run
         */
        bool update()
        {
            if(m_states && m_state_action)
            {
                auto previous_action = m_state_action;
                (m_states->*m_state_action)();
                m_is_entering_state = (m_state_action != previous_action);
            }
            return m_state_action != nullptr;
        }

    protected :
        /**
         * @fn void setState(StateAction state_action)
         * @brief Change the state of the FSM
         * @param[in] state_action Method of States that implements the new state behavior (nullptr stops the FSM)
         */
        void setState(StateAction state_action)
        {
            m_state_action = state_action;
        }

        /**
         * @fn bool isEnteringState() const
         * @brief Tells whether the current state has just be entered or is looping, waiting for a transition
         * @returns] true if and only if the current state hasn't yet been executed since it has been changed
         */
        bool isEnteringState() const
        {
            return m_is_entering_state;
        }

    private :
        States*     m_states;            //!< Pointer to the child class that implements the States of the FSM
        StateAction m_state_action;      //!< Pointer to the method of States that implements the current state behavior
        bool        m_is_entering_state; //!< Tells whether the current state has just be entered or is looping, waiting for a transition
};

...以及打算如何使用:
class States : public FSM<States>
{
    public :
        States()
        :
            FSM<States>(this, &States::state1)
        {}

        void state1()
        {
            // Actions to perform when entering the state
            if(isEnteringState())    cout << "Entering ";

            // Actions to perform constantly (regulate a system, detect events, etc...)
            cout << __func__ << endl;

            // Transitions to other states
            bool is_event_detected = true;
            if(is_event_detected)    setState(&States::state2);
        }

        void state2()
        {
            if(isEnteringState())    cout << "Entering ";

            cout << __func__ << endl;

            bool is_exit_detected = true;
            if(is_exit_detected)    setState(nullptr);
        }
};

int main()
{
    States fsm;
    bool is_fsm_running = true;
    while(is_fsm_running)
    {
        is_fsm_running = fsm.update();
    }
    return EXIT_SUCCESS;
}

在FSM中存储m_state(指向实现状态的派生对象的指针)的唯一目的是在update()中调用m_state_action,后者是指向派生对象的方法的指针。相反,我想知道是否将带有static_cast的(类型为FSM *的)强制转换为States *是一个好习惯?
(static_cast<States*>(this)->*m_state_action)();

行为是否定义明确? (我知道它适用于mingw 7.30)

此外,作为附带的问题,用一个属于其子类的类来模范FSM看起来有点怪异(类国家:公共(public)FSM)...我能做些不同的事情吗?

最佳答案

是的,在两种情况下是合法的:

  • States必须从FSM<States>公开和非虚拟继承。

    您的设计已经要求使用这种使用模式,但是最好添加static_assert来强制执行此要求。
  • 该对象实际上是States的实例。

    如果不是不可能的话,很难阻止struct BogusStates : FSM<TrueStates> {};
    通过给它一个FSM<States>构造函数,您至少可以阻止任何人直接实例化protected
  • 关于c++ - 从父类方法向 child 转换 “this”是一种好习惯吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57146997/

    10-11 06:24