I'm trying to move my previous "typed" declaration of std::function vector to work with any type.
This is the declaration code example that work from where I started:
enum STATE {OFF, ON};
class withState {
private:
STATE state;
using stateChangeHandlersFunc = std::function<void(STATE oldState, STATE newState)>;
std::vector<stateChangeHandlersFunc> stateChangeHandlers;
void _onStateChange(STATE oldState, STATE newState);
void setState(STATE /*state*/);
STATE getState();
void addStateChangeHandler(stateChangeHandlersFunc /*function*/);
public:
withState();
virtual ~withState();
};
This is the H file of what I'm trying to do: I define a template with a typename StateType which can take different ENUM types, and try to apply this templated type like this.
template<typename StateType>
class withState {
private:
protected:
StateType state;
using stateChangeHandlersFunc = std::function<void(StateType oldState, StateType newState)>;
// <error-type>
std::vector<stateChangeHandlersFunc> stateChangeHandlers;
void _onStateChange(StateType oldState, StateType newState);
void setState(StateType /*state*/);
StateType getState();
void addStateChangeHandler(stateChangeHandlersFunc /*function*/);
public:
withState();
virtual ~withState();
};
And the cpp file of my definitions:
#include <thread>
#include "withState.h"
template<typename StateType>
withState<StateType>::withState() {}
template<typename StateType>
void withState<StateType>::_onStateChange(StateType oldState, StateType newState) {
for(auto&& handler : this->stateChangeHandlers) {
std::thread(handler, oldState, newState).detach();
}
}
template<typename StateType>
void withState<StateType>::setState(StateType state) {
if (this->state != state) {
this->_onStateChange<StateType>(this->state, state);
this->state = state;
}
}
template<typename StateType>
StateType withState<StateType>::getState() {
return this->state;
}
template<typename StateType>
void withState<StateType>::addStateChangeHandler(stateChangeHandlersFunc function) {
this->stateChangeHandlers.push_back(function);
}
template<typename StateType>
withState<StateType>::~withState() {}
template class withState<STATE>;
template class withState<VMC_STATE>;
template class withState<VERRIERE_STATE>;
how can I achieve this?