|
|
@ -62,37 +62,6 @@ class myOwnClass : public trigger<int> { |
|
|
|
myOwnClass() : trigger() {}; |
|
|
|
myOwnClass() : trigger() {}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// ----------------- MULTIPLE TRIGGERS IN ONE CLASS ------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ClassWithTriggers { |
|
|
|
|
|
|
|
trigger<int> emitter1; |
|
|
|
|
|
|
|
trigger<string> emitter2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
template<typename... T> |
|
|
|
|
|
|
|
void on(const string& key, function<void(T...)> callback) { |
|
|
|
|
|
|
|
if constexpr (sizeof...(T) == 1 && is_same_v<tuple_element_t<0, tuple<T...>>, int>) { |
|
|
|
|
|
|
|
emitter1.on(key, callback); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if constexpr (sizeof...(T) == 1 && is_same_v<tuple_element_t<0, tuple<T...>>, string>) { |
|
|
|
|
|
|
|
emitter2.on(key, callback); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename... Args> |
|
|
|
|
|
|
|
void tick(const string& key, Args&&... args) { |
|
|
|
|
|
|
|
if constexpr (sizeof...(Args) == 1 && is_same_v<tuple_element_t<0, tuple<Args...>>, int>) { |
|
|
|
|
|
|
|
emitter1.tick(key, forward<Args>(args)...); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if constexpr (sizeof...(Args) == 1 && is_same_v<tuple_element_t<0, tuple<Args...>>, string>) { |
|
|
|
|
|
|
|
emitter2.tick(key, forward<Args>(args)...); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
static_assert(sizeof...(Args) == 0, "Unsupported number or types of arguments"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main () { |
|
|
|
int main () { |
|
|
|
|
|
|
|
|
|
|
@ -100,9 +69,9 @@ int main () { |
|
|
|
|
|
|
|
|
|
|
|
// --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
|
|
|
|
// --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// /**
|
|
|
|
* Init periodic and delayed; clear periodic and delayed |
|
|
|
// * Init periodic and delayed; clear periodic and delayed
|
|
|
|
*/ |
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
|
|
// periodic inter1 ([&]() {
|
|
|
|
// periodic inter1 ([&]() {
|
|
|
|
// cout << "periodic prvi " << rtime_ms() - start << endl;
|
|
|
|
// cout << "periodic prvi " << rtime_ms() - start << endl;
|
|
|
@ -160,30 +129,11 @@ int main () { |
|
|
|
// cout << "nije isteko " << endl;
|
|
|
|
// cout << "nije isteko " << endl;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// auto d = Delayed( [](){
|
|
|
|
// // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
|
|
|
|
// cout << "Delayed" << endl;
|
|
|
|
|
|
|
|
// }, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// auto p = Periodic( [](){
|
|
|
|
|
|
|
|
// cout << "Periodic" << endl;
|
|
|
|
|
|
|
|
// }, 700);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Periodic( [&] (){
|
|
|
|
|
|
|
|
// cout << "Delayed expire " << d->expired() << endl;
|
|
|
|
|
|
|
|
// cout << "Periodic ticks " << p->ticks() << endl;
|
|
|
|
|
|
|
|
// cout << "Delayed stoped " << d->stoped() << endl;
|
|
|
|
|
|
|
|
// cout << "Periodic stoped " << p->stoped() << endl;
|
|
|
|
|
|
|
|
// }, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Delayed( [&](){
|
|
|
|
|
|
|
|
// p->stop();
|
|
|
|
|
|
|
|
// }, 10000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
|
|
|
|
// /**
|
|
|
|
|
|
|
|
// * Run an function asyncronic
|
|
|
|
// // /**
|
|
|
|
// */
|
|
|
|
// // * Run an function asyncronic
|
|
|
|
|
|
|
|
// // */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async_ ( []() {
|
|
|
|
// async_ ( []() {
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
@ -205,13 +155,13 @@ int main () { |
|
|
|
// );
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // async(launch::async, [] () {
|
|
|
|
// async(launch::async, [] () {
|
|
|
|
// // cout << "Another thread in async style!" << endl;
|
|
|
|
// cout << "Another thread in async style!" << endl;
|
|
|
|
// // });
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// // /**
|
|
|
|
// /**
|
|
|
|
// // * Call class method
|
|
|
|
// * Call class method
|
|
|
|
// // */
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
|
|
// clm classes;
|
|
|
|
// clm classes;
|
|
|
|
// async_ ( [&classes] () {
|
|
|
|
// async_ ( [&classes] () {
|
|
|
@ -220,17 +170,17 @@ int main () { |
|
|
|
|
|
|
|
|
|
|
|
// sleep(5);
|
|
|
|
// sleep(5);
|
|
|
|
|
|
|
|
|
|
|
|
// // /**
|
|
|
|
// /**
|
|
|
|
// // * await_ after runned as async
|
|
|
|
// * await_ after runned as async
|
|
|
|
// // */
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
|
|
// auto aa = async_ ( []() {
|
|
|
|
// auto a = async_ ( []() {
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
// cout << "async_ 2" << endl;
|
|
|
|
// cout << "async_ 2" << endl;
|
|
|
|
// return 5;
|
|
|
|
// return 5;
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// cout << await_(aa) << endl;
|
|
|
|
// cout << await_(a) << endl;
|
|
|
|
// cout << "print after async_ 2" << endl;
|
|
|
|
// cout << "print after async_ 2" << endl;
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// /**
|
|
|
@ -321,19 +271,17 @@ int main () { |
|
|
|
// );
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// auto await_all2 = [&] () {
|
|
|
|
// auto await_all = [&] () {
|
|
|
|
// for (int i=0; i<fut_vec.size(); i++) {
|
|
|
|
// for (int i=0; i<fut_vec.size(); i++) {
|
|
|
|
// await_ (fut_vec[i]);
|
|
|
|
// await_ (fut_vec[i]);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
// await_all2();
|
|
|
|
// --------------- EVENTS -------------------
|
|
|
|
|
|
|
|
|
|
|
|
// // --------------- EVENTS -------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
/**
|
|
|
|
// * initialization of typed events
|
|
|
|
* initialization of typed events |
|
|
|
// */
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
// trigger<int, int> ev2int;
|
|
|
|
// trigger<int, int> ev2int;
|
|
|
|
// trigger<int, string> evintString;
|
|
|
|
// trigger<int, string> evintString;
|
|
|
@ -398,24 +346,6 @@ int main () { |
|
|
|
// cout << "Constructed " << i << endl;
|
|
|
|
// cout << "Constructed " << i << endl;
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
|
|
|
// *
|
|
|
|
|
|
|
|
// * Use class with multiple triggers
|
|
|
|
|
|
|
|
// *
|
|
|
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClassWithTriggers mt; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mt.on<int>("int", function<void(int)>([&](int i) { |
|
|
|
|
|
|
|
cout << "Emit int " << i << endl; |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mt.on<string>("string", function<void(string)>([&](string s) { |
|
|
|
|
|
|
|
cout << "Emit string " << s << endl; |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mt.tick("int", 5); |
|
|
|
|
|
|
|
mt.tick("string", string("Hello world")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// auto status = fs::read("test1.txt");
|
|
|
|
// auto status = fs::read("test1.txt");
|
|
|
|