473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ 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_CALLBA CK) ( arguments);

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

Mar 19 '07 #1
1 1893
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_CALLBA CK) ( arguments);

then the APP can call an API method like:
someclass->dostuff(char * mybuffer, API_NAME_CALLBA CK 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.blogspo t.com/]______\/
Mar 19 '07 #2

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

Similar topics

1
2290
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 comment about this same problem on the web but no solution was noted. What makes the callbacks with the sequences so different? It seems that when one of the callbacks with a sequence is called it just hangs. I am talking to a TAO orb from...
2
2075
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 the callbacks are held with a strong reference. My experiments suggest they are not... i.e. I'm trying to use this pattern: class Closer( object ): """Close the OIDStore (without a __del__)""" def __init__( self, btree ): """Initialise the...
1
1909
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 structure fields in a buffer. In my managed code(i.e. in C# program), I've used a delegate for invoking callback function and I've declared the structure too. The dll fucntion is executing finely without any errors but I'm not getting any values...
0
2723
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 not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
2
1652
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 problem doing non-instance based callbacks. I want to be able to pass a function into a constructor and from inside the class make a call make a call to the instance based function of another class. Do i have to use templates?
5
3240
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 object members. Can anyone help? I'm writing a C++ wrapper for a fairly old programming interface to a document editing program that has no OOP whatsoever; only tons of structs. This program has different callbacks I'm supposed to implement for...
4
3345
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 a problem with the callbacks, for example: class X { public: add(); };
0
1141
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 methods, deciding at runtime. 3. Notification
9
3337
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 limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
13
3037
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 virtuals in C++ leads us to want to implement a framework where those callbacks are simply overriden virtual methods in a derived class. So...
0
8424
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8415
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
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 we have to send another system
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.