mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
Updated C++03 observer class to accept void parameter notification types
This commit is contained in:
parent
f219e86806
commit
73395fe6f4
@ -294,19 +294,23 @@ namespace etl
|
||||
Observer_List observer_list;
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP11 && !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
|
||||
#if ETL_USING_CPP11
|
||||
template <typename... TTypes>
|
||||
class observer;
|
||||
|
||||
//*****************************************************************
|
||||
/// The observer class for N types.
|
||||
///\ingroup observer
|
||||
//*****************************************************************
|
||||
template <typename T1, typename... Types>
|
||||
class observer : public observer<T1>, public observer<Types...>
|
||||
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<Types...>::notification;
|
||||
using observer<TRest...>::notification;
|
||||
};
|
||||
|
||||
//*****************************************************************
|
||||
@ -351,18 +355,26 @@ namespace etl
|
||||
typename T6 = void,
|
||||
typename T7 = void,
|
||||
typename T8 = void>
|
||||
class observer
|
||||
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() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
virtual void notification(T3) = 0;
|
||||
virtual void notification(T4) = 0;
|
||||
virtual void notification(T5) = 0;
|
||||
virtual void notification(T6) = 0;
|
||||
virtual void notification(T7) = 0;
|
||||
virtual void notification(T8) = 0;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
@ -376,18 +388,24 @@ namespace etl
|
||||
typename T5,
|
||||
typename T6,
|
||||
typename T7>
|
||||
class observer<T1, T2, T3, T4, T5, T6, 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() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
virtual void notification(T3) = 0;
|
||||
virtual void notification(T4) = 0;
|
||||
virtual void notification(T5) = 0;
|
||||
virtual void notification(T6) = 0;
|
||||
virtual void notification(T7) = 0;
|
||||
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;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
@ -400,17 +418,22 @@ namespace etl
|
||||
typename T4,
|
||||
typename T5,
|
||||
typename T6>
|
||||
class observer<T1, T2, T3, T4, T5, 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() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
virtual void notification(T3) = 0;
|
||||
virtual void notification(T4) = 0;
|
||||
virtual void notification(T5) = 0;
|
||||
virtual void notification(T6) = 0;
|
||||
using observer<T1>::notification;
|
||||
using observer<T2>::notification;
|
||||
using observer<T3>::notification;
|
||||
using observer<T4>::notification;
|
||||
using observer<T5>::notification;
|
||||
using observer<T6>::notification;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
@ -422,16 +445,20 @@ namespace etl
|
||||
typename T3,
|
||||
typename T4,
|
||||
typename T5>
|
||||
class observer<T1, T2, T3, T4, 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() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
virtual void notification(T3) = 0;
|
||||
virtual void notification(T4) = 0;
|
||||
virtual void notification(T5) = 0;
|
||||
using observer<T1>::notification;
|
||||
using observer<T2>::notification;
|
||||
using observer<T3>::notification;
|
||||
using observer<T4>::notification;
|
||||
using observer<T5>::notification;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
@ -442,15 +469,18 @@ namespace etl
|
||||
typename T2,
|
||||
typename T3,
|
||||
typename T4>
|
||||
class observer<T1, T2, T3, T4>
|
||||
class observer<T1, T2, T3, T4> : public observer<T1>
|
||||
, public observer<T2>
|
||||
, public observer<T3>
|
||||
, public observer<T4>
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~observer() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
virtual void notification(T3) = 0;
|
||||
virtual void notification(T4) = 0;
|
||||
using observer<T1>::notification;
|
||||
using observer<T2>::notification;
|
||||
using observer<T3>::notification;
|
||||
using observer<T4>::notification;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
@ -460,14 +490,16 @@ namespace etl
|
||||
template <typename T1,
|
||||
typename T2,
|
||||
typename T3>
|
||||
class observer<T1, T2, T3>
|
||||
class observer<T1, T2, T3> : public observer<T1>
|
||||
, public observer<T2>
|
||||
, public observer<T3>
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~observer() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
virtual void notification(T3) = 0;
|
||||
using observer<T1>::notification;
|
||||
using observer<T2>::notification;
|
||||
using observer<T3>::notification;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
@ -476,13 +508,14 @@ namespace etl
|
||||
//*********************************************************************
|
||||
template <typename T1,
|
||||
typename T2>
|
||||
class observer<T1, T2>
|
||||
class observer<T1, T2> : public observer<T1>
|
||||
, public observer<T2>
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~observer() {}
|
||||
virtual void notification(T1) = 0;
|
||||
virtual void notification(T2) = 0;
|
||||
using observer<T1>::notification;
|
||||
using observer<T2>::notification;
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
|
||||
@ -70,17 +70,10 @@ namespace
|
||||
//*****************************************************************************
|
||||
typedef etl::observer<Notification1, Notification2&, const Notification3&> ObserverType;
|
||||
|
||||
#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
|
||||
//*****************************************************************************
|
||||
// The observer base type that does not take a notification type.
|
||||
//*****************************************************************************
|
||||
typedef etl::observer<void, int> ObserverVoidIntType;
|
||||
#else
|
||||
//*****************************************************************************
|
||||
// The observer base type that does not take a notification type.
|
||||
//*****************************************************************************
|
||||
typedef etl::observer<void> ObserverVoidType;
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
@ -122,7 +115,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
|
||||
//*****************************************************************************
|
||||
// The concrete observable 3 class.
|
||||
//*****************************************************************************
|
||||
@ -146,23 +138,6 @@ public:
|
||||
notify_observers(n);
|
||||
}
|
||||
};
|
||||
#else
|
||||
//*****************************************************************************
|
||||
// The concrete observable 3 class.
|
||||
//*****************************************************************************
|
||||
class ObservableVoid : public etl::observable<ObserverVoidType, 2>
|
||||
{
|
||||
public:
|
||||
|
||||
//*********************************
|
||||
// Notify all of the observers.
|
||||
//*********************************
|
||||
void send_notifications()
|
||||
{
|
||||
notify_observers();
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
// The first observer type.
|
||||
@ -254,7 +229,6 @@ public:
|
||||
int data3_count;
|
||||
};
|
||||
|
||||
#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
|
||||
//*****************************************************************************
|
||||
// The third observer type.
|
||||
// If any one of the overloads is missing or a parameter declaration is incorrect
|
||||
@ -266,6 +240,7 @@ public:
|
||||
|
||||
ObserverVoidInt()
|
||||
: data1_count(0)
|
||||
, data2_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
@ -288,33 +263,6 @@ public:
|
||||
int data1_count;
|
||||
int data2_count;
|
||||
};
|
||||
#else
|
||||
//*****************************************************************************
|
||||
// The third observer type.
|
||||
// If any one of the overloads is missing or a parameter declaration is incorrect
|
||||
// then the class will be 'abstract' and will not compile.
|
||||
//*****************************************************************************
|
||||
class ObserverVoid : public ObserverVoidType
|
||||
{
|
||||
public:
|
||||
|
||||
ObserverVoid()
|
||||
: data1_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// Notification1
|
||||
//*******************************************
|
||||
void notification() override
|
||||
{
|
||||
++data1_count;
|
||||
}
|
||||
|
||||
int data1_count;
|
||||
int data2_count;
|
||||
};
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
@ -603,7 +551,6 @@ namespace
|
||||
CHECK_EQUAL(0UL, observable.number_of_observers());
|
||||
}
|
||||
|
||||
#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
|
||||
//*************************************************************************
|
||||
TEST(test_void_int_observable)
|
||||
{
|
||||
@ -617,24 +564,13 @@ namespace
|
||||
|
||||
// Send the notifications.
|
||||
observable.send_notifications();
|
||||
CHECK_EQUAL(1U, observer.data1_count);
|
||||
CHECK_EQUAL(0U, observer.data2_count);
|
||||
|
||||
observable.send_notifications(1);
|
||||
CHECK_EQUAL(1U, observer.data1_count);
|
||||
CHECK_EQUAL(1U, observer.data2_count);
|
||||
}
|
||||
#else
|
||||
//*************************************************************************
|
||||
TEST(test_void_observable)
|
||||
{
|
||||
// The observable objects.
|
||||
ObservableVoid observable;
|
||||
|
||||
// The observer objects.
|
||||
ObserverVoid observer;
|
||||
|
||||
observable.add_observer(observer);
|
||||
|
||||
// Send the notifications.
|
||||
observable.send_notifications();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user