473,385 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Which Callback implementation is better?

Hi all,

I'm messing around with various signal/slot mechanisms, trying to build
something lean and fast. I've used libsigc++, and Sarah Thompson's at
sigslot.sourceforge.net, and most of the others you can find by
Googling, but I wanted to try doing it myself and needed some advice on
different approaches.

Most signal/slot libraries are based on template classes containing a
pointer-to-object and pointer-to-memfunc. You can also do callbacks or
signals with a pointer or reference to the object only, and use
templating to create a signal or callback based on an object and a
templated member function call. The shortest code I can cobble
together that demonstrates both approaches is below, which I threw into
a main.cpp.

#include <iostream>

template <class Arg>
class Callback1ArgBase
{
public:
Callback1ArgBase(){}
virtual ~Callback1ArgBase(){}
virtual void operator()(Arg& arg){}
};

template <class T, class Arg, void (T::*F)(Arg)>
class Callback1ArgTemplate : public Callback1ArgBase<Arg>
{
public:
Callback1ArgTemplate(T& t):object(t){}
virtual ~Callback1ArgTemplate(){}
virtual void operator()(Arg& arg){(object.*F)(arg);}

private:
T& object;
};
template <class T, class Arg>
class Callback1ArgMemPtr : public Callback1ArgBase<Arg>
{
public:
Callback1ArgMemPtr(T* t, void (T::*f)(Arg)):object(t),func(f){}
virtual void operator()(Arg& arg){(object->*func)(arg);}

private:
T* object;
void (T::*func)(Arg);
};

class Client
{
public:
Client(){}
~Client(){}
void TellMeInt(int i)
{
std::cout << "\n Wotcha. Callback fired! Int is " << i <<
"\n";
}
};

template<class Arg>
class Server
{
public:
Server():callbk1argtemplate(0),callbk1argmemptr(0) {}
~Server()
{
if (callbk1argtemplate)delete callbk1argtemplate;
if (callbk1argmemptr)delete callbk1argmemptr;
}

//create a templated callback
template<class T, void (T::*F)(Arg)> void
BindCallback1ArgTemplate(T& t)
{
callbk1argtemplate = new Callback1ArgTemplate<T, Arg, F>(t);
}

//create a member function pointer callback
template<class T> void BindCallback1ArgMemPtr(T* t, void
(T::*F)(Arg))
{
callbk1argmemptr = new Callback1ArgMemPtr<T, Arg>(t, F);
}

void DoSomethingArg(Arg arg)
{
std::cout << "\n This is Server doing something with an arg on
the template version.\n";
(*callbk1argtemplate)(arg);
std::cout << "\n This is Server doing something with an arg on
the member pointer version.\n";
(*callbk1argmemptr)(arg);
}
private:
Callback1ArgBase<Arg>* callbk1argtemplate;
Callback1ArgBase<Arg>* callbk1argmemptr;

};

int main()
{
Server<int> server;
Client client;
server.BindCallback1ArgTemplate<Client, &Client::TellMeInt>(client);
server.BindCallback1ArgMemPtr(&client, &Client::TellMeInt);
int fred = -32767;
server.DoSomethingArg(fred);
return 0;
}

Both methods work, but the Callback1ArgTemplate version is smaller
because it doesn't contain a member function pointer (unless I
misunderstand how the compiler builds template classes). Without a
member function pointer you can't rebind to a different member
function. I'm an engineer, not a Computer Science grad, so could
anyone with a bit of CompSci experience comment on these approaches?
Also, I noticed that server.BindCallback1ArgMemPtr... call doesn't
require the addition of the <Client> template part, I assume that's
because it's implicit in the arguments to the function.

Thanks in advance,

Damien

Feb 10 '06 #1
2 1827
Damien wrote:
Hi all,

I'm messing around with various signal/slot mechanisms, trying to build
something lean and fast. I've used libsigc++, and Sarah Thompson's at
sigslot.sourceforge.net, and most of the others you can find by
Googling, but I wanted to try doing it myself and needed some advice on
different approaches.

Most signal/slot libraries are based on template classes containing a
pointer-to-object and pointer-to-memfunc. You can also do callbacks or
signals with a pointer or reference to the object only, and use
templating to create a signal or callback based on an object and a
templated member function call. The shortest code I can cobble
together that demonstrates both approaches is below, which I threw into
a main.cpp.

