115 lines
3.4 KiB
C++
115 lines
3.4 KiB
C++
#ifndef I_STATE_MACHINE_MANAGER_H
|
|
#define I_STATE_MACHINE_MANAGER_H
|
|
#include "VReturnCode.h"
|
|
#include <memory>
|
|
/**
|
|
* @brief
|
|
* Use for sending message to state.
|
|
*/
|
|
/**
|
|
* @brief
|
|
* virtural class, which will be used to send all kind of message classes.
|
|
*/
|
|
class VMessageObject
|
|
{
|
|
public:
|
|
VMessageObject() = default;
|
|
virtual ~VMessageObject() = default;
|
|
};
|
|
class VStateMachineMessage
|
|
{
|
|
public:
|
|
VStateMachineMessage() = default;
|
|
virtual ~VStateMachineMessage() = default;
|
|
virtual int GetMessageName() const = 0;
|
|
virtual const std::shared_ptr<VMessageObject> &GetMessageObj(void) const = 0;
|
|
};
|
|
/**
|
|
* @brief
|
|
* Use for users to define specific state in their code.
|
|
*/
|
|
class State
|
|
{
|
|
public:
|
|
/**
|
|
* @Description Construct a new State:: State object.
|
|
*
|
|
* @param name - State name.[in]
|
|
*/
|
|
explicit State(const std::string &name) : mStateName(name) {}
|
|
|
|
/**
|
|
* @Description Destroy the State:: State object.
|
|
*
|
|
*/
|
|
virtual ~State() = default;
|
|
|
|
public:
|
|
virtual void GoInState() = 0;
|
|
virtual void GoOutState() = 0;
|
|
virtual bool ExecuteStateMsg(VStateMachineMessage *msg) = 0;
|
|
|
|
/**
|
|
* @Description : Obtains state name.
|
|
*
|
|
* @return std::string
|
|
*/
|
|
std::string GetStateName() { return mStateName; }
|
|
|
|
private:
|
|
std::string mStateName;
|
|
};
|
|
/**
|
|
* @brief
|
|
* A virtual state machine class, user must create the real state machine by IStateMachineManager.
|
|
*/
|
|
class VStateMachine
|
|
{
|
|
public:
|
|
VStateMachine() = default;
|
|
virtual ~VStateMachine() = default;
|
|
virtual bool InitialStateMachine() { return false; }
|
|
virtual void StatePlus(State *state, State *upper) {}
|
|
virtual void SetFirstState(State *firstState) {}
|
|
virtual void StartStateMachine() {}
|
|
virtual void SendMessage(int msgName) {}
|
|
virtual void StopHandlerThread() {}
|
|
virtual void SendMessage(int msgName, const std::shared_ptr<VMessageObject> &messageObj) {}
|
|
virtual void MessageExecutedLater(int msgName, const std::shared_ptr<VMessageObject> &messageObj, int64_t delayTimeMs) {}
|
|
virtual void SwitchState(State *targetState) {}
|
|
virtual void StopTimer(int timerName) {}
|
|
virtual void DelayMessage(VStateMachineMessage *msg) {}
|
|
};
|
|
class IStateMachineManager
|
|
{
|
|
public:
|
|
IStateMachineManager() = default;
|
|
virtual ~IStateMachineManager() = default;
|
|
/**
|
|
* @brief Get the Instance object
|
|
* Return reference for runing faster. Usage : IStateMachineManager::GetInstance()->Init();
|
|
* Don't use IStateMachineManager like this:
|
|
* std::shared_ptr<IStateMachineManager> &log = IStateMachineManager::GetInstance();
|
|
* or std::shared_ptr<IStateMachineManager> log = IStateMachineManager::GetInstance();
|
|
* @param impl
|
|
* @return std::shared_ptr<IStateMachineManager>&
|
|
*/
|
|
static std::shared_ptr<IStateMachineManager> &GetInstance(std::shared_ptr<IStateMachineManager> *impl = nullptr);
|
|
/**
|
|
* @brief Create a State Machine object
|
|
* Create a state machine for user.
|
|
* @return std::shared_prt<VStateMachine> Return the real state machine.
|
|
*/
|
|
virtual RETURN_CODE CreateStateMachine(std::shared_ptr<VStateMachine> &stateMachine)
|
|
{
|
|
return VReturnCode::NewCode(VReturnCodeDefine::NOT_OK_VIRTUAL_FUNCTION);
|
|
}
|
|
};
|
|
/**
|
|
* @brief Create a State Machine Manager object
|
|
* Create the real module object, which should be called in main thread.
|
|
* @return true
|
|
* @return false
|
|
*/
|
|
bool CreateStateMachineManager(void);
|
|
#endif |