Embedded Template Library 1.0
Loading...
Searching...
No Matches
message_router_registry.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2021 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_MESSAGE_ROUTER_REGISTRY_INCLUDED
30#define ETL_MESSAGE_ROUTER_REGISTRY_INCLUDED
31
32#include "platform.h"
33#include "error_handler.h"
34#include "exception.h"
35#include "file_error_numbers.h"
36#include "flat_multimap.h"
37#include "iterator.h"
38#include "memory.h"
39#include "message_router.h"
40
41#include <stdint.h>
42
43namespace etl
44{
45 //***************************************************************************
47 //***************************************************************************
48 class message_router_registry_exception : public etl::exception
49 {
50 public:
51
52 message_router_registry_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
53 : etl::exception(reason_, file_name_, line_number_)
54 {
55 }
56 };
57
58 //***************************************************************************
60 //***************************************************************************
61 class message_router_registry_full : public etl::message_router_registry_exception
62 {
63 public:
64
65 message_router_registry_full(string_type file_name_, numeric_type line_number_)
66 : message_router_registry_exception(ETL_ERROR_TEXT("message router registry:full", ETL_MESSAGE_ROUTER_REGISTRY_FILE_ID"A"), file_name_,
67 line_number_)
68 {
69 }
70 };
71
72 //***************************************************************************
74 //***************************************************************************
75 class imessage_router_registry
76 {
77 private:
78
80
81 public:
82
83 class const_iterator;
84
85 //********************************************
87 //********************************************
88 class iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, etl::imessage_router*>
89 {
90 public:
91
92 friend class imessage_router_registry;
93 friend class const_iterator;
94
95 //********************************************
96 iterator() {}
97
98 //********************************************
99 iterator(const iterator& other)
100 : itr(other.itr)
101 {
102 }
103
104 //********************************************
105 iterator& operator=(const iterator& other)
106 {
107 itr = other.itr;
108 return *this;
109 }
110
111 //********************************************
112 etl::imessage_router& operator*()
113 {
114 return *(itr->second);
115 }
116
117 //********************************************
118 const etl::imessage_router& operator*() const
119 {
120 return *(itr->second);
121 }
122
123 //********************************************
124 etl::imessage_router* operator->()
125 {
126 return itr->second;
127 }
128
129 //********************************************
130 const etl::imessage_router* operator->() const
131 {
132 return itr->second;
133 }
134
135 //********************************************
136 iterator& operator++()
137 {
138 ++itr;
139 return *this;
140 }
141
142 //********************************************
143 iterator operator++(int)
144 {
145 iterator temp(*this);
146 ++itr;
147 return temp;
148 }
149
150 //********************************************
151 friend bool operator==(const iterator& lhs, const iterator& rhs)
152 {
153 return lhs.itr == rhs.itr;
154 }
155
156 //********************************************
157 friend bool operator!=(const iterator& lhs, const iterator& rhs)
158 {
159 return !(lhs == rhs);
160 }
161
162 private:
163
164 //********************************************
165 iterator(IRegistry::iterator itr_)
166 : itr(itr_)
167 {
168 }
169
170 IRegistry::iterator itr;
171 };
172
173 //********************************************
175 //********************************************
176 class const_iterator : etl::iterator<ETL_OR_STD::forward_iterator_tag, const etl::imessage_router*>
177 {
178 public:
179
180 friend class imessage_router_registry;
181
182 //********************************************
183 const_iterator() {}
184
185 //********************************************
186 const_iterator(const imessage_router_registry::iterator& other)
187 : itr(other.itr)
188 {
189 }
190
191 //********************************************
192 const_iterator(const const_iterator& other)
193 : itr(other.itr)
194 {
195 }
196
197 //********************************************
198 const_iterator& operator=(const const_iterator& other)
199 {
200 itr = other.itr;
201 return *this;
202 }
203
204 //********************************************
205 const etl::imessage_router& operator*() const
206 {
207 return *(itr->second);
208 }
209
210 //********************************************
211 const etl::imessage_router* operator->() const
212 {
213 return itr->second;
214 }
215
216 //********************************************
217 const_iterator& operator++()
218 {
219 ++itr;
220 return *this;
221 }
222
223 //********************************************
224 const_iterator operator++(int)
225 {
226 const_iterator temp(*this);
227 ++itr;
228 return temp;
229 }
230
231 //********************************************
232 friend bool operator==(const const_iterator& lhs, const const_iterator& rhs)
233 {
234 return lhs.itr == rhs.itr;
235 }
236
237 //********************************************
238 friend bool operator!=(const const_iterator& lhs, const const_iterator& rhs)
239 {
240 return !(lhs == rhs);
241 }
242
243 private:
244
245 //********************************************
246 const_iterator(IRegistry::const_iterator itr_)
247 : itr(itr_)
248 {
249 }
250
251 IRegistry::const_iterator itr;
252 };
253
254 //********************************************
256 //********************************************
258 {
259 return iterator(registry.begin());
260 }
261
262 const_iterator begin() const
263 {
264 return const_iterator(registry.cbegin());
265 }
266
267 const_iterator cbegin() const
268 {
269 return const_iterator(registry.cbegin());
270 }
271
272 //********************************************
274 //********************************************
276 {
277 return iterator(registry.end());
278 }
279
280 const_iterator end() const
281 {
282 return const_iterator(registry.cend());
283 }
284
285 const_iterator cend() const
286 {
287 return const_iterator(registry.cend());
288 }
289
290 //********************************************
292 //********************************************
293 etl::imessage_router* find(etl::message_router_id_t id)
294 {
295 IRegistry::iterator itr = registry.find(id);
296
297 if (registry.find(id) != registry.end())
298 {
299 return itr->second;
300 }
301 else
302 {
303 return ETL_NULLPTR;
304 }
305 }
306
307 const etl::imessage_router* find(etl::message_router_id_t id) const
308 {
309 IRegistry::const_iterator itr = registry.find(id);
310
311 if (registry.find(id) != registry.end())
312 {
313 return itr->second;
314 }
315 else
316 {
317 return ETL_NULLPTR;
318 }
319 }
320
321 //********************************************
323 //********************************************
324 iterator lower_bound(etl::message_router_id_t id)
325 {
326 return iterator(registry.lower_bound(id));
327 }
328
329 const_iterator lower_bound(etl::message_router_id_t id) const
330 {
331 return const_iterator(IRegistry::const_iterator(registry.lower_bound(id)));
332 }
333
334 //********************************************
336 //********************************************
337 iterator upper_bound(etl::message_router_id_t id)
338 {
339 return iterator(registry.upper_bound(id));
340 }
341
342 const_iterator upper_bound(etl::message_router_id_t id) const
343 {
344 return const_iterator(IRegistry::const_iterator(registry.upper_bound(id)));
345 }
346
347 //********************************************
350 //********************************************
352 {
353 if (!registry.full() && !contains(router))
354 {
355 IRegistry::value_type element(router.get_message_router_id(), &router);
356
357 registry.insert(element);
358 }
359 else
360 {
361 ETL_ASSERT_FAIL(ETL_ERROR(etl::message_router_registry_full));
362 }
363 }
364
365 //********************************************
368 //********************************************
369 void add(etl::imessage_router* p_router)
370 {
371 if (p_router != ETL_NULLPTR)
372 {
373 add(*p_router);
374 }
375 }
376
377 //********************************************
380 //********************************************
381 template <typename TIterator>
382 void add(TIterator first, const TIterator& last)
383 {
384 while (first != last)
385 {
386 add(*first);
387 ++first;
388 }
389 }
390
391 //********************************************
393 //********************************************
394 void remove(etl::message_router_id_t id)
395 {
396 registry.erase(id);
397 }
398
399 //********************************************
402 //********************************************
403 bool contains(const etl::message_router_id_t id) const
404 {
405 return find(id) != ETL_NULLPTR;
406
407 // return registry.find(id) != registry.end();
408 }
409
410 //********************************************
413 //********************************************
414 bool contains(const etl::imessage_router* const p_router) const
415 {
416 if (p_router == ETL_NULLPTR)
417 {
418 return false;
419 }
420
421 IRegistry::const_iterator irouter = registry.find(p_router->get_message_router_id());
422
423 return (irouter != registry.cend()) && (irouter->second == p_router);
424 }
425
426 //********************************************
429 //********************************************
430 bool contains(const etl::imessage_router& router) const
431 {
432 IRegistry::const_iterator irouter = registry.find(router.get_message_router_id());
433
434 return (irouter != registry.cend()) && (irouter->second == &router);
435 }
436
437 //********************************************
439 //********************************************
440 size_t count(const etl::message_router_id_t id) const
441 {
442 return registry.count(id);
443 }
444
445 //********************************************
447 //********************************************
448 bool empty() const
449 {
450 return registry.empty();
451 }
452
453 //********************************************
455 //********************************************
456 bool full() const
457 {
458 return registry.full();
459 }
460
461 //********************************************
463 //********************************************
464 size_t size() const
465 {
466 return registry.size();
467 }
468
469 //********************************************
471 //********************************************
472 size_t available() const
473 {
474 return registry.available();
475 }
476
477 //********************************************
479 //********************************************
480 size_t max_size() const
481 {
482 return registry.max_size();
483 }
484
485 protected:
486
487 //********************************************
488 // Constructor.
489 //********************************************
490 imessage_router_registry(IRegistry& registry_)
491 : registry(registry_)
492 {
493 }
494
495 private:
496
497 IRegistry& registry;
498 };
499
500 //***************************************************************************
502 //***************************************************************************
503 template <size_t MaxRouters>
504 class message_router_registry : public etl::imessage_router_registry
505 {
506 public:
507
508 //********************************************
509 // Default constructor.
510 //********************************************
511 message_router_registry()
512 : imessage_router_registry(registry)
513 {
514 }
515
516 //********************************************
519 //********************************************
520 template <typename TIterator>
521 message_router_registry(TIterator first, const TIterator& last)
522 : imessage_router_registry(registry)
523 {
524 while (first != last)
525 {
526 this->add(*first);
527 ++first;
528 }
529 }
530
531#if ETL_HAS_INITIALIZER_LIST
532 //********************************************
533 // Initializer_list constructor.
534 //********************************************
535 message_router_registry(std::initializer_list<etl::imessage_router*> init)
536 : imessage_router_registry(registry)
537 {
538 std::initializer_list<etl::imessage_router*>::const_iterator itr = init.begin();
539
540 while (itr != init.end())
541 {
542 this->add(*itr);
543 ++itr;
544 }
545 }
546#endif
547
548 //********************************************
549 // Copy constructor.
550 //********************************************
551 message_router_registry(const message_router_registry& rhs)
552 : imessage_router_registry(registry)
553 {
554 registry = rhs.registry;
555 }
556
557 //********************************************
558 // Assignment operator.
559 //********************************************
560 message_router_registry& operator=(const message_router_registry& rhs)
561 {
562 registry = rhs.registry;
563
564 return *this;
565 }
566
567 private:
568
569 typedef etl::flat_multimap<etl::message_router_id_t, etl::imessage_router*, MaxRouters> Registry;
570 Registry registry;
571 };
572} // namespace etl
573
574#endif
Const Iterator.
Definition message_router_registry.h:177
Iterator.
Definition message_router_registry.h:89
This is the base of all message router registries.
Definition message_router_registry.h:76
void add(etl::imessage_router *p_router)
Definition message_router_registry.h:369
iterator lower_bound(etl::message_router_id_t id)
Get the lower bound in the registry with the specified ID.
Definition message_router_registry.h:324
bool contains(const etl::message_router_id_t id) const
Definition message_router_registry.h:403
bool contains(const etl::imessage_router &router) const
Definition message_router_registry.h:430
bool full() const
Returns true if the registry is full, otherwise false.
Definition message_router_registry.h:456
etl::imessage_router * find(etl::message_router_id_t id)
Get the first router in the registry with the specified ID.
Definition message_router_registry.h:293
bool empty() const
Returns true if the registry is empty, otherwise false.
Definition message_router_registry.h:448
void add(TIterator first, const TIterator &last)
Definition message_router_registry.h:382
size_t size() const
Returns the size of the registry.
Definition message_router_registry.h:464
size_t available() const
Returns the available size of the registry.
Definition message_router_registry.h:472
iterator upper_bound(etl::message_router_id_t id)
Get the upper bound in the registry with the specified ID.
Definition message_router_registry.h:337
bool contains(const etl::imessage_router *const p_router) const
Definition message_router_registry.h:414
size_t max_size() const
Returns the maximum size of the registry.
Definition message_router_registry.h:480
iterator end()
Get the end of the registry.
Definition message_router_registry.h:275
void add(etl::imessage_router &router)
Definition message_router_registry.h:351
iterator begin()
Get the beginning of the registry.
Definition message_router_registry.h:257
void remove(etl::message_router_id_t id)
Unregisters a router.
Definition message_router_registry.h:394
size_t count(const etl::message_router_id_t id) const
Returns the number of routers with the specified ID.
Definition message_router_registry.h:440
This is the base of all message routers.
Definition message_router.h:138
Base exception class for message router registry.
Definition message_router_registry.h:49
The registry is full.
Definition message_router_registry.h:62
Message router registry.
Definition message_router_registry.h:505
message_router_registry(TIterator first, const TIterator &last)
Definition message_router_registry.h:521
ETL_CONSTEXPR14 bool operator!=(const etl::bitset< Active_Bits, TElement > &lhs, const etl::bitset< Active_Bits, TElement > &rhs) ETL_NOEXCEPT
Definition bitset_new.h:2529
Definition exception.h:59
iterator end()
Definition flat_multimap.h:146
const_iterator cbegin() const
Definition flat_multimap.h:164
const_iterator cend() const
Definition flat_multimap.h:173
iterator upper_bound(const_key_reference key)
Definition flat_multimap.h:682
iterator find(const_key_reference key)
Definition flat_multimap.h:587
iterator lower_bound(const_key_reference key)
Definition flat_multimap.h:644
Definition flat_multimap.h:63
bitset_ext
Definition absolute.h:40
iterator
Definition iterator.h:424