#include <iostream>

template <class Arg>
class Callback1ArgBase
{
public:
Callback1ArgBase(){}
virtual ~Callback1ArgBase(){}
virtual void operator()(Arg& arg){}
};

template <class T, class Arg, void (T::*F)(Arg)>
class Callback1ArgTemplate : public Callback1ArgBase<Arg>
{
public:
Callback1ArgTemplate(T& t):object(t){}
virtual ~Callback1ArgTemplate(){}
virtual void operator()(Arg& arg){(object.*F)(arg);}

private:
T& object;
};
template <class T, class Arg>
class Callback1ArgMemPtr : public Callback1ArgBase<Arg>
{
public:
Callback1ArgMemPtr(T* t, void (T::*f)(Arg)):object(t),func(f){}
virtual void operator()(Arg& arg){(object->*func)(arg);}

private:
T* object;
void (T::*func)(Arg);
};

class Client
{
public:
Client(){}
~Client(){}
void TellMeInt(int i)
{
std::cout << "\n Wotcha. Callback fired! Int is " << i <<
"\n";
}
};

template<class Arg>
class Server
{
public:
Server():callbk1argtemplate(0),callbk1argmemptr(0) {}
~Server()
{
if (callbk1argtemplate)delete callbk1argtemplate;
if (callbk1argmemptr)delete callbk1argmemptr;
}

//create a templated callback
template<class T, void (T::*F)(Arg)> void
BindCallback1ArgTemplate(T& t)
{
callbk1argtemplate = new Callback1ArgTemplate<T, Arg, F>(t);
}

//create a member function pointer callback
template<class T> void BindCallback1ArgMemPtr(T* t, void
(T::*F)(Arg))
{
callbk1argmemptr = new Callback1ArgMemPtr<T, Arg>(t, F);
}

void DoSomethingArg(Arg arg)
{
std::cout << "\n This is Server doing something with an arg on
the template version.\n";
(*callbk1argtemplate)(arg);
std::cout << "\n This is Server doing something with an arg on
the member pointer version.\n";
(*callbk1argmemptr)(arg);
}
private:
Callback1ArgBase<Arg>* callbk1argtemplate;
Callback1ArgBase<Arg>* callbk1argmemptr;

};

int main()
{
Server<int> server;
Client client;
server.BindCallback1ArgTemplate<Client, &Client::TellMeInt>(client);
server.BindCallback1ArgMemPtr(&client, &Client::TellMeInt);
int fred = -32767;
server.DoSomethingArg(fred);
return 0;
}

Both methods work, but the Callback1ArgTemplate version is smaller
because it doesn't contain a member function pointer (unless I
misunderstand how the compiler builds template classes). Without a
member function pointer you can't rebind to a different member
function. I'm an engineer, not a Computer Science grad, so could
anyone with a bit of CompSci experience comment on these approaches?
Also, I noticed that server.BindCallback1ArgMemPtr... call doesn't
require the addition of the <Client> template part, I assume that's
because it's implicit in the arguments to the function.

Thanks in advance,

Damien

For callbacks, use the boost function and boost bind libraries. You
should at the very least evaluate them to see if the meet your needs.

Feb 10 '06 #2
Tried Boost too. Pretty good, but I'm still wondering about what the
experts think of either approach above.

Damien

Feb 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem...
8
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
6
by: db_from_mn | last post by:
I'm having a lot of difficulty getting even the simplest callback function (void return, no arguments) to work, from a dll to C#. When the callback is invoked from the dll, I get a fatal error,...
4
by: J055 | last post by:
Hi I have 2 update buttons in my FormView ('Apply' and 'OK'). I want both buttons to update the data source but the 'OK' button should redirect afterwards. I can see which button is clicked...
2
by: Chris Bore | last post by:
If I have a self-contained server component, that specifies callback functions, then would it be good practice for: 1) the server component itself to provide a header file with the callback...
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
18
by: kid joe | last post by:
Hello, I have seen the WndProc prototyped two ways, LONG WINAPI WndProc ( .... ); and LRESULT CALLBACK WndProc ( .... ); Are these functionally equivalent? What are WINAPI and CALLBACK...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.