-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.h
87 lines (77 loc) · 2.75 KB
/
State.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef STATE_H
#define STATE_H
#include "commons/components_commons.h"
#include "DeviceAware.h"
#include "events/EventHandler.h"
/**
* A class that defines State's base behavior, that is event handling
* and entering/exiting from that state. Extend State<D, E> to create
* a custom state for your device
*/
class AbstractState {
friend class GenericDevice;
template<typename D, typename E> friend class State;
protected:
Logger logger {nullptr, LogLevel::OFF};
virtual bool onEnterState() = 0;
virtual void onExitState() = 0;
virtual ~AbstractState() {};
private:
AbstractState(const char *stateName, LogLevel logLevel);
virtual bool canHandleEvent(const Event &event) = 0;
virtual bool handleGenericEvent(const Event &event) = 0;
virtual bool onEnterStateWithGenericEvent(const Event &event) = 0;
};
/*
* This is the base class that you should extend to create a device's state
*
* The behavior of the state is given by the following methods:
*
* bool handleEvent(const E &event)
* bool onEnterState()
* bool onEnterState(const E &event)
* void onExitState()
*
* You can just customize the behavior you are interested in, e.g.
* if you are not interested in what the state does when entering
* with an event, you can leave the default method implementation
*/
template<typename D, typename E> class State : public AbstractState, public DeviceAware<D>, public EventHandler<E> {
friend class GenericDevice;
protected:
State(D &device, const char *stateName, LogLevel logLevel = COMPONENTS_DEFAULT_LOG_LEVEL);
virtual bool handleEvent(const E &event) override {return false;}
virtual bool onEnterState() {return false;}
virtual bool onEnterState(const E &event) {return false;}
virtual void onExitState() {}
private:
bool canHandleEvent(const Event &event) override;
bool handleGenericEvent(const Event &event) override;
bool onEnterStateWithGenericEvent(const Event &event) override;
};
/*
*******************
* Implementations
*******************
*/
inline AbstractState::AbstractState(const char *stateName, LogLevel logLevel) {
logger.setName(stateName);
logger.setLogLevel(logLevel);
}
template<typename D, typename E>
inline State<D, E>::State(D &device, const char *stateName, LogLevel logLevel) :
AbstractState(stateName, logLevel), DeviceAware<D>::DeviceAware(device) {
}
template<typename D, typename E>
inline bool State<D, E>::canHandleEvent(const Event &event) {
return EventHandler<E>::EventHandler::canHandleEvent(event);
}
template<typename D, typename E>
inline bool State<D, E>::handleGenericEvent(const Event &event) {
return handleEvent(static_cast<const E&>(event));
}
template<typename D, typename E>
inline bool State<D, E>::onEnterStateWithGenericEvent(const Event &event) {
return onEnterState(static_cast<const E&>(event));
}
#endif