SMACC
Loading...
Searching...
No Matches
smacc_state_reactor.h
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6
7#pragma once
8
9#include <functional>
10#include <memory>
11#include <vector>
12#include <algorithm>
14#include <boost/statechart/event.hpp>
15#include <map>
16
17namespace smacc
18{
19class ISmaccState;
20class ISMaccStateMachine;
21
22namespace state_reactors
23{
25{
26};
27} // namespace state_reactors
28
30{
31public:
33 std::function<void()> postEventFn;
34 std::vector<const std::type_info *> eventTypes;
35 std::map<const std::type_info *, std::function<void(void *)>> eventCallbacks_;
36
38
39 virtual void onInitialized();
40
41 virtual void onEntry();
42 virtual void onExit();
43
44 virtual void onEventNotified(const std::type_info *eventType);
45
46 template <typename EventType>
47 void postEvent(const EventType &ev);
48
49 template <typename EventType>
50 void postEvent();
51
52 // type based event callback
53 template <typename T, typename TClass>
54 void createEventCallback(void (TClass::*callback)(T *), TClass *object);
55
56 // type based event callback
57 template <typename T>
58 void createEventCallback(std::function<void(T *)> callback);
59
60 void update();
61
62 //must returns true when the output event is triggered
63 virtual bool triggers() = 0;
64
65 template <typename TEv>
66 void addInputEvent();
67
68 template <typename TEv>
69 void setOutputEvent();
70
71 //TDerived
73
74private:
76
77 template <typename TEvent>
78 void notifyEvent(TEvent *ev)
79 {
80 //the state machine uses this method to notify this state reactor some event happened.
81 auto tid = &(typeid(TEvent));
82 if (std::find(eventTypes.begin(), eventTypes.end(), tid) != eventTypes.end())
83 {
84 this->onEventNotified(tid);
85 this->update();
86
87 if (eventCallbacks_.count(tid))
88 {
89 eventCallbacks_[tid]((void *)ev);
90 }
91 }
92 }
93};
94
95} // namespace smacc
virtual void onEntry()
virtual void onEventNotified(const std::type_info *eventType)
std::function< void()> postEventFn
virtual bool triggers()=0
void initialize(ISmaccState *ownerState)
std::map< const std::type_info *, std::function< void(void *)> > eventCallbacks_
std::vector< const std::type_info * > eventTypes
void createEventCallback(void(TClass::*callback)(T *), TClass *object)
void notifyEvent(TEvent *ev)
virtual void onInitialized()
virtual void onExit()