c++ - Signals-slots system: defining signal (magic macro needed) -


i need have signals/slots analog (with no boost) of qt's signals/slots system. problem if want call signal's slots, need generate code. @ example:

struct test {     void on_mouse_click_event(int x, int y) // #1     {         using function = void (test::*)(int x, int y); // #2         auto event_id_receivers_pair = event_id_to_receivers_.find(typeid(function).hash_code());         if(event_id_receivers_pair != event_id_to_receivers_.end())         {             functionargs<function> args(x, y); // #3             for(auto& p_receiver : event_id_receivers_pair->second)                 p_receiver->call(&args);         }         on_mouse_click(x, y);     }      virtual void on_mouse_click(int x, int y)     {         std::cout << "on click: " << x << ", " << y             << " - " << << "\n";     }      std::unordered_map<         std::size_t/*function (event) id*/,         std::vector<std::unique_ptr<icallee>>/*array of receivers*/>         event_id_to_receivers_;  };  struct foo {     void test_click(int x, int y)     {         std::cout << "on test click: " << x << ", " << y             << " - " << << "\n";     } };  template<typename sender, typename receiver, typename signal, typename slot> void connect(sender* sender, signal signal,     receiver* receiver, slot slot) {     auto callee = create_callee<receiver, slot>(receiver, slot);     sender->event_id_to_receivers_[         typeid(signal).hash_code()].             push_back(std::move(callee)); }  int main() {     test test;     foo foo;     foo foo1;      connect(&test, &test::on_mouse_click_event,         &foo, &foo::test_click);      connect(&test, &test::on_mouse_click_event,         &foo1, &foo::test_click);      test.on_mouse_click_event(10, 20); } 

class test has event on_mouse_click(). in main, have 2 connections event , last line event's emit. result, want see this:

on click: 10, 20 - 009cfc63 on click: 10, 20 - 009cfc57 on test click: 10, 20 - 009cfc6c 

the question: there way me have macro write code this:

class test {     signal(on_mouse_click)(int x, int y); }; 

i need have:

  1. type of pointer member function : void (test::*)(int x, int y); // #2
  2. names of function parameters pass them around: args(x, y); // #3

is possible ? there workarounds ?

thanks


Comments