From 73395fe6f4cab9463d3bcba77ef81b40e4000407 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 2 Aug 2024 10:36:13 +0100 Subject: [PATCH] Updated C++03 observer class to accept void parameter notification types --- include/etl/observer.h | 125 ++++++++++++++++++++++++++--------------- test/test_observer.cpp | 76 ++----------------------- 2 files changed, 85 insertions(+), 116 deletions(-) diff --git a/include/etl/observer.h b/include/etl/observer.h index 32eee70ec..eda17dfa7 100644 --- a/include/etl/observer.h +++ b/include/etl/observer.h @@ -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 + class observer; //***************************************************************** /// The observer class for N types. ///\ingroup observer //***************************************************************** - template - class observer : public observer, public observer + template + class observer : public observer, public observer { public: + ETL_STATIC_ASSERT((!etl::has_duplicates::value), "Observer has duplicate notification types"); + using observer::notification; - using observer::notification; + using observer::notification; }; //***************************************************************** @@ -351,18 +355,26 @@ namespace etl typename T6 = void, typename T7 = void, typename T8 = void> - class observer + class observer : public observer + , public observer + , public observer + , public observer + , public observer + , public observer + , public observer + , public observer { 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::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; }; //********************************************************************* @@ -376,18 +388,24 @@ namespace etl typename T5, typename T6, typename T7> - class observer + class observer : public observer + , public observer + , public observer + , public observer + , public observer + , public observer + , public observer { 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::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; }; //********************************************************************* @@ -400,17 +418,22 @@ namespace etl typename T4, typename T5, typename T6> - class observer + class observer : public observer + , public observer + , public observer + , public observer + , public observer + , public observer { 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::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; }; //********************************************************************* @@ -422,16 +445,20 @@ namespace etl typename T3, typename T4, typename T5> - class observer + class observer : public observer + , public observer + , public observer + , public observer + , public observer { 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::notification; + using observer::notification; + using observer::notification; + using observer::notification; + using observer::notification; }; //********************************************************************* @@ -442,15 +469,18 @@ namespace etl typename T2, typename T3, typename T4> - class observer + class observer : public observer + , public observer + , public observer + , public observer { 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::notification; + using observer::notification; + using observer::notification; + using observer::notification; }; //********************************************************************* @@ -460,14 +490,16 @@ namespace etl template - class observer + class observer : public observer + , public observer + , public observer { public: virtual ~observer() {} - virtual void notification(T1) = 0; - virtual void notification(T2) = 0; - virtual void notification(T3) = 0; + using observer::notification; + using observer::notification; + using observer::notification; }; //********************************************************************* @@ -476,13 +508,14 @@ namespace etl //********************************************************************* template - class observer + class observer : public observer + , public observer { public: virtual ~observer() {} - virtual void notification(T1) = 0; - virtual void notification(T2) = 0; + using observer::notification; + using observer::notification; }; //********************************************************************* diff --git a/test/test_observer.cpp b/test/test_observer.cpp index aff048f07..bf08e17b9 100644 --- a/test/test_observer.cpp +++ b/test/test_observer.cpp @@ -70,17 +70,10 @@ namespace //***************************************************************************** typedef etl::observer ObserverType; -#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION) //***************************************************************************** // The observer base type that does not take a notification type. //***************************************************************************** typedef etl::observer ObserverVoidIntType; -#else - //***************************************************************************** - // The observer base type that does not take a notification type. - //***************************************************************************** - typedef etl::observer ObserverVoidType; -#endif } //***************************************************************************** @@ -122,7 +115,6 @@ class Observable2 : public etl::observable } }; -#if !defined(ETL_OBSERVER_FORCE_CPP03_IMPLEMENTATION) //***************************************************************************** // The concrete observable 3 class. //***************************************************************************** @@ -146,23 +138,6 @@ class ObservableVoidInt : public etl::observable notify_observers(n); } }; -#else -//***************************************************************************** -// The concrete observable 3 class. -//***************************************************************************** -class ObservableVoid : public etl::observable -{ -public: - - //********************************* - // Notify all of the observers. - //********************************* - void send_notifications() - { - notify_observers(); - } -}; -#endif //***************************************************************************** // The first observer type. @@ -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 @@ -266,6 +240,7 @@ class ObserverVoidInt : public ObserverVoidIntType ObserverVoidInt() : data1_count(0) + , data2_count(0) { } @@ -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 { @@ -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(); - 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 } }