Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
EventHandlerContainer.cxx
Go to the documentation of this file.
1
36
37#include <algorithm>
38
39namespace openlcb
40{
41
43 unsigned mask)
44{
45 AtomicHolder h(this);
46 LOG(VERBOSE, "%p: register %p", this, entry.handler);
47 set_dirty();
48 handlers_[mask].insert(EventRegistryEntry(entry));
49}
50
52 EventHandler *handler, uint32_t user_arg, uint32_t user_arg_mask)
53{
54 AtomicHolder h(this);
55 set_dirty();
56 LOG(VERBOSE, "%p: unregister %p", this, handler);
57 for (auto r = handlers_.begin(); r != handlers_.end(); ++r)
58 {
59 auto begin_it = r->second.begin();
60 auto end_it = r->second.end();
61 auto erase_it = std::remove_if(begin_it, end_it,
62 [handler, user_arg, user_arg_mask](const EventRegistryEntry &e) {
63 return e.handler == handler &&
64 ((e.user_arg & user_arg_mask) ==
65 (user_arg & user_arg_mask));
66 });
67 if (erase_it != end_it)
68 {
69 r->second.erase(erase_it, end_it);
70 }
71 }
72}
73
75{
76 AtomicHolder h(this);
77 handlers_[0].reserve(handlers_[0].size() + count);
78}
79
83{
84public:
86 : parent_(parent)
87 {
88 AtomicHolder h(parent_);
89 parent->handlers_[0];
91 }
92
94 {
95 AtomicHolder h(parent_);
96 while (maskIterator_ != parent_->handlers_.end())
97 {
98 if (it_ == end_)
99 {
100 maskIterator_++;
101 if (maskIterator_ != parent_->handlers_.end())
102 {
103 setup_current_mask();
104 }
105 continue;
106 }
107 else
108 {
109 EventRegistryEntry *e = &*it_;
110 it_++;
111 return e;
112 }
113 }
114 return nullptr;
115 }
116
118 {
119 AtomicHolder h(parent_);
120 maskIterator_ = parent_->handlers_.end();
121 }
123 {
124 AtomicHolder h(parent_);
125 currentReport_ = r;
126 maskIterator_ = parent_->handlers_.begin();
127 setup_current_mask();
128 }
129
130private:
131 void setup_current_mask()
132 {
133 if (maskIterator_->first == 64)
134 {
135 // 64 bits -> all events go to everyone.
136 it_ = maskIterator_->second.begin();
137 end_ = maskIterator_->second.end();
138 return;
139 }
140 unsigned mask_log = maskIterator_->first;
141 uint64_t current_mask = (1ULL << mask_log) - 1;
142 uint64_t eventid_key = currentReport_->event & (~current_mask);
143 it_ = maskIterator_->second.lower_bound(eventid_key);
144 eventid_key = currentReport_->event + currentReport_->mask;
145 end_ = maskIterator_->second.upper_bound(eventid_key);
146 }
147 TreeEventHandlers *parent_;
148 EventReport *currentReport_;
149 MaskLookupMap::iterator maskIterator_;
152};
153
158
159TreeEventHandlers::TreeEventHandlers()
160{
161}
162
163} // namespace openlcb
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
container_type::iterator iterator
Iterator type.
Abstract base class for all event handlers.
Abstract class for representing iteration through a container for event handlers.
Structure used in registering event handlers.
EventHandler * handler
Pointer to the handler.
uint32_t user_arg
Opaque user argument.
void set_dirty()
Implementations must call this function from register and unregister handler to mark iterators being ...
Class representing the iteration state on the binary tree-based event handler registry.
EventRegistryEntry * next_entry() OVERRIDE
Steps the iteration.
void clear_iteration() OVERRIDE
Stops iteration and resets iteration variables.
void init_iteration(EventReport *r) OVERRIDE
Starts the iteration.
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.
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int VERBOSE
Loglevel that is usually not printed, reporting debugging information.
Definition logging.h:59
#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,...
EventId mask
Specifies the mask in case the request is for an event range.
EventId event
The event ID from the incoming message.