aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-01-01 22:20:21 +0800
committercrupest <crupest@outlook.com>2024-01-18 21:12:28 +0800
commitdf799bf394fa63dc9f39a5b09fe21c16b1cc8a57 (patch)
treec9efa5b7a53545e2e1b7a9dff765817208911a7a
parentf79fc16b29a32c21461490a6482eada7baa9d7f8 (diff)
downloadcru-df799bf394fa63dc9f39a5b09fe21c16b1cc8a57.tar.gz
cru-df799bf394fa63dc9f39a5b09fe21c16b1cc8a57.tar.bz2
cru-df799bf394fa63dc9f39a5b09fe21c16b1cc8a57.zip
NEED TEST: fix compile errors of Event2 and create init test.
-rw-r--r--include/cru/common/Event2.h15
-rw-r--r--test/common/CMakeLists.txt1
-rw-r--r--test/common/Event2Test.cpp19
3 files changed, 28 insertions, 7 deletions
diff --git a/include/cru/common/Event2.h b/include/cru/common/Event2.h
index 5c3a330a..7d7bda82 100644
--- a/include/cru/common/Event2.h
+++ b/include/cru/common/Event2.h
@@ -51,7 +51,8 @@ class EventHandlerToken {
static_assert(is_event2_v<TEvent2>, "TEvent2 must be Event2 class.");
public:
- EventHandlerToken(ObjectResolver<TEvent2> event_resolver, int token_value);
+ EventHandlerToken(ObjectResolver<TEvent2> event_resolver, int token_value)
+ : event_resolver_(std::move(event_resolver)), token_value_(token_value) {}
ObjectResolver<TEvent2> GetEventResolver() const { return event_resolver_; }
int GetTokenValue() const { return token_value_; }
@@ -82,17 +83,17 @@ class Event2 : public SelfResolvable<Event2<TArgument, TResult>> {
public:
using HandlerToken = EventHandlerToken<Event2>;
using Context = EventContext<TArgument, TResult>;
- using Handler = std::function<void(Context&)>;
+ using Handler = std::function<void(Context*)>;
using SpyOnlyHandler = std::function<void()>;
template <typename TFunc>
static std::enable_if_t<std::invocable<TFunc>, Handler> WrapAsHandler(
TFunc&& handler) {
- return Handler([h = std::forward<TFunc>(handler)](Context&) { h(); });
+ return Handler([h = std::forward<TFunc>(handler)](Context*) { h(); });
}
template <typename TFunc>
- static std::enable_if_t<std::invocable<TFunc, Context&>, Handler>
+ static std::enable_if_t<std::invocable<TFunc, Context*>, Handler>
WrapAsHandler(TFunc&& handler) {
return Handler(std::forward<TFunc>(handler));
}
@@ -138,19 +139,19 @@ class Event2 : public SelfResolvable<Event2<TArgument, TResult>> {
TResult Raise() {
Context context;
- RunInContext(context);
+ RunInContext(&context);
return context.TakeResult();
}
TResult Raise(TArgument argument) {
Context context(std::move(argument));
- RunInContext(context);
+ RunInContext(&context);
return context.TakeResult();
}
TResult Raise(TArgument argument, TResult result) {
Context context(std::move(argument), std::move(result));
- RunInContext(context);
+ RunInContext(&context);
return context.TakeResult();
}
diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt
index 602af819..f2fda47d 100644
--- a/test/common/CMakeLists.txt
+++ b/test/common/CMakeLists.txt
@@ -1,4 +1,5 @@
add_executable(CruBaseTest
+ Event2Test.cpp
HandlerRegistryTest.cpp
PropertyTreeTest.cpp
SelfResolvableTest.cpp
diff --git a/test/common/Event2Test.cpp b/test/common/Event2Test.cpp
new file mode 100644
index 00000000..f63a5347
--- /dev/null
+++ b/test/common/Event2Test.cpp
@@ -0,0 +1,19 @@
+#include "cru/common/Event2.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+using cru::Event2;
+
+TEST_CASE("Event2 handlers should work.", "[event2]") {
+ Event2 event;
+
+ int counter = 0;
+
+ auto handler = [&counter] { counter++; };
+
+ event.AddHandler(handler);
+
+ event.Raise();
+
+ REQUIRE(counter == 1);
+}