Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
EventHandlerContainer.hxx
Go to the documentation of this file.
1
37#ifndef _OPENLCB_EVENTHANDLERCONTAINER_HXX_
38#define _OPENLCB_EVENTHANDLERCONTAINER_HXX_
39
40#include <algorithm>
41#include <vector>
42#include <forward_list>
43#include <stdint.h>
44#include <endian.h>
45
46#ifndef LOGLEVEL
47//#define LOGLEVEL VERBOSE
48#endif
49
50#include "utils/Atomic.hxx"
51#include "utils/logging.h"
55
56namespace openlcb
57{
58
62protected:
65
66public:
67 virtual ~EventIterator() {}
68
74
79 virtual void init_iteration(EventReport* event) = 0;
80
82 virtual void clear_iteration() = 0;
83};
84
87template<class C> class FullContainerIterator : public EventIterator {
88public:
89 FullContainerIterator(C* container)
90 : container_(container) {
92 }
94 if (it_ == container_->end()) return nullptr;
95 EventRegistryEntry* h = &*it_;
96 ++it_;
97 return h;
98 }
100 it_ = container_->end();
101 }
103 it_ = container_->begin();
104 }
105
106private:
107 typename C::iterator it_;
108 C* container_;
109};
110
114{
115public:
117
118 // Creates a new event iterator. Caller takes ownership of object.
122
124 const EventRegistryEntry &entry, unsigned mask) OVERRIDE
125 {
126 AtomicHolder h(this);
127 handlers_.push_front(entry);
128 set_dirty();
129 }
130 void unregister_handler(EventHandler *handler, uint32_t user_arg = 0,
131 uint32_t user_arg_mask = 0) OVERRIDE
132 {
133 AtomicHolder h(this);
134 handlers_.remove_if([handler, user_arg, user_arg_mask](
135 const EventRegistryEntry &e) {
136 return e.handler == handler &&
137 ((e.user_arg & user_arg_mask) == (user_arg & user_arg_mask));
138 });
139 set_dirty();
140 }
141
142 private:
143 typedef std::forward_list<EventRegistryEntry> HandlersList;
144 HandlersList handlers_;
145};
146
150class TreeEventHandlers : public EventRegistry, private Atomic {
151public:
153
155 void register_handler(const EventRegistryEntry &entry,
156 unsigned mask) OVERRIDE;
157 void unregister_handler(EventHandler *handler, uint32_t user_arg = 0,
158 uint32_t user_arg_mask = 0) OVERRIDE;
159 void reserve(size_t count) OVERRIDE;
160
161private:
162 class Iterator;
163 friend class Iterator;
164
166 struct cmpop
167 {
168 bool operator()(const EventRegistryEntry &d, uint64_t k)
169 {
170 return d.event < k;
171 }
172 bool operator()(uint64_t k, const EventRegistryEntry &d)
173 {
174 return k < d.event;
175 }
176 bool operator()(const EventRegistryEntry &a, const EventRegistryEntry &b)
177 {
178 return a.event < b.event;
179 }
180 };
181
183 typedef std::map<uint8_t, OneMaskMap> MaskLookupMap;
187 MaskLookupMap handlers_;
188};
189
190}; /* namespace openlcb */
191
192#endif // _OPENLCB_EVENTHANDLERCONTAINER_HXX_
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
An mostly std::set<> compatible class that stores the internal data in a sorted vector.
Abstract base class for all event handlers.
Abstract class for representing iteration through a container for event handlers.
EventIterator()
Creates an EventIterator.
virtual EventRegistryEntry * next_entry()=0
Steps the iteration.
virtual void clear_iteration()=0
Stops iteration and resets iteration variables.
virtual void init_iteration(EventReport *event)=0
Starts the iteration.
Structure used in registering event handlers.
EventHandler * handler
Pointer to the handler.
uint32_t user_arg
Opaque user argument.
EventId event
Stores the event ID or beginning of range for which to register the given handler.
Global static object for registering event handlers.
void set_dirty()
Implementations must call this function from register and unregister handler to mark iterators being ...
EventIterator that produces every single entry in a given container (which can be any STL-compatible ...
void init_iteration(EventReport *) OVERRIDE
Starts the iteration.
void clear_iteration() OVERRIDE
Stops iteration and resets iteration variables.
EventRegistryEntry * next_entry() OVERRIDE
Steps the iteration.
Class representing the iteration state on the binary tree-based event handler registry.
EventRegistry implementation that keeps event handlers in a SortedListMap and filters the event handl...
void register_handler(const EventRegistryEntry &entry, unsigned mask) OVERRIDE
Adds a new event handler to the registry.
void unregister_handler(EventHandler *handler, uint32_t user_arg=0, uint32_t user_arg_mask=0) OVERRIDE
Removes all registered instances of a given event handler pointer.
void reserve(size_t count) OVERRIDE
Prepares storage for adding many event handlers.
MaskLookupMap handlers_
The registered handlers.
EventIterator * create_iterator() OVERRIDE
Creates a new event iterator. Caller takes ownership of object.
EventRegistry implementation that keeps all event handlers in a vector and forwards every single call...
EventIterator * create_iterator() OVERRIDE
Creates a new event iterator. Caller takes ownership of object.
void unregister_handler(EventHandler *handler, uint32_t user_arg=0, uint32_t user_arg_mask=0) OVERRIDE
Removes all registered instances of a given event handler pointer.
void register_handler(const EventRegistryEntry &entry, unsigned mask) OVERRIDE
Adds a new event handler to the registry.
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
Shared notification structure that is assembled for each incoming event-related message,...
Comparison operator for event registry entries.