Loading...
Searching...
No Matches
delegate_observable.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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_DELEGATE_OBSERVABLE_INCLUDED
#define ETL_DELEGATE_OBSERVABLE_INCLUDED
#include "platform.h"
#include "array.h"
#include "delegate.h"
namespace etl
{
//*********************************************************************
//*********************************************************************
template <typename TNotification, size_t Max_Observers>
class delegate_observable
{
public:
typedef etl::delegate<void(TNotification)> delegate_type;
private:
typedef etl::array<delegate_type, Max_Observers> DelegateList;
public:
typedef size_t size_type;
typedef TNotification notification_type;
//*****************************************************************
//*****************************************************************
ETL_CONSTEXPR14 delegate_observable()
: delegate_list()
, delegate_count(0)
{
}
#if ETL_USING_CPP11
//*****************************************************************
//*****************************************************************
template <typename... TDelegate>
ETL_CONSTEXPR14 delegate_observable(TDelegate&&... delegates)
: delegate_list{etl::forward<TDelegate>(delegates)...}
, delegate_count(sizeof...(delegates))
{
ETL_STATIC_ASSERT(Max_Observers >= sizeof...(delegates), "Number of delegates exceeds maximum observers");
ETL_STATIC_ASSERT((etl::are_all_same<delegate_type, etl::decay_t<TDelegate>...>::value), "All delegates must be delegate_type");
}
//*****************************************************************
//*****************************************************************
template <typename... TDelegate>
ETL_CONSTEXPR14 delegate_observable(notification_type, TDelegate&&... delegates)
: delegate_observable(etl::forward<TDelegate>(delegates)...)
{
}
#endif
//*****************************************************************
//*****************************************************************
ETL_CONSTEXPR14 bool add_observer(delegate_type observer)
{
for (size_t i = 0; i < Max_Observers; ++i)
{
if (delegate_list[i] == observer)
{
// Already there, so just return.
return true;
}
}
// If we get here, then we need to add it.
for (size_t i = 0; i < Max_Observers; ++i)
{
if (!delegate_list[i].is_valid())
{
// Found an empty slot, so add it.
delegate_list[i] = observer;
++delegate_count;
return true;
}
}
return false;
}
//*****************************************************************
//*****************************************************************
ETL_CONSTEXPR14 bool remove_observer(const delegate_type& observer)
{
for (size_t i = 0; i < Max_Observers; ++i)
{
if (delegate_list[i] == observer)
{
// Clear it.
delegate_list[i].clear();
--delegate_count;
return true;
}
}
return false;
}
//*****************************************************************
//*****************************************************************
ETL_CONSTEXPR14 void clear_observers()
{
for (size_t i = 0; i < Max_Observers; ++i)
{
delegate_list[i].clear();
}
delegate_count = 0;
}
//*****************************************************************
//*****************************************************************
ETL_CONSTEXPR14 size_type number_of_observers() const
{
return delegate_count;
}
//*****************************************************************
//*****************************************************************
ETL_CONSTEXPR14 void notify_observers(notification_type n) const
{
if (delegate_count != 0)
{
for (size_t i = 0; i < Max_Observers; ++i)
{
delegate_list[i].call_if(n);
}
}
}
private:
DelegateList delegate_list;
size_t delegate_count;
};
//*************************************************************************
//*************************************************************************
#if ETL_USING_CPP17
template <typename TNotification, typename... TDelegates>
delegate_observable(TNotification, TDelegates...) -> delegate_observable<TNotification, sizeof...(TDelegates)>;
#endif
} // namespace etl
#endif
array.h
etl::delegate
Declaration.
Definition delegate_cpp03.h:191
delegate.h
etl::array
Definition array.h:85
etl::delegate_observable::clear_observers
ETL_CONSTEXPR14 void clear_observers()
Clear all observers.
Definition delegate_observable.h:157
etl::delegate_observable::notification_type
TNotification notification_type
The type of the notification.
Definition delegate_observable.h:64
etl::delegate_observable::delegate_type
etl::delegate< void(TNotification)> delegate_type
The type of the observers.
Definition delegate_observable.h:52
etl::delegate_observable::delegate_observable
ETL_CONSTEXPR14 delegate_observable()
Default constructor.
Definition delegate_observable.h:69
etl::delegate_observable::remove_observer
ETL_CONSTEXPR14 bool remove_observer(const delegate_type &observer)
Definition delegate_observable.h:138
etl::delegate_observable::notify_observers
ETL_CONSTEXPR14 void notify_observers(notification_type n) const
Definition delegate_observable.h:180
etl::delegate_observable::add_observer
ETL_CONSTEXPR14 bool add_observer(delegate_type observer)
Definition delegate_observable.h:107
etl::delegate_observable::number_of_observers
ETL_CONSTEXPR14 size_type number_of_observers() const
Returns the number of observers.
Definition delegate_observable.h:170
etl::delegate_observable::size_type
size_t size_type
The type for sizes.
Definition delegate_observable.h:61
etl::observer
Definition observer.h:379
etl
Definition absolute.h:40
platform.h