Rename event to trigger

dev
mbandic 5 months ago
parent 22dbfe89f1
commit e3eddf006b
  1. 26
      README.md
  2. 34
      lib/trigger.hpp
  3. 104
      test/test.cpp

@ -17,7 +17,7 @@ The asynchronous filesystem is provided solely to guide users on how to wrap any
- Asynchronous programming - Asynchronous programming
- Multithread - Multithread
- Asynchronous timer functions: periodic, delayed (like setInterval and setTimeout from JS) - 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 - Event loops
- Multiple parallel execution loops - Multiple parallel execution loops
- Asynchronous file IO - 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 #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/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/timers.hpp" // periodic, delayed (like setInterval and setTimeout from JS)
#include "asynco/lib/filesystem.hpp" // for async read and write files #include "asynco/lib/filesystem.hpp" // for async read and write files
using namespace marcelb; using namespace marcelb;
using namespace asynco; using namespace asynco;
using namespace events; using namespace triggers;
// At the end of the main function, always set // At the end of the main function, always set
_asynco_engine.run(); _asynco_engine.run();
@ -189,9 +189,9 @@ Events
* initialization of typed events * initialization of typed events
*/ */
event<int, int> ev2int; trigger<int, int> ev2int;
event<int, string> evintString; trigger<int, string> evintString;
event<> evoid; trigger<> evoid;
ev2int.on("sum", [](int a, int b) { ev2int.on("sum", [](int a, int b) {
cout << "Sum " << a+b << endl; cout << "Sum " << a+b << endl;
@ -219,32 +219,32 @@ sleep(1);
* Emit * Emit
*/ */
ev2int.emit("sum", 5, 8); ev2int.tick("sum", 5, 8);
sleep(1); sleep(1);
evintString.emit("substract", 3, to_string(2)); evintString.tick("substract", 3, to_string(2));
sleep(1); sleep(1);
evoid.emit("void"); evoid.tick("void");
// Turn off the event listener // Turn off the event listener
evoid.off("void"); evoid.off("void");
evoid.emit("void"); // nothing is happening evoid.tick("void"); // nothing is happening
``` ```
Extend own class whit events Extend own class whit events
```c++ ```c++
class myOwnClass : public event<int> { class myOwnClass : public trigger<int> {
public: public:
myOwnClass() : event() {}; myOwnClass() : trigger() {};
}; };
myOwnClass myclass; myOwnClass myclass;
delayed t( [&] { delayed t( [&] {
myclass.emit("constructed", 1); myclass.tick("constructed", 1);
}, 200); }, 200);
myclass.on("constructed", [] (int i) { myclass.on("constructed", [] (int i) {

@ -1,5 +1,5 @@
#ifndef _EVENT_ #ifndef _TRIGGER_
#define _EVENT_ #define _TRIGGER_
#include <map> #include <map>
#include <vector> #include <vector>
@ -11,17 +11,17 @@ using namespace std;
#include "asynco.hpp" #include "asynco.hpp"
namespace marcelb { namespace marcelb {
namespace asynco { 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 * These events are typed according to the arguments of the callback function
*/ */
template<typename... T> template<typename... T>
class event { class trigger {
private: private:
mutex m_eve; mutex m_eve;
unordered_map<string, vector<function<void(T...)>>> events; unordered_map<string, vector<function<void(T...)>>> triggers;
public: public:
@ -30,16 +30,16 @@ class event {
*/ */
void on(const string& key, function<void(T...)> callback) { void on(const string& key, function<void(T...)> callback) {
lock_guard _off(m_eve); 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 * It emits an event and sends a callback function saved according to the key with the passed parameters
*/ */
template<typename... Args> template<typename... Args>
void emit(const string& key, Args... args) { void tick(const string& key, Args... args) {
auto it_eve = events.find(key); auto it_eve = triggers.find(key);
if (it_eve != events.end()) { if (it_eve != triggers.end()) {
for (uint i =0; i<it_eve->second.size(); i++) { for (uint i =0; i<it_eve->second.size(); i++) {
auto callback = bind(it_eve->second[i], forward<Args>(args)...); auto callback = bind(it_eve->second[i], forward<Args>(args)...);
atask(callback); 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) { void off(const string& key) {
lock_guard _off(m_eve); lock_guard _off(m_eve);
events.erase(key); triggers.erase(key);
} }
/** /**
* Remove all event listener * Remove all trigger listener
*/ */
void off() { void off() {
lock_guard _off(m_eve); 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) { 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() {
unsigned int listeners = 0; unsigned int listeners = 0;
for (auto& ev : events) { for (auto& ev : triggers) {
listeners += ev.second.size(); listeners += ev.second.size();
} }
return listeners; return listeners;

@ -1,12 +1,12 @@
// // #define NUM_OF_RUNNERS 2 // // #define NUM_OF_RUNNERS 2
#include "../lib/asynco.hpp" #include "../lib/asynco.hpp"
#include "../lib/event.hpp" #include "../lib/trigger.hpp"
#include "../lib/filesystem.hpp" #include "../lib/filesystem.hpp"
#include "../lib/timers.hpp" #include "../lib/timers.hpp"
using namespace marcelb::asynco; using namespace marcelb::asynco;
using namespace events; using namespace triggers;
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
@ -53,9 +53,9 @@ class clm {
// ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- // ------------------ EXTEND OWN CLASS WITH EVENTS -------------------
class myOwnClass : public event<int> { class myOwnClass : public trigger<int> {
public: public:
myOwnClass() : event() {}; myOwnClass() : trigger() {};
}; };
@ -216,74 +216,74 @@ int main () {
// }); // });
// }); // });
// // --------------- EVENTS ------------------- // --------------- EVENTS -------------------
// /** /**
// * initialization of typed events * initialization of typed events
// */ */
// event<int, int> ev2int; trigger<int, int> ev2int;
// event<int, string> evintString; trigger<int, string> evintString;
// event<> evoid; trigger<> evoid;
// ev2int.on("sum", [](int a, int b) { ev2int.on("sum", [](int a, int b) {
// cout << "Sum " << a+b << endl; cout << "Sum " << a+b << endl;
// }); });
// ev2int.on("sum", [](int a, int b) { ev2int.on("sum", [](int a, int b) {
// cout << "Sum done" << endl; cout << "Sum done" << endl;
// }); });
// evintString.on("substract", [](int a, string b) { evintString.on("substract", [](int a, string b) {
// cout << "Substract " << a-stoi(b) << endl; cout << "Substract " << a-stoi(b) << endl;
// }); });
// evoid.on("void", []() { evoid.on("void", []() {
// cout << "Void emited" << endl; cout << "Void emited" << endl;
// }); });
// string emited2 = "2"; string emited2 = "2";
// evoid.on("void", [&]() { evoid.on("void", [&]() {
// cout << "Void emited " << emited2 << endl; cout << "Void emited " << emited2 << endl;
// }); });
// evoid.emit("void"); evoid.tick("void");
// sleep(1); sleep(1);
// /** /**
// * Emit * Emit
// */ */
// ev2int.emit("sum", 5, 8); ev2int.tick("sum", 5, 8);
// sleep(1); sleep(1);
// evintString.emit("substract", 3, to_string(2)); evintString.tick("substract", 3, to_string(2));
// sleep(1); sleep(1);
// evoid.off("void"); evoid.off("void");
// evoid.emit("void"); evoid.tick("void");
// cout << "Ukupno 2 int " << ev2int.listeners() << endl; cout << "Ukupno 2 int " << ev2int.listeners() << endl;
// cout << "Ukupno evintString " << evintString.listeners() << endl; cout << "Ukupno evintString " << evintString.listeners() << endl;
// cout << "Ukupno evoid " << evoid.listeners() << endl; cout << "Ukupno evoid " << evoid.listeners() << endl;
// cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl; cout << "Ukupno 2 int " << ev2int.listeners("sum") << endl;
// /** /**
// * Own class * Own class
// */ */
// myOwnClass myclass; myOwnClass myclass;
// delayed t( [&] { delayed t( [&] {
// myclass.emit("constructed", 1); myclass.tick("constructed", 1);
// }, 200); }, 200);
// myclass.on("constructed", [] (int i) { myclass.on("constructed", [] (int i) {
// cout << "Constructed " << i << endl; cout << "Constructed " << i << endl;
// }); });

Loading…
Cancel
Save