From 60761df5e3976a276ef20313e72b9564c489d388 Mon Sep 17 00:00:00 2001 From: wqking Date: Sun, 17 Dec 2023 14:19:34 +0800 Subject: [PATCH] Fix compile error when using Heter classes with CounterRemover, #68 --- include/eventpp/utilities/counterremover.h | 7 +++++-- readme.md | 1 + tests/unittest/test_counterremover.cpp | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/eventpp/utilities/counterremover.h b/include/eventpp/utilities/counterremover.h index 1cc6542..6ab51ce 100644 --- a/include/eventpp/utilities/counterremover.h +++ b/include/eventpp/utilities/counterremover.h @@ -15,6 +15,7 @@ #define COUNTERREMOVER_H_211532679833 #include "../eventpolicies.h" +#include "../internal/typeutil_i.h" namespace eventpp { @@ -41,7 +42,8 @@ private: }; template - void operator() (Args && ...args) const { + auto operator() (Args && ...args) const + -> typename std::enable_if::value, void>::type { if(--data->triggerCount <= 0) { data->dispatcher.removeListener(data->event, data->handle); } @@ -123,7 +125,8 @@ private: }; template - void operator() (Args && ...args) const { + auto operator() (Args && ...args) const + -> typename std::enable_if::value, void>::type { if(--data->triggerCount <= 0) { data->callbackList.remove(data->handle); } diff --git a/readme.md b/readme.md index 3bab2e5..ccae83a 100644 --- a/readme.md +++ b/readme.md @@ -320,6 +320,7 @@ Added CallbackList, EventDispatcher, EventQueue, CounterRemover, ConditionalRemo henaiguohenaiguo sr-treamsr-tream Drise13Drise13 +iamzoneiamzone diff --git a/tests/unittest/test_counterremover.cpp b/tests/unittest/test_counterremover.cpp index 2f82de7..4c610a6 100644 --- a/tests/unittest/test_counterremover.cpp +++ b/tests/unittest/test_counterremover.cpp @@ -162,7 +162,7 @@ TEST_CASE("CounterRemover, CallbackList, has parameters") TEST_CASE("CounterRemover, HeterEventDispatcher") { - eventpp::HeterEventDispatcher > dispatcher; + eventpp::HeterEventDispatcher > dispatcher; constexpr int event = 3; std::vector dataList(4); @@ -174,7 +174,7 @@ TEST_CASE("CounterRemover, HeterEventDispatcher") eventpp::counterRemover(dispatcher).prependListener(event, [&dataList]() { ++dataList[1]; }); - auto handle = eventpp::counterRemover(dispatcher).appendListener(event, [&dataList]() { + auto handle = eventpp::counterRemover(dispatcher).appendListener(event, [&dataList](int) { ++dataList[2]; }, 2); eventpp::counterRemover(dispatcher).insertListener(event, [&dataList]() { @@ -183,22 +183,27 @@ TEST_CASE("CounterRemover, HeterEventDispatcher") REQUIRE(dataList == std::vector { 0, 0, 0, 0 }); - dispatcher.dispatch(event); + dispatcher.dispatch(event); // for void() + REQUIRE(dataList == std::vector { 1, 1, 0, 1 }); + dispatcher.dispatch(event, 5); // for void(int) REQUIRE(dataList == std::vector { 1, 1, 1, 1 }); dispatcher.dispatch(event); + dispatcher.dispatch(event, 5); REQUIRE(dataList == std::vector { 2, 1, 2, 2 }); dispatcher.dispatch(event); + dispatcher.dispatch(event, 5); REQUIRE(dataList == std::vector { 3, 1, 2, 3 }); dispatcher.dispatch(event); + dispatcher.dispatch(event, 5); REQUIRE(dataList == std::vector { 4, 1, 2, 3 }); } TEST_CASE("CounterRemover, HeterCallbackList") { - eventpp::HeterCallbackList > callbackList; + eventpp::HeterCallbackList > callbackList; std::vector dataList(4); @@ -209,7 +214,7 @@ TEST_CASE("CounterRemover, HeterCallbackList") eventpp::counterRemover(callbackList).prepend([&dataList]() { ++dataList[1]; }); - auto handle = eventpp::counterRemover(callbackList).append([&dataList]() { + auto handle = eventpp::counterRemover(callbackList).append([&dataList](int) { ++dataList[2]; }, 2); eventpp::counterRemover(callbackList).insert([&dataList]() { @@ -219,15 +224,20 @@ TEST_CASE("CounterRemover, HeterCallbackList") REQUIRE(dataList == std::vector { 0, 0, 0, 0 }); callbackList(); + REQUIRE(dataList == std::vector { 1, 1, 0, 1 }); + callbackList(5); REQUIRE(dataList == std::vector { 1, 1, 1, 1 }); callbackList(); + callbackList(5); REQUIRE(dataList == std::vector { 2, 1, 2, 2 }); callbackList(); + callbackList(5); REQUIRE(dataList == std::vector { 3, 1, 2, 3 }); callbackList(); + callbackList(5); REQUIRE(dataList == std::vector { 4, 1, 2, 3 }); }