FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
events.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef EVENTS_HH
22#define EVENTS_HH
23
24#include <stddef.h>
25#include "queue.h"
26#include "linked_list.h"
27
81namespace events
82{
92 namespace Type
93 {
98 const uint8_t NO_EVENT = 0;
99
105 const uint8_t WDT_TIMER = 1;
106
112 const uint8_t RTT_TIMER = 2;
113
123 const uint8_t USER_EVENT = 128;
124 };
125
143 template<typename T> class Event
144 {
145 public:
147 Event(const Event&) = default;
148 Event& operator=(const Event&) = default;
150
154 using TYPE = T;
155
162 explicit Event(uint8_t type = Type::NO_EVENT, T value = T{}) INLINE : type_{type}, value_{value} {}
163
167 uint8_t type() const INLINE
168 {
169 return type_;
170 }
171
177 T value() const INLINE
178 {
179 return value_;
180 }
181
182 private:
183 uint8_t type_;
184 T value_;
185 };
186
188 template<> class Event<void>
189 {
190 public:
191 Event(const Event&) = default;
192 Event& operator=(const Event&) = default;
193
194 using TYPE = void;
195
196 explicit Event(uint8_t type = Type::NO_EVENT) INLINE : type_{type} {}
197 uint8_t type() const INLINE
198 {
199 return type_;
200 }
201
202 private:
203 uint8_t type_;
204 };
206
208 template<typename T> struct Event_trait
209 {
210 static constexpr const bool IS_EVENT = false;
211 };
212 // Note: the following line compiled with GCC 7.4 but does not with 9.2 and 10.2
213 // template<> template<typename T> struct Event_trait<Event<T>>
214 // Replaced with this line to make GCC 9.2 happy
215 template<typename T> struct Event_trait<Event<T>>
216 {
217 static constexpr const bool IS_EVENT = true;
218 };
220
222 template<typename EVENT> class EventHandler;
224
241 template<typename EVENT> class Dispatcher : public containers::LinkedList<EventHandler<EVENT>>
242 {
243 static_assert(Event_trait<EVENT>::IS_EVENT, "EVENT type must be an events::Event<T>");
244
245 public:
246 Dispatcher() = default;
247 Dispatcher(const Dispatcher&) = delete;
248 Dispatcher& operator=(const Dispatcher&) = delete;
249
258 void dispatch(const EVENT& event)
259 {
260 this->traverse(HandlerCaller(event));
261 }
262
263 private:
264 class HandlerCaller
265 {
266 public:
267 explicit HandlerCaller(const EVENT& event) INLINE : event_{event} {}
268 bool operator()(EventHandler<EVENT>& handler) INLINE
269 {
270 if (handler.type() == event_.type()) handler.on_event(event_);
271 return false;
272 }
273
274 private:
275 const EVENT event_;
276 };
277 };
278
287 template<typename EVENT> class EventHandler : public containers::Link<EventHandler<EVENT>>
288 {
289 static_assert(Event_trait<EVENT>::IS_EVENT, "EVENT type must be an events::Event<T>");
290
291 public:
295 uint8_t type() const INLINE
296 {
297 return type_;
298 }
299
300 protected:
302 EventHandler(const EventHandler&) = default;
303 EventHandler& operator=(const EventHandler&) = default;
305
311 virtual void on_event(const EVENT& event) = 0;
312
317 explicit EventHandler(uint8_t type = Type::NO_EVENT) INLINE : type_{type} {}
318
319 private:
320 uint8_t type_;
321 friend class Dispatcher<EVENT>;
322 };
323};
324#endif /* EVENTS_HH */
Linked list of type T_ items.
Definition: linked_list.h:81
void traverse(F func)
Traverse all items of this list and execute f functor.
Definition: linked_list.h:132
Utility to dispatch an event to a list of EventHandlers that are registered for its type.
Definition: events.h:242
void dispatch(const EVENT &event)
Dispatch the given event to the right EventHandler, based on the event type.
Definition: events.h:258
Abstract event handler, used by Dispatcher to get called back when an event of the expected type is d...
Definition: events.h:288
EventHandler(uint8_t type=Type::NO_EVENT) INLINE
Create an Event Handler for given type of event.
Definition: events.h:317
uint8_t type() const INLINE
The type of event that this handler accepts and can act upon.
Definition: events.h:295
virtual void on_event(const EVENT &event)=0
This pure virtual method is called by Dispatcher::dispatch() when event.type() matches the type suppo...
A standard Event as managed by FastArduino event API.
Definition: events.h:144
T TYPE
The type of additional event value, as defined in template paraneter T.
Definition: events.h:154
T value() const INLINE
The associated value of this event.
Definition: events.h:177
uint8_t type() const INLINE
The type of this event.
Definition: events.h:167
Event(uint8_t type=Type::NO_EVENT, T value=T{}) INLINE
Create a new event with the given type and the given value.
Definition: events.h:162
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
Utility API to handle linked list containers.
const uint8_t NO_EVENT
Special event type attached to no event at all.
Definition: events.h:98
const uint8_t USER_EVENT
The first ordinal event type that you may use for your own custom events.
Definition: events.h:123
const uint8_t WDT_TIMER
Type of events generated by watchdog::Watchdog for each watchdog timeout interrupt.
Definition: events.h:105
const uint8_t RTT_TIMER
Type of events generated by timer::RTTEventCallback whenever elapsed RTT::millis() reaches a multiple...
Definition: events.h:112
Defines all API to handle events within FastArduino programs.
Definition: events.h:82
Utility API to handle ring-buffer queue containers.