Loading...
Searching...
No Matches
message_router_registry.h
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_MESSAGE_ROUTER_REGISTRY_INCLUDED
#define ETL_MESSAGE_ROUTER_REGISTRY_INCLUDED
#include "platform.h"
#include "error_handler.h"
#include "exception.h"
#include "file_error_numbers.h"
#include "flat_multimap.h"
#include "iterator.h"
#include "memory.h"
#include "message_router.h"
#include <stddef.h>
namespace etl
{
//***************************************************************************
//***************************************************************************
class message_router_registry_exception : public etl::exception
{
public:
message_router_registry_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: etl::exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class message_router_registry_full : public etl::message_router_registry_exception
{
public:
message_router_registry_full(string_type file_name_, numeric_type line_number_)
: message_router_registry_exception(ETL_ERROR_TEXT("message router registry:full", ETL_MESSAGE_ROUTER_REGISTRY_FILE_ID"A"), file_name_,
line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class imessage_router_registry
{
private:
typedef etl::iflat_multimap<etl::message_router_id_t, etl::imessage_router*> IRegistry;
public:
class const_iterator;
//********************************************
//********************************************
class iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, etl::imessage_router*>
{
public:
friend class imessage_router_registry;
friend class const_iterator;
//********************************************
iterator() {}
//********************************************
iterator(const iterator& other)
: itr(other.itr)
{
}
//********************************************
iterator& operator=(const iterator& other)
{
itr = other.itr;
return *this;
}
//********************************************
etl::imessage_router& operator*()
{
return *(itr->second);
}
//********************************************
const etl::imessage_router& operator*() const
{
return *(itr->second);
}
//********************************************
etl::imessage_router* operator->()
{
return itr->second;
}
//********************************************
const etl::imessage_router* operator->() const
{
return itr->second;
}
//********************************************
iterator& operator++()
{
++itr;
return *this;
}
//********************************************
iterator operator++(int)
{
iterator temp(*this);
++itr;
return temp;
}
//********************************************
friend bool operator==(const iterator& lhs, const iterator& rhs)
{
return lhs.itr == rhs.itr;
}
//********************************************
friend bool operator!=(const iterator& lhs, const iterator& rhs)
{
return !(lhs == rhs);
}
private:
//********************************************
iterator(IRegistry::iterator itr_)
: itr(itr_)
{
}
IRegistry::iterator itr;
};
//********************************************
//********************************************
class const_iterator : etl::iterator<ETL_OR_STD::forward_iterator_tag, const etl::imessage_router*>
{
public:
friend class imessage_router_registry;
//********************************************
const_iterator() {}
//********************************************
const_iterator(const imessage_router_registry::iterator& other)
: itr(other.itr)
{
}
//********************************************
const_iterator(const const_iterator& other)
: itr(other.itr)
{
}
//********************************************
const_iterator& operator=(const const_iterator& other)
{
itr = other.itr;
return *this;
}
//********************************************
const etl::imessage_router& operator*() const
{
return *(itr->second);
}
//********************************************
const etl::imessage_router* operator->() const
{
return itr->second;
}
//********************************************
const_iterator& operator++()
{
++itr;
return *this;
}
//********************************************
const_iterator operator++(int)
{
const_iterator temp(*this);
++itr;
return temp;
}
//********************************************
friend bool operator==(const const_iterator& lhs, const const_iterator& rhs)
{
return lhs.itr == rhs.itr;
}
//********************************************
friend bool operator!=(const const_iterator& lhs, const const_iterator& rhs)
{
return !(lhs == rhs);
}
private:
//********************************************
const_iterator(IRegistry::const_iterator itr_)
: itr(itr_)
{
}
IRegistry::const_iterator itr;
};
//********************************************
//********************************************
iterator begin()
{
return iterator(registry.begin());
}
const_iterator begin() const
{
return const_iterator(registry.cbegin());
}
const_iterator cbegin() const
{
return const_iterator(registry.cbegin());
}
//********************************************
//********************************************
iterator end()
{
return iterator(registry.end());
}
const_iterator end() const
{
return const_iterator(registry.cend());
}
const_iterator cend() const
{
return const_iterator(registry.cend());
}
//********************************************
//********************************************
etl::imessage_router* find(etl::message_router_id_t id)
{
IRegistry::iterator itr = registry.find(id);
if (registry.find(id) != registry.end())
{
return itr->second;
}
else
{
return ETL_NULLPTR;
}
}
const etl::imessage_router* find(etl::message_router_id_t id) const
{
IRegistry::const_iterator itr = registry.find(id);
if (registry.find(id) != registry.end())
{
return itr->second;
}
else
{
return ETL_NULLPTR;
}
}
//********************************************
//********************************************
iterator lower_bound(etl::message_router_id_t id)
{
return iterator(registry.lower_bound(id));
}
const_iterator lower_bound(etl::message_router_id_t id) const
{
return const_iterator(IRegistry::const_iterator(registry.lower_bound(id)));
}
//********************************************
//********************************************
iterator upper_bound(etl::message_router_id_t id)
{
return iterator(registry.upper_bound(id));
}
const_iterator upper_bound(etl::message_router_id_t id) const
{
return const_iterator(IRegistry::const_iterator(registry.upper_bound(id)));
}
//********************************************
//********************************************
void add(etl::imessage_router& router)
{
if (!registry.full() && !contains(router))
{
IRegistry::value_type element(router.get_message_router_id(), &router);
registry.insert(element);
}
else
{
ETL_ASSERT_FAIL(ETL_ERROR(etl::message_router_registry_full));
}
}
//********************************************
//********************************************
void add(etl::imessage_router* p_router)
{
if (p_router != ETL_NULLPTR)
{
add(*p_router);
}
}
//********************************************
//********************************************
template <typename TIterator>
void add(TIterator first, const TIterator& last)
{
while (first != last)
{
add(*first);
++first;
}
}
//********************************************
//********************************************
void remove(etl::message_router_id_t id)
{
registry.erase(id);
}
//********************************************
//********************************************
bool contains(const etl::message_router_id_t id) const
{
return find(id) != ETL_NULLPTR;
// return registry.find(id) != registry.end();
}
//********************************************
//********************************************
bool contains(const etl::imessage_router* const p_router) const
{
if (p_router == ETL_NULLPTR)
{
return false;
}
IRegistry::const_iterator irouter = registry.find(p_router->get_message_router_id());
return (irouter != registry.cend()) && (irouter->second == p_router);
}
//********************************************
//********************************************
bool contains(const etl::imessage_router& router) const
{
IRegistry::const_iterator irouter = registry.find(router.get_message_router_id());
return (irouter != registry.cend()) && (irouter->second == &router);
}
//********************************************
//********************************************
size_t count(const etl::message_router_id_t id) const
{
return registry.count(id);
}
//********************************************
//********************************************
bool empty() const
{
return registry.empty();
}
//********************************************
//********************************************
bool full() const
{
return registry.full();
}
//********************************************
//********************************************
size_t size() const
{
return registry.size();
}
//********************************************
//********************************************
size_t available() const
{
return registry.available();
}
//********************************************
//********************************************
size_t max_size() const
{
return registry.max_size();
}
protected:
//********************************************
// Constructor.
//********************************************
imessage_router_registry(IRegistry& registry_)
: registry(registry_)
{
}
private:
IRegistry& registry;
};
//***************************************************************************
//***************************************************************************
template <size_t MaxRouters>
class message_router_registry : public etl::imessage_router_registry
{
public:
//********************************************
// Default constructor.
//********************************************
message_router_registry()
: imessage_router_registry(registry)
{
}
//********************************************
//********************************************
template <typename TIterator>
message_router_registry(TIterator first, const TIterator& last)
: imessage_router_registry(registry)
{
while (first != last)
{
this->add(*first);
++first;
}
}
#if ETL_HAS_INITIALIZER_LIST
//********************************************
// Initializer_list constructor.
//********************************************
message_router_registry(std::initializer_list<etl::imessage_router*> init)
: imessage_router_registry(registry)
{
std::initializer_list<etl::imessage_router*>::const_iterator itr = init.begin();
while (itr != init.end())
{
this->add(*itr);
++itr;
}
}
#endif
//********************************************
// Copy constructor.
//********************************************
message_router_registry(const message_router_registry& rhs)
: imessage_router_registry(registry)
{
registry = rhs.registry;
}
//********************************************
// Assignment operator.
//********************************************
message_router_registry& operator=(const message_router_registry& rhs)
{
registry = rhs.registry;
return *this;
}
private:
typedef etl::flat_multimap<etl::message_router_id_t, etl::imessage_router*, MaxRouters> Registry;
Registry registry;
};
} // namespace etl
#endif
etl::imessage_router_registry::const_iterator
Const Iterator.
Definition message_router_registry.h:177
etl::imessage_router_registry::iterator
Iterator.
Definition message_router_registry.h:89
etl::imessage_router_registry
This is the base of all message router registries.
Definition message_router_registry.h:76
etl::imessage_router_registry::add
void add(etl::imessage_router *p_router)
Definition message_router_registry.h:369
etl::imessage_router_registry::lower_bound
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
etl::imessage_router_registry::contains
bool contains(const etl::message_router_id_t id) const
Definition message_router_registry.h:403
etl::imessage_router_registry::contains
bool contains(const etl::imessage_router &router) const
Definition message_router_registry.h:430
etl::imessage_router_registry::full
bool full() const
Returns true if the registry is full, otherwise false.
Definition message_router_registry.h:456
etl::imessage_router_registry::find
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
etl::imessage_router_registry::empty
bool empty() const
Returns true if the registry is empty, otherwise false.
Definition message_router_registry.h:448
etl::imessage_router_registry::add
void add(TIterator first, const TIterator &last)
Definition message_router_registry.h:382
etl::imessage_router_registry::size
size_t size() const
Returns the size of the registry.
Definition message_router_registry.h:464
etl::imessage_router_registry::available
size_t available() const
Returns the available size of the registry.
Definition message_router_registry.h:472
etl::imessage_router_registry::upper_bound
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
etl::imessage_router_registry::contains
bool contains(const etl::imessage_router *const p_router) const
Definition message_router_registry.h:414
etl::imessage_router_registry::max_size
size_t max_size() const
Returns the maximum size of the registry.
Definition message_router_registry.h:480
etl::imessage_router_registry::end
iterator end()
Get the end of the registry.
Definition message_router_registry.h:275
etl::imessage_router_registry::add
void add(etl::imessage_router &router)
Definition message_router_registry.h:351
etl::imessage_router_registry::begin
iterator begin()
Get the beginning of the registry.
Definition message_router_registry.h:257
etl::imessage_router_registry::remove
void remove(etl::message_router_id_t id)
Unregisters a router.
Definition message_router_registry.h:394
etl::imessage_router_registry::count
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
etl::imessage_router
This is the base of all message routers.
Definition message_router.h:141
etl::message_router_registry_exception
Base exception class for message router registry.
Definition message_router_registry.h:49
etl::message_router_registry_full
The registry is full.
Definition message_router_registry.h:62
etl::message_router_registry
Message router registry.
Definition message_router_registry.h:505
etl::message_router_registry::message_router_registry
message_router_registry(TIterator first, const TIterator &last)
Definition message_router_registry.h:521
error_handler.h
exception.h
file_error_numbers.h
flat_multimap.h
etl::const_iterator
etl::exception
Definition exception.h:59
etl::iflat_multimap::end
iterator end()
Definition flat_multimap.h:146
etl::iflat_multimap::cbegin
const_iterator cbegin() const
Definition flat_multimap.h:164
etl::iflat_multimap::cend
const_iterator cend() const
Definition flat_multimap.h:173
etl::iflat_multimap::upper_bound
iterator upper_bound(const_key_reference key)
Definition flat_multimap.h:682
etl::iflat_multimap::find
iterator find(const_key_reference key)
Definition flat_multimap.h:587
etl::iflat_multimap::lower_bound
iterator lower_bound(const_key_reference key)
Definition flat_multimap.h:644
etl::iflat_multimap
Definition flat_multimap.h:63
iterator.h
memory.h
etl
Definition absolute.h:40
platform.h
etl::iterator
iterator
Definition iterator.h:482
operator==
ETL_CONSTEXPR14 bool operator==(const etl::to_arithmetic_result< T > &lhs, const etl::to_arithmetic_result< T > &rhs)
Equality test for etl::to_arithmetic_result.
Definition to_arithmetic.h:903
operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::to_arithmetic_result< T > &lhs, const etl::to_arithmetic_result< T > &rhs)
Inequality test for etl::to_arithmetic_result.
Definition to_arithmetic.h:937