Loading...
Searching...
No Matches
observer.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) 2014 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_OBSERVER_INCLUDED
#define ETL_OBSERVER_INCLUDED
//*****************************************************************************
//*****************************************************************************
#include "platform.h"
#include "algorithm.h"
#include "error_handler.h"
#include "exception.h"
#include "type_list.h"
#include "utility.h"
#include "vector.h"
namespace etl
{
//***************************************************************************
//***************************************************************************
class observer_exception : public exception
{
public:
observer_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class observer_list_full : public observer_exception
{
public:
observer_list_full(string_type file_name_, numeric_type line_number_)
: observer_exception(ETL_ERROR_TEXT("observer:full", ETL_OBSERVER_FILE_ID"A"), file_name_, line_number_)
{
}
};
//*********************************************************************
//*********************************************************************
template <typename TObserver, const size_t Max_Observers>
class observable
{
private:
//***********************************
// Item stored in the observer list.
//***********************************
struct observer_item
{
observer_item(TObserver& observer_)
: p_observer(&observer_)
, enabled(true)
{
}
TObserver* p_observer;
bool enabled;
};
//***********************************
// How to compare an observer with an observer list item.
//***********************************
struct compare_observers
{
compare_observers(TObserver& observer_)
: p_observer(&observer_)
{
}
bool operator()(const observer_item& item) const
{
return p_observer == item.p_observer;
}
TObserver* p_observer;
};
public:
typedef size_t size_type;
typedef etl::vector<observer_item, Max_Observers> Observer_List;
//*****************************************************************
//*****************************************************************
void add_observer(TObserver& observer)
{
// See if we already have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Not there?
if (i_observer_item == observer_list.end())
{
// Is there enough room?
ETL_ASSERT_OR_RETURN(!observer_list.full(), ETL_ERROR(etl::observer_list_full));
// Add it.
observer_list.push_back(observer_item(observer));
}
}
//*****************************************************************
//*****************************************************************
bool remove_observer(TObserver& observer)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Found it?
if (i_observer_item != observer_list.end())
{
// Erase it.
observer_list.erase(i_observer_item);
return true;
}
else
{
return false;
}
}
//*****************************************************************
//*****************************************************************
void enable_observer(TObserver& observer, bool state = true)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Found it?
if (i_observer_item != observer_list.end())
{
i_observer_item->enabled = state;
}
}
//*****************************************************************
//*****************************************************************
void disable_observer(TObserver& observer)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Found it?
if (i_observer_item != observer_list.end())
{
i_observer_item->enabled = false;
}
}
//*****************************************************************
//*****************************************************************
void clear_observers()
{
observer_list.clear();
}
//*****************************************************************
//*****************************************************************
size_type number_of_observers() const
{
return observer_list.size();
}
//*****************************************************************
//*****************************************************************
template <typename TNotification>
void notify_observers(TNotification n)
{
typename Observer_List::iterator i_observer_item = observer_list.begin();
while (i_observer_item != observer_list.end())
{
if (i_observer_item->enabled)
{
i_observer_item->p_observer->notification(n);
}
++i_observer_item;
}
}
//*****************************************************************
//*****************************************************************
void notify_observers()
{
typename Observer_List::iterator i_observer_item = observer_list.begin();
while (i_observer_item != observer_list.end())
{
if (i_observer_item->enabled)
{
i_observer_item->p_observer->notification();
}
++i_observer_item;
}
}
protected:
~observable() {}
private:
//*****************************************************************
//*****************************************************************
typename Observer_List::iterator find_observer(TObserver& observer_)
{
return etl::find_if(observer_list.begin(), observer_list.end(), compare_observers(observer_));
}
Observer_List observer_list;
};
#if ETL_USING_CPP11 && !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
template <typename... TTypes>
class observer;
//*****************************************************************
//*****************************************************************
template <typename T1, typename... TRest>
class observer<T1, TRest...>
: public observer<T1>
, public observer<TRest...>
{
public:
ETL_STATIC_ASSERT((!etl::has_duplicates<T1, TRest...>::value), "Observer has duplicate notification types");
using observer<T1>::notification;
using observer<TRest...>::notification;
using type_list = etl::type_list<T1, TRest...>;
};
//*****************************************************************
//*****************************************************************
template <typename T1>
class observer<T1>
{
public:
using type_list = etl::type_list<T1>;
virtual ~observer() = default;
virtual void notification(T1) = 0;
};
//*****************************************************************
//*****************************************************************
template <>
class observer<void>
{
public:
using type_list = etl::type_list<>;
virtual ~observer() = default;
virtual void notification() = 0;
};
//***************************************************************************
template <typename TList>
struct observer_from_type_list;
template <typename... TTypes>
struct observer_from_type_list<etl::type_list<TTypes...>>
{
using type = etl::observer<TTypes...>;
};
template <typename TTypeList>
using observer_from_type_list_t = typename observer_from_type_list<TTypeList>::type;
#else
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2 = void, typename T3 = void, typename T4 = void, typename T5 = void, typename T6 = void, typename T7 = void,
typename T8 = void>
class observer
: public observer<T1>
, public observer<T2>
, public observer<T3>
, public observer<T4>
, public observer<T5>
, public observer<T6>
, public observer<T7>
, public observer<T8>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
using observer<T3>::notification;
using observer<T4>::notification;
using observer<T5>::notification;
using observer<T6>::notification;
using observer<T7>::notification;
using observer<T8>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
class observer<T1, T2, T3, T4, T5, T6, T7>
: public observer<T1>
, public observer<T2>
, public observer<T3>
, public observer<T4>
, public observer<T5>
, public observer<T6>
, public observer<T7>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
using observer<T3>::notification;
using observer<T4>::notification;
using observer<T5>::notification;
using observer<T6>::notification;
using observer<T7>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
class observer<T1, T2, T3, T4, T5, T6>
: public observer<T1>
, public observer<T2>
, public observer<T3>
, public observer<T4>
, public observer<T5>
, public observer<T6>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
using observer<T3>::notification;
using observer<T4>::notification;
using observer<T5>::notification;
using observer<T6>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2, typename T3, typename T4, typename T5>
class observer<T1, T2, T3, T4, T5>
: public observer<T1>
, public observer<T2>
, public observer<T3>
, public observer<T4>
, public observer<T5>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
using observer<T3>::notification;
using observer<T4>::notification;
using observer<T5>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2, typename T3, typename T4>
class observer<T1, T2, T3, T4>
: public observer<T1>
, public observer<T2>
, public observer<T3>
, public observer<T4>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
using observer<T3>::notification;
using observer<T4>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2, typename T3>
class observer<T1, T2, T3>
: public observer<T1>
, public observer<T2>
, public observer<T3>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
using observer<T3>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1, typename T2>
class observer<T1, T2>
: public observer<T1>
, public observer<T2>
{
public:
virtual ~observer() {}
using observer<T1>::notification;
using observer<T2>::notification;
};
//*********************************************************************
//*********************************************************************
template <typename T1>
class observer<T1>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
};
//*********************************************************************
//*********************************************************************
template <>
class observer<void>
{
public:
virtual ~observer() {}
virtual void notification() = 0;
};
#endif
} // namespace etl
#endif
algorithm.h
error_handler.h
exception.h
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::observable::add_observer
void add_observer(TObserver &observer)
Definition observer.h:148
etl::observable::remove_observer
bool remove_observer(TObserver &observer)
Definition observer.h:169
etl::observable::notify_observers
void notify_observers(TNotification n)
Definition observer.h:242
etl::observable::notify_observers
void notify_observers()
Notify all of the observers, sending them the notification.
Definition observer.h:260
etl::observable::disable_observer
void disable_observer(TObserver &observer)
Disable an observer.
Definition observer.h:208
etl::observable::enable_observer
void enable_observer(TObserver &observer, bool state=true)
Definition observer.h:193
etl::observable::number_of_observers
size_type number_of_observers() const
Returns the number of observers.
Definition observer.h:231
etl::observable::clear_observers
void clear_observers()
Clear all observers from the list.
Definition observer.h:223
etl::observable
Definition observer.h:99
etl::observer
Definition observer.h:379
etl::observer_list_full
Definition observer.h:82
etl::vector
Definition vector.h:1830
etl
Definition absolute.h:40
platform.h
utility.h
vector.h