From e3eddf006b312b66a69e9b67f2d62d26fbc71a7d Mon Sep 17 00:00:00 2001 From: mbandic Date: Fri, 26 Apr 2024 09:58:45 +0000 Subject: [PATCH] Rename event to trigger --- README.md | 26 ++++----- lib/{event.hpp => trigger.hpp} | 34 +++++------ test/test.cpp | 104 ++++++++++++++++----------------- 3 files changed, 82 insertions(+), 82 deletions(-) rename lib/{event.hpp => trigger.hpp} (67%) diff --git a/README.md b/README.md index 4101fe9..40497df 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ The asynchronous filesystem is provided solely to guide users on how to wrap any - Asynchronous programming - Multithread - Asynchronous timer functions: periodic, delayed (like setInterval and setTimeout from JS) -- Typed events (on, emit, off) +- Typed events (on, tick, off) (like EventEmitter from JS: on, emit, etc) - Event loops - Multiple parallel execution loops - Asynchronous file IO @@ -30,13 +30,13 @@ Just download the latest release and unzip it into your project. #define NUM_OF_RUNNERS 8 // To change the number of threads used by atask, without this it runs according to the number of cores #include "asynco/lib/asynco.hpp" // atask(), wait() -#include "asynco/lib/event.hpp" // event +#include "asynco/lib/triggers.hpp" // trigger (event emitter) #include "asynco/lib/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS) #include "asynco/lib/filesystem.hpp" // for async read and write files using namespace marcelb; using namespace asynco; -using namespace events; +using namespace triggers; // At the end of the main function, always set _asynco_engine.run(); @@ -189,9 +189,9 @@ Events * initialization of typed events */ -event ev2int; -event evintString; -event<> evoid; +trigger ev2int; +trigger evintString; +trigger<> evoid; ev2int.on("sum", [](int a, int b) { cout << "Sum " << a+b << endl; @@ -219,32 +219,32 @@ sleep(1); * Emit */ -ev2int.emit("sum", 5, 8); +ev2int.tick("sum", 5, 8); sleep(1); -evintString.emit("substract", 3, to_string(2)); +evintString.tick("substract", 3, to_string(2)); sleep(1); -evoid.emit("void"); +evoid.tick("void"); // Turn off the event listener evoid.off("void"); -evoid.emit("void"); // nothing is happening +evoid.tick("void"); // nothing is happening ``` Extend own class whit events ```c++ -class myOwnClass : public event { +class myOwnClass : public trigger { public: - myOwnClass() : event() {}; + myOwnClass() : trigger() {}; }; myOwnClass myclass; delayed t( [&] { - myclass.emit("constructed", 1); + myclass.tick("constructed", 1); }, 200); myclass.on("constructed", [] (int i) { diff --git a/lib/event.hpp b/lib/trigger.hpp similarity index 67% rename from lib/event.hpp rename to lib/trigger.hpp index 02430d7..13ce96a 100644 --- a/lib/event.hpp +++ b/lib/trigger.hpp @@ -1,5 +1,5 @@ -#ifndef _EVENT_ -#define _EVENT_ +#ifndef _TRIGGER_ +#define _TRIGGER_ #include #include @@ -11,17 +11,17 @@ using namespace std; #include "asynco.hpp" namespace marcelb { namespace asynco { -namespace events { +namespace triggers { /** - * Event class, for event-driven programming. + * trigger class, for event-driven programming. * These events are typed according to the arguments of the callback function */ template -class event { +class trigger { private: mutex m_eve; - unordered_map>> events; + unordered_map>> triggers; public: @@ -30,16 +30,16 @@ class event { */ void on(const string& key, function callback) { lock_guard _off(m_eve); - events[key].push_back(callback); + triggers[key].push_back(callback); } /** * It emits an event and sends a callback function saved according to the key with the passed parameters */ template - void emit(const string& key, Args... args) { - auto it_eve = events.find(key); - if (it_eve != events.end()) { + void tick(const string& key, Args... args) { + auto it_eve = triggers.find(key); + if (it_eve != triggers.end()) { for (uint i =0; isecond.size(); i++) { auto callback = bind(it_eve->second[i], forward(args)...); atask(callback); @@ -48,27 +48,27 @@ class event { } /** - * Remove an event listener from an event + * Remove an trigger listener from an event */ void off(const string& key) { lock_guard _off(m_eve); - events.erase(key); + triggers.erase(key); } /** - * Remove all event listener + * Remove all trigger listener */ void off() { lock_guard _off(m_eve); - events.clear(); + triggers.clear(); } /** - * Get num of listeners by an event key + * Get num of listeners by an trigger key */ unsigned int listeners(const string& key) { - return events[key].size(); + return triggers[key].size(); } /** @@ -76,7 +76,7 @@ class event { */ unsigned int listeners() { unsigned int listeners = 0; - for (auto& ev : events) { + for (auto& ev : triggers) { listeners += ev.second.size(); } return listeners; diff --git a/test/test.cpp b/test/test.cpp index eff2b52..9e33d89 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,12 +1,12 @@ // // #define NUM_OF_RUNNERS 2 #include "../lib/asynco.hpp" -#include "../lib/event.hpp" +#include "../lib/trigger.hpp" #include "../lib/filesystem.hpp" #include "../lib/timers.hpp" using namespace marcelb::asynco; -using namespace events; +using namespace triggers; #include #include @@ -53,9 +53,9 @@ class clm { // ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- -class myOwnClass : public event { +class myOwnClass : public trigger { public: - myOwnClass() : event() {}; + myOwnClass() : trigger() {}; }; @@ -216,74 +216,74 @@ int main () { // }); // }); - // // --------------- EVENTS ------------------- + // --------------- EVENTS ------------------- - // /** - // * initialization of typed events - // */ + /** + * initialization of typed events + */ - // event ev2int; - // event evintString; - // event<> evoid; + trigger ev2int; + trigger evintString; + trigger<> evoid; - // ev2int.on("sum", [](int a, int b) { - // cout << "Sum " << a+b << endl; - // }); + ev2int.on("sum", [](int a, int b) { + cout << "Sum " << a+b << endl; + }); - // ev2int.on("sum", [](int a, int b) { - // cout << "Sum done" << endl; - // }); + ev2int.on("sum", [](int a, int b) { + cout << "Sum done" << endl; + }); - // evintString.on("substract", [](int a, string b) { - // cout << "Substract " << a-stoi(b) << endl; - // }); + evintString.on("substract", [](int a, string b) { + cout << "Substract " << a-stoi(b) << endl; + }); - // evoid.on("void", []() { - // cout << "Void emited" << endl; - // }); + evoid.on("void", []() { + cout << "Void emited" << endl; + }); - // string emited2 = "2"; + string emited2 = "2"; - // evoid.on("void", [&]() { - // cout << "Void emited " << emited2 << endl; - // }); + evoid.on("void", [&]() { + cout << "Void emited " << emited2 << endl; + }); - // evoid.emit("void"); - // sleep(1); + evoid.tick("void"); + sleep(1); - // /** - // * Emit - // */ + /** + * Emit + */ - // ev2int.emit("sum", 5, 8); + ev2int.tick("sum", 5, 8); - // sleep(1); - // evintString.emit("substract", 3, to_string(2)); + sleep(1); + evintString.tick("substract", 3, to_string(2)); - // sleep(1); - // evoid.off("void"); - // evoid.emit("void"); + sleep(1); + evoid.off("void"); + evoid.tick("void"); - // cout << "Ukupno 2 int " << ev2int.listeners() << endl; - // cout << "Ukupno evintString " << evintString.listeners() << endl; - // cout << "Ukupno evoid " << evoid.listeners() << endl; - // cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl; + cout << "Ukupno 2 int " << ev2int.listeners() << endl; + cout << "Ukupno evintString " << evintString.listeners() << endl; + cout << "Ukupno evoid " << evoid.listeners() << endl; + cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl; - // /** - // * Own class - // */ + /** + * Own class + */ - // myOwnClass myclass; + myOwnClass myclass; - // delayed t( [&] { - // myclass.emit("constructed", 1); - // }, 200); + delayed t( [&] { + myclass.tick("constructed", 1); + }, 200); - // myclass.on("constructed", [] (int i) { - // cout << "Constructed " << i << endl; - // }); + myclass.on("constructed", [] (int i) { + cout << "Constructed " << i << endl; + });