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
- 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<int, int> ev2int;
event<int, string> evintString;
event<> evoid;
trigger<int, int> ev2int;
trigger<int, string> 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<int> {
class myOwnClass : public trigger<int> {
public:
myOwnClass() : event() {};
myOwnClass() : trigger() {};
};
myOwnClass myclass;
delayed t( [&] {
myclass.emit("constructed", 1);
myclass.tick("constructed", 1);
}, 200);
myclass.on("constructed", [] (int i) {

@ -1,5 +1,5 @@
#ifndef _EVENT_
#define _EVENT_
#ifndef _TRIGGER_
#define _TRIGGER_
#include <map>
#include <vector>
@ -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<typename... T>
class event {
class trigger {
private:
mutex m_eve;
unordered_map<string, vector<function<void(T...)>>> events;
unordered_map<string, vector<function<void(T...)>>> triggers;
public:
@ -30,16 +30,16 @@ class event {
*/
void on(const string& key, function<void(T...)> 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<typename... Args>
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; i<it_eve->second.size(); i++) {
auto callback = bind(it_eve->second[i], forward<Args>(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;

@ -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 <iostream>
#include <unistd.h>
@ -53,9 +53,9 @@ class clm {
// ------------------ EXTEND OWN CLASS WITH EVENTS -------------------
class myOwnClass : public event<int> {
class myOwnClass : public trigger<int> {
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;
// event<int, string> evintString;
// event<> evoid;
trigger<int, int> ev2int;
trigger<int, string> 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;
});

Loading…
Cancel
Save