Embedded Template Library 1.0
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 "file_error_numbers.h"
34#include "message_router.h"
35#include "flat_multimap.h"
36#include "exception.h"
37#include "error_handler.h"
38#include "iterator.h"
39#include "memory.h"
40
41#include <stdint.h>
42
43namespace etl
44{
45 //***************************************************************************
47 //***************************************************************************
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 //***************************************************************************
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_, line_number_)
67 {
68 }
69 };
70
71 //***************************************************************************
73 //***************************************************************************
75 {
76 private:
77
79
80 public:
81
82 class const_iterator;
83
84 //********************************************
86 //********************************************
87 class iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, etl::imessage_router*>
88 {
89 public:
90
91 friend class imessage_router_registry;
92 friend class const_iterator;
93
94 //********************************************
95 iterator()
96 {
97 }
98
99 //********************************************
100 iterator(const iterator& other)
101 : itr(other.itr)
102 {
103 }
104
105 //********************************************
106 iterator& operator =(const iterator& other)
107 {
108 itr = other.itr;
109 return *this;
110 }
111
112 //********************************************
113 etl::imessage_router& operator *()
114 {
115 return *(itr->second);
116 }
117
118 //********************************************
119 const etl::imessage_router& operator *() const
120 {
121 return *(itr->second);
122 }
123
124 //********************************************
125 etl::imessage_router* operator ->()
126 {
127 return itr->second;
128 }
129
130 //********************************************
131 const etl::imessage_router* operator ->() const
132 {
133 return itr->second;
134 }
135
136 //********************************************
137 iterator& operator ++()
138 {
139 ++itr;
140 return *this;
141 }
142
143 //********************************************
144 iterator operator ++(int)
145 {
146 iterator temp(*this);
147 ++itr;
148 return temp;
149 }
150
151 //********************************************
152 friend bool operator ==(const iterator& lhs, const iterator& rhs)
153 {
154 return lhs.itr == rhs.itr;
155 }
156
157 //********************************************
158 friend bool operator !=(const iterator& lhs, const iterator& rhs)
159 {
160 return !(lhs == rhs);
161 }
162
163 private:
164
165 //********************************************
167 : itr(itr_)
168 {
169 }
170
172 };
173
174 //********************************************
176 //********************************************
177 class const_iterator : etl::iterator<ETL_OR_STD::forward_iterator_tag, const etl::imessage_router*>
178 {
179 public:
180
181 friend class imessage_router_registry;
182
183 //********************************************
185 {
186 }
187
188 //********************************************
190 : itr(other.itr)
191 {
192 }
193
194 //********************************************
195 const_iterator(const const_iterator& other)
196 : itr(other.itr)
197 {
198 }
199
200 //********************************************
201 const_iterator& operator =(const const_iterator& other)
202 {
203 itr = other.itr;
204 return *this;
205 }
206
207 //********************************************
208 const etl::imessage_router& operator *() const
209 {
210 return *(itr->second);
211 }
212
213 //********************************************
214 const etl::imessage_router* operator ->() const
215 {
216 return itr->second;
217 }
218
219 //********************************************
220 const_iterator& operator ++()
221 {
222 ++itr;
223 return *this;
224 }
225
226 //********************************************
227 const_iterator operator ++(int)
228 {
229 const_iterator temp(*this);
230 ++itr;
231 return temp;
232 }
233
234 //********************************************
235 friend bool operator ==(const const_iterator& lhs, const const_iterator& rhs)
236 {
237 return lhs.itr == rhs.itr;
238 }
239
240 //********************************************
241 friend bool operator !=(const const_iterator& lhs, const const_iterator& rhs)
242 {
243 return !(lhs == rhs);
244 }
245
246 private:
247
248 //********************************************
250 : itr(itr_)
251 {
252 }
253
255 };
256
257 //********************************************
259 //********************************************
261 {
262 return iterator(registry.begin());
263 }
264
265 const_iterator begin() const
266 {
267 return const_iterator(registry.cbegin());
268 }
269
270 const_iterator cbegin() const
271 {
272 return const_iterator(registry.cbegin());
273 }
274
275 //********************************************
277 //********************************************
279 {
280 return iterator(registry.end());
281 }
282
283 const_iterator end() const
284 {
285 return const_iterator(registry.cend());
286 }
287
288 const_iterator cend() const
289 {
290 return const_iterator(registry.cend());
291 }
292
293 //********************************************
295 //********************************************
296 etl::imessage_router* find(etl::message_router_id_t id)
297 {
298 IRegistry::iterator itr = registry.find(id);
299
300 if (registry.find(id) != registry.end())
301 {
302 return itr->second;
303 }
304 else
305 {
306 return ETL_NULLPTR;
307 }
308 }
309
310 const etl::imessage_router* find(etl::message_router_id_t id) const
311 {
312 IRegistry::const_iterator itr = registry.find(id);
313
314 if (registry.find(id) != registry.end())
315 {
316 return itr->second;
317 }
318 else
319 {
320 return ETL_NULLPTR;
321 }
322 }
323
324 //********************************************
326 //********************************************
327 iterator lower_bound(etl::message_router_id_t id)
328 {
329 return iterator(registry.lower_bound(id));
330 }
331
332 const_iterator lower_bound(etl::message_router_id_t id) const
333 {
334 return const_iterator(IRegistry::const_iterator(registry.lower_bound(id)));
335 }
336
337 //********************************************
339 //********************************************
340 iterator upper_bound(etl::message_router_id_t id)
341 {
342 return iterator(registry.upper_bound(id));
343 }
344
345 const_iterator upper_bound(etl::message_router_id_t id) const
346 {
347 return const_iterator(IRegistry::const_iterator(registry.upper_bound(id)));
348 }
349
350 //********************************************
353 //********************************************
355 {
356 if (!registry.full() && !contains(router))
357 {
358 IRegistry::value_type element(router.get_message_router_id(), &router);
359
360 registry.insert(element);
361 }
362 else
363 {
364 ETL_ASSERT_FAIL(ETL_ERROR(etl::message_router_registry_full));
365 }
366 }
367
368 //********************************************
371 //********************************************
372 void add(etl::imessage_router* p_router)
373 {
374 if (p_router != ETL_NULLPTR)
375 {
376 add(*p_router);
377 }
378 }
379
380 //********************************************
383 //********************************************
384 template <typename TIterator>
385 void add(TIterator first, const TIterator& last)
386 {
387 while (first != last)
388 {
389 add(*first);
390 ++first;
391 }
392 }
393
394 //********************************************
396 //********************************************
397 void remove(etl::message_router_id_t id)
398 {
399 registry.erase(id);
400 }
401
402 //********************************************
405 //********************************************
406 bool contains(const etl::message_router_id_t id) const
407 {
408 return find(id) != ETL_NULLPTR;
409
410
411 //return registry.find(id) != registry.end();
412 }
413
414 //********************************************
417 //********************************************
418 bool contains(const etl::imessage_router* const p_router) const
419 {
420 if (p_router == ETL_NULLPTR)
421 {
422 return false;
423 }
424
425 IRegistry::const_iterator irouter = registry.find(p_router->get_message_router_id());
426
427 return (irouter != registry.cend()) && (irouter->second == p_router);
428 }
429
430 //********************************************
433 //********************************************
434 bool contains(const etl::imessage_router& router) const
435 {
436 IRegistry::const_iterator irouter = registry.find(router.get_message_router_id());
437
438 return (irouter != registry.cend()) && (irouter->second == &router);
439 }
440
441 //********************************************
443 //********************************************
444 size_t count(const etl::message_router_id_t id) const
445 {
446 return registry.count(id);
447 }
448
449 //********************************************
451 //********************************************
452 bool empty() const
453 {
454 return registry.empty();
455 }
456
457 //********************************************
459 //********************************************
460 bool full() const
461 {
462 return registry.full();
463 }
464
465 //********************************************
467 //********************************************
468 size_t size() const
469 {
470 return registry.size();
471 }
472
473 //********************************************
475 //********************************************
476 size_t available() const
477 {
478 return registry.available();
479 }
480
481 //********************************************
483 //********************************************
484 size_t max_size() const
485 {
486 return registry.max_size();
487 }
488
489 protected:
490
491 //********************************************
492 // Constructor.
493 //********************************************
494 imessage_router_registry(IRegistry& registry_)
495 : registry(registry_)
496 {
497 }
498
499 private:
500
501 IRegistry& registry;
502 };
503
504 //***************************************************************************
506 //***************************************************************************
507 template <size_t MaxRouters>
509 {
510 public:
511
512 //********************************************
513 // Default constructor.
514 //********************************************
516 : imessage_router_registry(registry)
517 {
518 }
519
520 //********************************************
523 //********************************************
524 template <typename TIterator>
525 message_router_registry(TIterator first, const TIterator& last)
526 : imessage_router_registry(registry)
527 {
528 while (first != last)
529 {
530 this->add(*first);
531 ++first;
532 }
533 }
534
535#if ETL_HAS_INITIALIZER_LIST
536 //********************************************
537 // Initializer_list constructor.
538 //********************************************
539 message_router_registry(std::initializer_list<etl::imessage_router*> init)
540 : imessage_router_registry(registry)
541 {
542 std::initializer_list<etl::imessage_router*>::const_iterator itr = init.begin();
543
544 while (itr != init.end())
545 {
546 this->add(*itr);
547 ++itr;
548 }
549 }
550#endif
551
552 //********************************************
553 // Copy constructor.
554 //********************************************
555 message_router_registry(const message_router_registry& rhs)
556 : imessage_router_registry(registry)
557 {
558 registry = rhs.registry;
559 }
560
561 //********************************************
562 // Assignment operator.
563 //********************************************
564 message_router_registry& operator =(const message_router_registry& rhs)
565 {
566 registry = rhs.registry;
567
568 return *this;
569 }
570
571 private:
572
574 Registry registry;
575 };
576}
577
578#endif
Const Iterator.
Definition: message_router_registry.h:178
Iterator.
Definition: message_router_registry.h:88
This is the base of all message router registries.
Definition: message_router_registry.h:75
void add(etl::imessage_router *p_router)
Definition: message_router_registry.h:372
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:327
bool contains(const etl::message_router_id_t id) const
Definition: message_router_registry.h:406
bool contains(const etl::imessage_router &router) const
Definition: message_router_registry.h:434
bool full() const
Returns true if the registry is full, otherwise false.
Definition: message_router_registry.h:460
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:296
bool empty() const
Returns true if the registry is empty, otherwise false.
Definition: message_router_registry.h:452
void add(TIterator first, const TIterator &last)
Definition: message_router_registry.h:385
size_t size() const
Returns the size of the registry.
Definition: message_router_registry.h:468
size_t available() const
Returns the available size of the registry.
Definition: message_router_registry.h:476
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:340
bool contains(const etl::imessage_router *const p_router) const
Definition: message_router_registry.h:418
size_t max_size() const
Returns the maximum size of the registry.
Definition: message_router_registry.h:484
iterator end()
Get the end of the registry.
Definition: message_router_registry.h:278
void add(etl::imessage_router &router)
Definition: message_router_registry.h:354
iterator begin()
Get the beginning of the registry.
Definition: message_router_registry.h:260
void remove(etl::message_router_id_t id)
Unregisters a router.
Definition: message_router_registry.h:397
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:444
This is the base of all message routers.
Definition: message_router_generator.h:121
Definition: reference_flat_multimap.h:191
Definition: reference_flat_multimap.h:107
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:509
message_router_registry(TIterator first, const TIterator &last)
Definition: message_router_registry.h:525
Definition: exception.h:47
iterator end()
Definition: flat_multimap.h:145
size_type size() const
Definition: flat_multimap.h:788
const_iterator cbegin() const
Definition: flat_multimap.h:163
size_t available() const
Definition: flat_multimap.h:833
bool full() const
Definition: flat_multimap.h:806
size_type max_size() const
Definition: flat_multimap.h:824
size_t erase(const_key_reference key)
Definition: flat_multimap.h:464
size_t count(const_key_reference key) const
Definition: flat_multimap.h:614
iterator begin()
Definition: flat_multimap.h:127
const_iterator cend() const
Definition: flat_multimap.h:172
iterator upper_bound(const_key_reference key)
Definition: flat_multimap.h:671
ETL_OR_STD::pair< iterator, bool > insert(const value_type &value)
Definition: flat_multimap.h:260
bool empty() const
Definition: flat_multimap.h:797
iterator find(const_key_reference key)
Definition: flat_multimap.h:576
iterator lower_bound(const_key_reference key)
Definition: flat_multimap.h:633
Definition: flat_multimap.h:62
bitset_ext
Definition: absolute.h:38
ETL_CONSTEXPR TContainer::const_iterator cbegin(const TContainer &container)
Definition: iterator.h:951
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition: iterator.h:981
iterator
Definition: iterator.h:399