C++ multiple member function callbacks without using std::bind, std::function or boost -


i have application requires 1 or many member functions of object used callbacks when hardware event occurs in monitoring object. callbacks require no arguments.

normally use boost or standard library functions (std::function, std::bind) wrap member function called cant use them (boost, c++11) not supported or allowed on platform. im not sure of efficiency either thats off-topic.

in short, have written class (monitorswi) spawns thread wait hardware events. hardware event , callback configured when object created such many other objects, such devicemanager below can utilise encapsulated functionality.

apart using bind, method have base class pure virtual handler implemented child object. however, of 'devicemanagers' handle multiple swis require different handlers.

the method using have static member redirects particular object instance , specified handler swi.

i have written simple example unnecessary code stripped out:

#include <iostream>  class monitorswi { public:    typedef void (*swihandlerfunc_t)(void*);     monitorswi(int id, void* pinstance, swihandlerfunc_t handler) :       m_id(id),       m_instance(pinstance),       m_handler(handler)    {    }     void handleevent()    {       std::cout << "monitorswi::handleevent() handle swi id = " << m_id << std::endl;       (*m_handler)(m_instance);    }  private:    int              m_id;    void*            m_instance;    swihandlerfunc_t m_handler; };   class devicemanager { public:    devicemanager() :       m_swi1(1, this, handleswi1), // register handler swi 1 events       m_swi2(2, this, handleswi2)  // register handler swi 2 events    {    }     static void handleswi1(void* pinstance)    {       std::cout << "devicemanager::handleswi1() redirect object..." << std::endl;       devicemanager* ptr = reinterpret_cast<devicemanager*>(pinstance);       ptr->handleswi1member();    }     static void handleswi2(void* pinstance)    {       std::cout << "devicemanager::handleswi2() redirect object..." << std::endl;       devicemanager* ptr = reinterpret_cast<devicemanager*>(pinstance);       ptr->handleswi2member();    }     void handleswi1member()    {       std::cout << "devicemanager::handleswi1member()" << std::endl;        // process event swi 1 has signalled...    }     void handleswi2member()    {       std::cout << "devicemanager::handleswi2member()" << std::endl;        // process event swi 2 has signalled...    }     monitorswi m_swi1;    monitorswi m_swi2; };  int main() {    devicemanager device;     // fake hardware calling swi 1 (normally performed hardware event handler)    device.m_swi1.handleevent();     // fake hardware calling swi 2 (normally performed hardware event handler)    device.m_swi2.handleevent();     return 0; } 

output test application:

monitorswi::handleevent() handle swi id = 1 devicemanager::handleswi1() redirect object... devicemanager::handleswi1member() monitorswi::handleevent() handle swi id = 2 devicemanager::handleswi2() redirect object... devicemanager::handleswi2member() 

i'm sure there must way using template specify class type , 1 or many member function handlers declared in class type.

are there more efficient or generic ways of achieving above?

you store

typedef void (t::*swihandlerfunc_t)(); 

(for type t taken template parameter monitorswi<t>) , call by

(m_instance->*m_handler)(); 

the full code in live example.


Popular posts from this blog