473,378 Members | 1,321 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,378 software developers and data experts.

callback design question

Hi, I'm implementing a class which will serve as a callback. It will
have multiple virtual operations, which will be called upon certain
events.
The user of the class can inherit from this class and implement the
virtual operations to fit his/her needs.
Each operation will have up to 3 parameters of either basic integral
types (e.g. int, unsigned, size_t) or pointers (e.g. const char*).
However these parameters can be stored in the class as attributes.
My question is - whould you rather design operations with parameters
or have these parameters as members of the class and the operations
would have no parameters?
Can this have a impact on the performance (copying the parameters via
stack)? I know, design first, then do the profiling and see.. ;) But
I'd like to hear your opinion.
Thanks

Feb 21 '07 #1
9 1440
mliptak wrote:
Hi, I'm implementing a class which will serve as a callback. It will
have multiple virtual operations, which will be called upon certain
events.
The user of the class can inherit from this class and implement the
virtual operations to fit his/her needs.
Each operation will have up to 3 parameters of either basic integral
types (e.g. int, unsigned, size_t) or pointers (e.g. const char*).
However these parameters can be stored in the class as attributes.
My question is - whould you rather design operations with parameters
or have these parameters as members of the class and the operations
would have no parameters?
Can this have a impact on the performance (copying the parameters via
stack)? I know, design first, then do the profiling and see.. ;) But
I'd like to hear your opinion.
Thanks
I there is any possibility that the parameters would change during the
lifetime of the callback object then I would undoubtedly want the
parameters to be real parameters, rather than class attributes.

Class attributes would be an example of the 'accumulate and fire'
anti-pattern.

http://en.wikipedia.org/wiki/Accumulate_and_fire

john
Feb 21 '07 #2
I there is any possibility that the parameters would change during the
lifetime of the callback object then I would undoubtedly want the
parameters to be real parameters, rather than class attributes.
Yes, the parameters would change, however they are meaningful only
during the call of the operation.

Feb 21 '07 #3
On Feb 21, 10:00 am, "mliptak" <Meht...@gmail.comwrote:
Hi, I'm implementing a class which will serve as a callback. It will
have multiple virtual operations, which will be called upon certain
events.
The user of the class can inherit from this class and implement the
virtual operations to fit his/her needs.
Each operation will have up to 3 parameters of either basic integral
types (e.g. int, unsigned, size_t) or pointers (e.g. const char*).
However these parameters can be stored in the class as attributes.
My question is - whould you rather design operations with parameters
or have these parameters as members of the class and the operations
would have no parameters?
Can this have a impact on the performance (copying the parameters via
stack)? I know, design first, then do the profiling and see.. ;) But
I'd like to hear your opinion.
Thanks
Maybe before you are writing your own code, you want to think of the
best pattern that I know - using a well design lib that will do what
you need to do and that was writing by expert programmers, in this
case you may want to look at:
http://libsigc.sourceforge.net/doc.shtml
and
http://www.boost.org/doc/html/function.html
and
http://www.boost.org/doc/html/signals.html
as libraries writing by programmers who devoted enough time to the
problem and have enough skill to have a well design lib for other to
use. By saying this I don't mean that you don't have the skills, just
that it may save you time and if you don't find them useful then at
least you may find some good ideas
Have luck

Feb 21 '07 #4
mliptak wrote:
>>I there is any possibility that the parameters would change during the
lifetime of the callback object then I would undoubtedly want the
parameters to be real parameters, rather than class attributes.


Yes, the parameters would change, however they are meaningful only
during the call of the operation.
Which is why 'real parameters' are the natural fit. Class attributes
last for the lifetime of the object.

One situation in which class attributes would not only be ugly but
actually fail to work is when the same callback object was being used in
different threads of execution.

john
Feb 21 '07 #5
Maybe before you are writing your own code, you want to think of the
best pattern that I know - using a well design lib that will do what
you need to do and that was writing by expert programmers, in this
case you may want to look at:http://libsigc.sourceforge.net/doc.shtml
andhttp://www.boost.org/doc/html/function.html
andhttp://www.boost.org/doc/html/signals.html
as libraries writing by programmers who devoted enough time to the
problem and have enough skill to have a well design lib for other to
use. By saying this I don't mean that you don't have the skills, just
that it may save you time and if you don't find them useful then at
least you may find some good ideas
Have luck
Thanks for the info. Have you actually used any of these? I'm kind
of familiar with http://sigslot.sf.net - are the libs above better
than this one?

Feb 21 '07 #6
On 21 Feb, 08:58, "mliptak" <Meht...@gmail.comwrote:
Maybe before you are writing your own code, you want to think of the
best pattern that I know - using a well design lib that will do what
you need to do and that was writing by expert programmers, in this
case you may want to look at:http://libsigc.sourceforge.net/doc.shtml
andhttp://www.boost.org/doc/html/function.html
andhttp://www.boost.org/doc/html/signals.html
as libraries writing by programmers who devoted enough time to the
problem and have enough skill to have a well design lib for other to
use. By saying this I don't mean that you don't have the skills, just
that it may save you time and if you don't find them useful then at
least you may find some good ideas
Have luck

