I am trying to make a simple templated FSM library that would allow: machine.react(Event1{}); which would call the implementation SomeState.react(std::shared_ptr<StateMachine> machine, Event1 event) without the need to know all possible events in advance, without the need to implement all and every possible event for all Derived class.
As I call the functions from current_state_ stored as State type, when calling react (which is not virtual), it will call the Base implementation and not the Derived (State1) class implementation.
#include <iostream>
#include <memory>
#include <tuple>
#include <typeinfo>
#include <vector>
template <typename ContextT, typename... StatesT>
class MachineT : public std::enable_shared_from_this<MachineT<ContextT, StatesT...>> {
using M_ = MachineT<ContextT, StatesT...>;
public:
using Context = ContextT;
class State {
public:
template <typename EVENT>
void react(std::shared_ptr<M_> machine, EVENT event) {
std::cout << "State::react" << std::endl;
};
virtual void update(std::shared_ptr<M_> machine) = 0;
};
MachineT(std::shared_ptr<ContextT> context)
: context_(context),
states_{std::make_shared<StatesT>()...} {
using FirstEntityType = std::tuple_element_t<0, std::tuple<StatesT...>>;
set<FirstEntityType>();
}
~MachineT() {}
template <typename T>
std::shared_ptr<T> get() {
return std::get<std::shared_ptr<T>>(states_);
}
template <typename T>
void set() {
current_state_ = std::get<std::shared_ptr<T>>(states_);
}
std::shared_ptr<M_> shared() {
return this->shared_from_this();
}
void update(){
current_state_->update(shared());
};
template <typename EVENT>
void react(EVENT event){
current_state_->react(shared(), event);
};
private:
std::shared_ptr<ContextT> context_;
std::tuple<std::shared_ptr<StatesT>...> states_;
std::shared_ptr<State> current_state_;
};
struct State1;
struct State2;
struct Context {};
struct Event1 {};
using StateMachine = MachineT<Context, State1, State2>;
struct State1 : public StateMachine::State {
int value = 10;
void print() { std::cout << value << std::endl; }
void react(std::shared_ptr<StateMachine> machine, Event1 event) {
std::cout << "State1::react()" << std::endl;
}
void update(std::shared_ptr<StateMachine> machine) {
std::cout << "State1::update()" << std::endl;
}
};
struct State2 : public StateMachine::State {
int value = 20;
void print() { std::cout << value << std::endl; }
void react(std::shared_ptr<StateMachine> machine, Event1 event) {
std::cout << "State2::react()" << std::endl;
}
void update(std::shared_ptr<StateMachine> machine) {
std::cout << "State2::update()" << std::endl;
}
};
int main() {
std::shared_ptr<Context> context;
context.reset(new Context());
std::shared_ptr<StateMachine> machine;
machine.reset(new StateMachine(context));
machine->set<State1>();
machine->update();
machine->react(Event1{});
machine->set<State2>();
machine->update();
machine->react(Event1{});
}
Full Example: http://cpp.sh/7d37rq
Current output:
State1::update()
State::react()
State2::update()
State::react()
Expected output:
State1::update()
State1::react()
State2::update()
State2::react()
You'll need std::variant for that and store the current state as:
std::variant<std::shared_ptr<StatesT>...> current_state_;
With a trait to detect if a given state reacts to some event type:
template <typename State, typename Event, typename = void>
struct can_react : std::false_type {};
template <typename State, typename Event>
struct can_react<State, Event,
decltype(void(std::declval<State>()->react(nullptr, std::declval<Event>())))>
: std::true_type {};
Then, you can send an event to the state if it can handle it and discard otherwise:
template <typename Event>
void react(Event event) {
std::visit([&](auto state) {
if constexpr (can_react<decltype(state), Event>{}) {
state->react(shared(), event);
}
}, current_state_);
}
Similarly, update becomes:
void update() {
std::visit([this](auto state) {
state->update(shared());
}, current_state_);
}
DEMO
Full code:
template <typename ContextT, typename... StatesT>
class MachineT : public std::enable_shared_from_this<MachineT<ContextT, StatesT...>> {
using M_ = MachineT<ContextT, StatesT...>;
public:
using Context = ContextT;
MachineT(std::shared_ptr<ContextT> context)
: context_(context),
states_{std::make_shared<StatesT>()...} {
using FirstEntityType = std::tuple_element_t<0, std::tuple<StatesT...>>;
set<FirstEntityType>();
}
~MachineT() {}
template <typename T>
std::shared_ptr<T> get() {
return std::get<std::shared_ptr<T>>(states_);
}
template <typename T>
void set() {
current_state_ = std::get<std::shared_ptr<T>>(states_);
}
std::shared_ptr<M_> shared() {
return this->shared_from_this();
}
void update() {
std::visit([this](auto state) { state->update(shared()); }, current_state_);
}
template <typename State, typename Event, typename = void>
struct can_react : std::false_type {};
template <typename State, typename Event>
struct can_react<State, Event,
decltype(void(std::declval<State>()->react(nullptr, std::declval<Event>())))>
: std::true_type {};
template <typename Event>
void react(Event event) {
std::visit([&](auto state) {
if constexpr (can_react<decltype(state), Event>{}) {
state->react(shared(), event);
}
}, current_state_);
}
private:
std::shared_ptr<ContextT> context_;
std::tuple<std::shared_ptr<StatesT>...> states_;
std::variant<std::shared_ptr<StatesT>...> current_state_;
};
States and events:
struct State1;
struct State2;
struct Context {};
struct Event1 {};
struct Event2 {};
using StateMachine = MachineT<Context, State1, State2>;
struct State1 {
void react(std::shared_ptr<StateMachine> machine, Event1 event) {
std::cout << "State1::react(Event1)" << std::endl;
}
void update(std::shared_ptr<StateMachine> machine) {
std::cout << "State1::update()" << std::endl;
}
};
struct State2 {
void react(std::shared_ptr<StateMachine> machine, Event2 event) {
std::cout << "State2::react(Event2)" << std::endl;
}
void update(std::shared_ptr<StateMachine> machine) {
std::cout << "State2::update()" << std::endl;
}
};
Tests:
int main() {
std::shared_ptr<Context> context;
context.reset(new Context());
std::shared_ptr<StateMachine> machine;
machine.reset(new StateMachine(context));
machine->set<State1>();
machine->update();
machine->react(Event1{});
machine->set<State2>();
machine->update();
machine->react(Event2{});
}
Output:
State1::update()
State1::react(Event1)
State2::update()
State2::react(Event2)
Using c++14 it's possible using boost::variant:
template <typename Event>
struct Visitor {
MachineT* machine;
Event* event;
template <typename State>
std::enable_if_t<can_react<State, Event>{}> operator()(State state) const {
state->react(machine->shared(), *event);
}
template <typename State>
std::enable_if_t<!can_react<State, Event>{}> operator()(State) const {}
};
template <typename Event>
void react(Event event) {
boost::apply_visitor(Visitor<Event>{ this, &event }, current_state_);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With