Skip to content

Commit

Permalink
Updated C++03 observer class to accept void parameter notification types
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Aug 2, 2024
1 parent f219e86 commit 73395fe
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 116 deletions.
125 changes: 79 additions & 46 deletions include/etl/observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

//*****************************************************************
Expand Down Expand Up @@ -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;
};

//*********************************************************************
Expand All @@ -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;
};

//*********************************************************************
Expand All @@ -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;
};

//*********************************************************************
Expand All @@ -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;
};

//*********************************************************************
Expand All @@ -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;
};

//*********************************************************************
Expand All @@ -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;
};

//*********************************************************************
Expand All @@ -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;
};

//*********************************************************************
Expand Down
76 changes: 6 additions & 70 deletions test/test_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

//*****************************************************************************
Expand Down Expand Up @@ -122,7 +115,6 @@ class Observable2 : public etl::observable<ObserverType, 2>
}
};

#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
//*****************************************************************************
// The concrete observable 3 class.
//*****************************************************************************
Expand All @@ -146,23 +138,6 @@ class ObservableVoidInt : public etl::observable<ObserverVoidIntType, 2>
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.
Expand Down Expand Up @@ -254,7 +229,6 @@ class Observer2 : public ObserverType
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
Expand All @@ -266,6 +240,7 @@ class ObserverVoidInt : public ObserverVoidIntType

ObserverVoidInt()
: data1_count(0)
, data2_count(0)
{
}

Expand All @@ -288,33 +263,6 @@ class ObserverVoidInt : public ObserverVoidIntType
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
{
Expand Down Expand Up @@ -603,7 +551,6 @@ namespace
CHECK_EQUAL(0UL, observable.number_of_observers());
}

#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
TEST(test_void_int_observable)
{
Expand All @@ -617,24 +564,13 @@ namespace

// Send the notifications.
observable.send_notifications();
observable.send_notifications(1);
}
#else
//*************************************************************************
TEST(test_void_observable)
{
// The observable objects.
ObservableVoid observable;
CHECK_EQUAL(1U, observer.data1_count);
CHECK_EQUAL(0U, observer.data2_count);

// The observer objects.
ObserverVoid observer;

observable.add_observer(observer);

// Send the notifications.
observable.send_notifications();
observable.send_notifications(1);
CHECK_EQUAL(1U, observer.data1_count);
CHECK_EQUAL(1U, observer.data2_count);
}
#endif
}
}

0 comments on commit 73395fe

Please sign in to comment.