From eb8cdee237562e954e8c87c5eb9999cee9293e49 Mon Sep 17 00:00:00 2001 From: marcelb Date: Sun, 24 Nov 2024 20:26:13 +0100 Subject: [PATCH] Example of implement multi trigger in class --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/README.md b/README.md index 08991a9..900a4b9 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,55 @@ myclass.on("constructed", [] (int i) { ``` +Implementing a class with multiple triggers of different types + +```c++ + +class ClassWithTriggers { + trigger emitter1; + trigger emitter2; + +public: + template + void on(const string& key, function callback) { + if constexpr (sizeof...(T) == 1 && is_same_v>, int>) { + emitter1.on(key, callback); + } + else if constexpr (sizeof...(T) == 1 && is_same_v>, string>) { + emitter2.on(key, callback); + } + } + + template + void tick(const string& key, Args&&... args) { + if constexpr (sizeof...(Args) == 1 && is_same_v>, int>) { + emitter1.tick(key, forward(args)...); + } + else if constexpr (sizeof...(Args) == 1 && is_same_v>, string>) { + emitter2.tick(key, forward(args)...); + } + else { + static_assert(sizeof...(Args) == 0, "Unsupported number or types of arguments"); + } + } +}; + +ClassWithTriggers mt; + +mt.on("int", function([&](int i) { + cout << "Emit int " << i << endl; +})); + +mt.on("string", function([&](string s) { + cout << "Emit string " << s << endl; +})); + +mt.tick("int", 5); +mt.tick("string", string("Hello world")); + +``` + + Asynchronous file IO ```c++ diff --git a/test/test.cpp b/test/test.cpp index 779dd37..43e284f 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -62,6 +62,37 @@ class myOwnClass : public trigger { myOwnClass() : trigger() {}; }; +// ----------------- MULTIPLE TRIGGERS IN ONE CLASS ------------------ + +class ClassWithTriggers { + trigger emitter1; + trigger emitter2; + +public: + template + void on(const string& key, function callback) { + if constexpr (sizeof...(T) == 1 && is_same_v>, int>) { + emitter1.on(key, callback); + } + else if constexpr (sizeof...(T) == 1 && is_same_v>, string>) { + emitter2.on(key, callback); + } + } + + template + void tick(const string& key, Args&&... args) { + if constexpr (sizeof...(Args) == 1 && is_same_v>, int>) { + emitter1.tick(key, forward(args)...); + } + else if constexpr (sizeof...(Args) == 1 && is_same_v>, string>) { + emitter2.tick(key, forward(args)...); + } + else { + static_assert(sizeof...(Args) == 0, "Unsupported number or types of arguments"); + } + } +}; + int main () { @@ -367,6 +398,24 @@ int main () { // cout << "Constructed " << i << endl; // }); + // /** + // * + // * Use class with multiple triggers + // * + // */ + + ClassWithTriggers mt; + + mt.on("int", function([&](int i) { + cout << "Emit int " << i << endl; + })); + + mt.on("string", function([&](string s) { + cout << "Emit string " << s << endl; + })); + + mt.tick("int", 5); + mt.tick("string", string("Hello world")); // auto status = fs::read("test1.txt");