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 "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 //***************************************************************************
57
58 //***************************************************************************
60 //***************************************************************************
62 {
63 public:
64
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 //********************************************
107 {
108 itr = other.itr;
109 return *this;
110 }
111
112 //********************************************
114 {
115 return *(itr->second);
116 }
117
118 //********************************************
119 const etl::imessage_router& operator *() const
120 {
121 return *(itr->second);
122 }
123
124 //********************************************
126 {
127 return itr->second;
128 }
129
130 //********************************************
131 const etl::imessage_router* operator ->() const
132 {
133 return itr->second;
134 }
135
136 //********************************************
138 {
139 ++itr;
140 return *this;
141 }
142
143 //********************************************
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 //********************************************
166 iterator(IRegistry::iterator itr_)
167 : itr(itr_)
168 {
169 }
170
171 IRegistry::iterator itr;
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 //********************************************
196 : itr(other.itr)
197 {
198 }
199
200 //********************************************
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 //********************************************
221 {
222 ++itr;
223 return *this;
224 }
225
226 //********************************************
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 //********************************************
249 const_iterator(IRegistry::const_iterator itr_)
250 : itr(itr_)
251 {
252 }
253
254 IRegistry::const_iterator itr;
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 //********************************************
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 //********************************************
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 //********************************************
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 //********************************************
398 {
399 registry.erase(id);
400 }
401
402 //********************************************
405 //********************************************
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 //********************************************
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 //********************************************
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>
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
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
ETL_CONSTEXPR14 bool operator==(const etl::expected< TValue, TError > &lhs, const etl::expected< TValue2, TError2 > &rhs)
Equivalence operators.
Definition expected.h:968
Definition exception.h:47
bitset_ext
Definition absolute.h:38
iterator
Definition iterator.h:399
pair holds two objects of arbitrary type
Definition utility.h:164
T2 second
second is a copy of the second object
Definition utility.h:169