callbacks

B

brekehan

I am going to try and keep this non-OS specific, especially when I am
trying to implement this on multiple OSes

The need has arisen for a callback mechanism. Currently, things are
going to go something like this:

1) Application requests some work to be done from API and supplies a
memory buffer that cannot be modified until called back
2) API does work, fills buffer, and calls back Application when
completed <--I am working on this part

This is to minimize copying of large amounts of data, but still keep
things running independantly of each other without complicating things
with threads.

My question is:
Do callbacks need to be C style functions?
If not,
Can a callback be a method of a class (and static)
If so, can it be private (and static)?

I really hate to make things ugly by implementing global functions
outside a class to communicate between 'modules' (APP and API lib),
get data from the callback, then post something to an instance of a
class. that would require all kinds of seperate methods for posting
back to the class instance from outside....and they would have to be
public and exposed to the user whom is going to say to himself,"wth
are these?"

I am not aiming to use anything Windows or Linux specific such as the
CALLBACK definition in windows.h. I will be making my own callback
definition:

something like
typedef void (* API_NAME_CALLBACK) ( arguments);

then the APP can call an API method like:
someclass->dostuff(char * mybuffer, API_NAME_CALLBACK routine);
and be notiied when his buffer is ready to be released.
 
A

Adrian Hawryluk

brekehan said:
I am going to try and keep this non-OS specific, especially when I am
trying to implement this on multiple OSes

The need has arisen for a callback mechanism. Currently, things are
going to go something like this:

1) Application requests some work to be done from API and supplies a
memory buffer that cannot be modified until called back
2) API does work, fills buffer, and calls back Application when
completed <--I am working on this part

This is to minimize copying of large amounts of data, but still keep
things running independantly of each other without complicating things
with threads.

My question is:
Do callbacks need to be C style functions?

You can have a C++ style 'callback'. You can do this by inheriting from
an interface class (contains only pure virtual functions), pass that
object to the buffer class which it would then store. Upon filling the
buffer, the buffer would then invoke one of the virtual functions,
telling the object that the job has been completed. This is a fairly
standard approach in OO languages.
If not,
Can a callback be a method of a class (and static)
If so, can it be private (and static)?

You can make a callback as a static function within the class. And it
can be private (callback would have to be either set from within the
class, from a friend function or the class would return a pointer to
that function).

Usually, the only reason to do it this way would be for passing the
callback to legacy C code. When done, it also usually has a pointer
passed along with it pointing at the C 'object' which is passed along to
the C callback function. If no pointer to an 'object' is passed, it
usually means that the function is touching some global stuff (bad) or
is some type of singleton (valid, but could be questionable).

If you are interfacing with other languages, a C style callback may be
in order, otherwise I would recommend the interface method described
above. You could of course use templates, but in this case, I think it
would be overkill, and if meant for late binding, probably not even
possible.
I really hate to make things ugly by implementing global functions
outside a class to communicate between 'modules' (APP and API lib),
get data from the callback, then post something to an instance of a
class. that would require all kinds of seperate methods for posting
back to the class instance from outside....and they would have to be
public and exposed to the user whom is going to say to himself,"wth
are these?"

That is what documentation is for. ;)
I am not aiming to use anything Windows or Linux specific such as the
CALLBACK definition in windows.h. I will be making my own callback
definition:

something like
typedef void (* API_NAME_CALLBACK) ( arguments);

then the APP can call an API method like:
someclass->dostuff(char * mybuffer, API_NAME_CALLBACK routine);
and be notiied when his buffer is ready to be released.

Sure, if you're crossing a language boundary or want to make it as
generic as possible so that anything that can interface with C can
access this, then this would be the way to go. But if you do this,
unless you are calling back to some singleton, you might want to expand
your 'dostuff' function to include a void* pObj parameter so that you
would be able to inform the correct object of the completion.

That's my 4bits.


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/by-nc-sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top