Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
NodeHandlerMap.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_NODEHANDLERMAP_HXX_
37#define _UTILS_NODEHANDLERMAP_HXX_
38
39#include <stdint.h>
40#include <utility>
41
42#include "utils/StlMap.hxx"
43#include "utils/LinearMap.hxx"
44#include "utils/SysMap.hxx"
45
46
47#if UINTPTR_MAX > UINT32_MAX
48#define NODEHANDLER_USE_PAIR
49#endif
50
51
62{
63private:
64#ifdef NODEHANDLER_USE_PAIR
66 typedef std::pair<void*, uint32_t> key_type;
67#else
69 typedef uint64_t key_type;
70#endif
72 typedef void* value_type;
75
76public:
78 {
79 }
80
82 NodeHandlerMapBase(size_t entries) : entries_(entries)
83 {
84 }
85
91 void insert(void* node, uint32_t id, void* value)
92 {
93 entries_[make_key(node, id)] = value;
94 }
95
102 void erase(void *node, uint32_t id, void *value)
103 {
104 auto it = entries_.find(make_key(node, id));
105 if (it == entries_.end()) return;
106 if (it->second == value) {
107 entries_.erase(it);
108 }
109 }
110
116 void* lookup(void* node, uint32_t id)
117 {
118 auto it = entries_.find(make_key(node, id));
119 if (it == entries_.end())
120 {
121 it = entries_.find(make_key(nullptr, id));
122 }
123 if (it == entries_.end())
124 {
125 return nullptr;
126 }
127 else
128 {
129 return it->second;
130 }
131 }
132
134 typedef typename map_type::Iterator iterator;
137 {
138 return entries_.begin();
139 }
140
143 {
144 return entries_.end();
145 }
146
149 static pair<void *, uint32_t> read_key(key_type key)
150 {
151#ifdef NODEHANDLER_USE_PAIR
152 return key;
153#else
154 uint32_t id = key & 0xFFFFFFFFU;
155 uint32_t n = key >> 32;
156 return std::make_pair(reinterpret_cast<void *>(n), id);
157#endif
158 }
159
160private:
162 key_type make_key(void* node, uint32_t id)
163 {
164#ifdef NODEHANDLER_USE_PAIR
165 return std::make_pair(node, id);
166#else
167 uint64_t key = reinterpret_cast<uint32_t>(node);
168 key <<= 32;
169 key |= id;
170 return key;
171#endif
172 }
173
176};
177
180template <class Node, class Handler>
182{
183public:
185 {
186 }
187
190 TypedNodeHandlerMap(size_t entries) : NodeHandlerMapBase(entries)
191 {
192 }
193
199 void insert(Node* node, uint32_t id, Handler* handler)
200 {
201 NodeHandlerMapBase::insert(node, id, handler);
202 }
203
211 void erase(Node* node, uint32_t id, Handler* handler)
212 {
213 NodeHandlerMapBase::erase(node, id, handler);
214 }
215
221 Handler* lookup(Node* node, uint32_t id)
222 {
223 return static_cast<Handler*>(NodeHandlerMapBase::lookup(node, id));
224 }
225
227 class iterator {
228 public:
232
234 void operator++() {
235 ++impl_;
236 }
237
239 bool operator!=(const iterator& o) {
240 return impl_ != o.impl_;
241 }
242
244 std::pair<std::pair<Node*, uint32_t>, Handler*> operator*() {
245 auto p = NodeHandlerMapBase::read_key(impl_->first);
246 return std::make_pair(std::make_pair((Node*) p.first, p.second),
247 (Handler*)impl_->second);
248 }
249
250 private:
253 };
254
259
264};
265
266#endif // _UTILS_NODEHANDLERMAP_HXX_
A map that allows registration and lookup or per-node handler of a particular message ID.
key_type make_key(void *node, uint32_t id)
Combines the node pointer and the message ID into a lookup key.
uint64_t key_type
Compacted payload type.
void insert(void *node, uint32_t id, void *value)
Inserts a handler into the map.
static pair< void *, uint32_t > read_key(key_type key)
Decodes a compact key into a pair.
NodeHandlerMapBase(size_t entries)
Creates a map with.
void * value_type
Generic handler type that we'll keep.
StlMap< key_type, value_type > map_type
Type of the storage object.
void erase(void *node, uint32_t id, void *value)
Removes a handlerfrom this map.
map_type::Iterator iterator
Iterator type.
void * lookup(void *node, uint32_t id)
Finds a handler for a particular node and particular messageID.
map_type entries_
The actual storage object.
Node information.
Definition Devtab.hxx:549
Though at the surface, this may seem like an unnecessary abstraction of std::map, it has the purpose ...
Definition StlMap.hxx:50
std::map< key_type, value_type >::iterator Iterator
Short hand for the iterator type of a given instance.
Definition StlMap.hxx:87
Iterator end()
Get an iterator index pointing one past the last element in mapping.
Definition StlMap.hxx:152
size_t erase(Key key)
Remove an element from the tree.
Definition StlMap.hxx:102
Iterator begin()
Get an iterator index pointing one past the last element in mapping.
Definition StlMap.hxx:160
Iterator find(const Key &key)
Find an element matching the given key.
Definition StlMap.hxx:144
Type-safe iterator for NodeHandlerMap.
NodeHandlerMapBase::iterator impl_
untyped iterator
bool operator!=(const iterator &o)
iterator(NodeHandlerMapBase::iterator i)
std::pair< std::pair< Node *, uint32_t >, Handler * > operator*()
Dereference.
A type-safe map that allows registration and lookup or per-node handler of a particular message ID.
void insert(Node *node, uint32_t id, Handler *handler)
Inserts a handler into the map.
TypedNodeHandlerMap(size_t entries)
void erase(Node *node, uint32_t id, Handler *handler)
Removes a handler from the map.
Handler * lookup(Node *node, uint32_t id)
Finds a handler for a particular node and particular messageID.