diff --git a/include/eventpp/eventqueue.h b/include/eventpp/eventqueue.h index 80c7700..507b013 100644 --- a/include/eventpp/eventqueue.h +++ b/include/eventpp/eventqueue.h @@ -155,7 +155,7 @@ public: } template - auto enqueue(A ...args) -> typename std::enable_if::type + auto enqueue(A && ...args) -> typename std::enable_if::type { static_assert(super::ArgumentPassingMode::canIncludeEventType, "Enqueuing arguments count doesn't match required (Event type should be included)."); @@ -172,7 +172,7 @@ public: } template - auto enqueue(T && first, A ...args) -> typename std::enable_if::type + auto enqueue(T && first, A && ...args) -> typename std::enable_if::type { static_assert(super::ArgumentPassingMode::canExcludeEventType, "Enqueuing arguments count doesn't match required (Event type should NOT be included)."); diff --git a/readme.md b/readme.md index 4d9ab13..e352807 100644 --- a/readme.md +++ b/readme.md @@ -308,10 +308,11 @@ Added CallbackList, EventDispatcher, EventQueue, CounterRemover, ConditionalRemo MH2033MH2033 zhllxtzhllxt marsCatXdumarsCatXdu +ChaojimengnanChaojimengnan -I (wqking) would like to sincerely thank all participants for the contributions. Your contributions makes `eventpp` better and bright future. +I (wqking) would like to sincerely thank all participants for the contributions. Your contributions make `eventpp` better and bright future. I maintain the contributors list manually, according to the criteria below, 1. Your Pull Request is approved and merged to any branch. diff --git a/tests/unittest/test_queue_basic.cpp b/tests/unittest/test_queue_basic.cpp index 0c0fe87..ca3aacd 100644 --- a/tests/unittest/test_queue_basic.cpp +++ b/tests/unittest/test_queue_basic.cpp @@ -344,6 +344,48 @@ TEST_CASE("EventQueue, non-copyable but movable unique_ptr") } } +TEST_CASE("EventQueue, copyable event object") +{ + struct MyCopyable { + MyCopyable(int * counter, const int value) : counter(counter), value(value) { + } + + MyCopyable(const MyCopyable & other) : counter(other.counter), value(other.value) { + ++*counter; + } + + MyCopyable(MyCopyable && other) : counter(other.counter), value(other.value) { + } + + MyCopyable & operator = (const MyCopyable & other) { + counter = other.counter; + value = other.value; + ++*counter; + return *this; + } + + int * counter; + int value; + }; + + using EQ = eventpp::EventQueue; + EQ queue; + + // This is to ensure changing EventQueue::enqueue(A ...args) to EventQueue::enqueue(A && ...args) works properly + SECTION("For enqueue by r-value, there should be no copy/assignment") { + int copiedCount = 0; + queue.appendListener(3, [&copiedCount](const MyCopyable & object) { + REQUIRE(*object.counter == 0); + REQUIRE(copiedCount == 0); + REQUIRE(object.value == 5); + }); + + queue.enqueue(3, MyCopyable(&copiedCount, 5)); + queue.process(); + REQUIRE(copiedCount == 0); + } +} + TEST_CASE("EventQueue, peekEvent/takeEvent/dispatch") { using SP = std::shared_ptr;