Thanks for the info. Have you actually used any of these? I'm kind
of familiar withhttp://sigslot.sf.net- are the libs above better
than this one?
libsigc and boost::signals are actually the same lib
(by the same people) with some cosmetic differences.
I am not familiar with sigslot but at short glance it
seems to offer similar, albeit more limited, functionality
to libsigc/signals, the only real difference I see is that
libsigc/signals thread safety is left entirely to the user.
The benefit of boost is that it is well documented,
tested, supported and portable. it is also an entry point
for future standard library extensions. (you also get
an added benefit of the rest of excellent boost libraries)

I have been using libsigc/signals for several years
and I warmly recommend it.
Feb 21 '07 #7
I have been using libsigc/signals for several years
and I warmly recommend it.
So I gave it a try and instead of using virtual functions I used the
signals..
But I gained nothing in terms of performance and also the interface
remained pretty much the same.
So what's the rationale behind using signals / slots?

Mar 1 '07 #8
On 1 Mar, 07:19, "mliptak" <Meht...@gmail.comwrote:
I have been using libsigc/signals for several years
and I warmly recommend it.

So I gave it a try and instead of using virtual functions I used the
signals..
But I gained nothing in terms of performance and also the interface
remained pretty much the same.
So what's the rationale behind using signals / slots?
signals/slots is a very simple library.

you have much less restrictive interface with signals than with
abstract bases.

struct my_interface
{
virtual void fun1(A &)=0;
virtual void fun2(B &)=0;
};

with this approach you have to implement both functions even if
you don't use one of them

signal<void (A&)asignal;
signal<void (B&)bsignal;

slots are arbitrary function objects
you can connect any function object that is convertible to
the slot

void fun(A&);
asignal.connect(&fun);
struct T
{
void fun(A&);
};
T t;
asignal.connect(bind(&T:fun, &t, _1));

you can bind extra arguments and adapt
interface to fit the signal.

void my_fun(A & a, int n);
asignal.connect(bind(&my_fun, _1, 4));

you have much more robust
and functional connection management.

void my_fun1(B &);
connection con = bsignal.connect(bind(&my_fun1, _1));
....
con.block();
...
con.unblock();
...
con.disconnect();

using signals::trackable and signals::scoped_connection you
can have automatic connection management

you can group/order slots. you can chain signals. you can
collate results.

now, of course you can implement all that functionality yourself.
the point is that you don't have to since someone else has done
it already.

Mar 1 '07 #9
On Mar 1, 12:17 pm, "dasjotre" <dasjo...@googlemail.comwrote:
On 1 Mar, 07:19, "mliptak" <Meht...@gmail.comwrote:
I have been using libsigc/signals for several years
and I warmly recommend it.
So I gave it a try and instead of using virtual functions I used the
signals..
But I gained nothing in terms of performance and also the interface
remained pretty much the same.
So what's the rationale behind using signals / slots?

signals/slots is a very simple library.

you have much less restrictive interface with signals than with
abstract bases.

struct my_interface
{
virtual void fun1(A &)=0;
virtual void fun2(B &)=0;

};

with this approach you have to implement both functions even if
you don't use one of them
You can do the default implementation in the base class as well,
instead of pure virtuals..
>
signal<void (A&)asignal;
signal<void (B&)bsignal;

slots are arbitrary function objects
you can connect any function object that is convertible to
the slot

void fun(A&);
asignal.connect(&fun);
struct T
{
void fun(A&);};

T t;
asignal.connect(bind(&T:fun, &t, _1));

you can bind extra arguments and adapt
interface to fit the signal.

void my_fun(A & a, int n);
asignal.connect(bind(&my_fun, _1, 4));

you have much more robust
and functional connection management.

void my_fun1(B &);
connection con = bsignal.connect(bind(&my_fun1, _1));
...
con.block();
..
con.unblock();
..
con.disconnect();

using signals::trackable and signals::scoped_connection you
can have automatic connection management

you can group/order slots. you can chain signals. you can
collate results.

now, of course you can implement all that functionality yourself.
the point is that you don't have to since someone else has done
it already.
Thanks for the explanation.
I think I just did not see the use for all this because I did not come
over a realworld application where this could be a perfect fit.

Mar 1 '07 #10

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

Similar topics

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...
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
7
by: Varangian | last post by:
Hi all, I have a question, what does the function ClientScriptManager.RegisterForEventValidation really does? Does it means securing your client side code? more security? The question is how...
13
by: Wilfried Mestdagh | last post by:
Hi, I have an application using a DLL and callbacks. It generate random the error "A callback was made on a garbage collected delegate". I found some articles that the pointer to the delegate...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.