sml icon indicating copy to clipboard operation
sml copied to clipboard

Troubles using inheritances on State/Actions

Open nitrogene opened this issue 4 years ago • 0 comments

I do not understand why I have to override the OnEntry method in the following InitState class. Any idea?

Code sample

StateMachine

class StateMachine
{
public:
	auto operator()() const noexcept
	{
		using boost::sml::_;
		using boost::sml::event;
		using boost::sml::make_transition_table;
		using boost::sml::on_entry;
		using boost::sml::on_exit;
		using boost::sml::state;

		const auto entryPoint =state<class entryPoint>;

		return make_transition_table(
			*entryPoint + event<Events::StartEvent> = state<States::InitState>
			, state<States::InitState> + on_entry<_> / &States::InitState::OnEntry
		);
	}
};

State

	class State 
	{
	protected:
		const std::string m_name;

	public:
		State(const std::string& name) :m_name(name)
		{

		}

                // Uncomment and this will compile
		/*virtual*/ void OnEntry()
		{
			std::cout << m_name << "::OnEntry" << std::endl;
		}
};

InitState

	class InitState : public State
	{
	public:
		InitState():State("InitState")
		{

		}

                // Uncomment and this will compile
		/*void OnEntry() override
		{
			State::OnEntry();
		}*/
	};

main

	States::InitState initState{};

	boost::sml::sm<
		StateMachine, 
		boost::sml::process_queue<std::queue>,
		boost::sml::thread_safe<std::recursive_mutex>
	> sm(StateMachine(), initState);

	sm.process_event(Events::StartEvent{});

nitrogene avatar Dec 21 '21 15:12 nitrogene