473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

callbacks

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.

Mar 19 '07 #1
1 1884
brekehan wrote:
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/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 19 '07 #2

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

Similar topics

1
2279
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a...
2
2060
by: Mike C. Fletcher | last post by:
I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate __del__ methods (and the memory leaks they create). Looking at the docs for 2.3's weakref.ref, there's no mention of whether...
1
1881
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
0
2696
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
1643
by: johny smith | last post by:
I cannot seem to figure out how to do instance based callbacks for some reason. I found one site on functors but I did not find it that helpful actually I just don't understand it. I have no...
5
3231
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
4
3325
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes...
0
1135
by: anilkoli | last post by:
I want clear cut idea about callbacks and also of delegates I have doughts about callbacks, I feel callbacks are used for 1. recursion 2. dynamically calling a perticular method out of many...
9
3326
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
13
3023
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.