我正在实现一个非常基本的基类来运行有限状态机。这就是我所做的...
/**
* @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;
}
将m_states(指向实现状态的派生对象的指针)存储在FSM中的唯一目的是在update()中调用m_state_action,它是指向派生对象的方法的指针。相反,我想知道使用 static_cast 将 this (属于 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++ - 将 "this"从父类方法转换到子类方法是一个好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146997/