By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

API Callbacks into Objects?

P: n/a
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 different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).

Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.

I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.

Anybody have any ideas? Hints? Suggestions?

Thanks!

( btw, don't reply to the mailinator.com address.
If you must reply via email, my email addr is:
My email is cej AT i n t e c h . c o m )

Christopher Jastram
Programmer, Technical Instructor
Integrated Technologies, Inc.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Christopher Jastram wrote:
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 different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?
Totally unclear so far what your problem is. Which direction are the
callbacks? Who's calling those callbacks? The library? How does it
do that? Does it store the function pointers somewhere? Does it use
the pointers provided in another function call as an argument? Why
do you think you need persistence?
I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).
I don't understand what you mean by "didn't even see the callbacks".
Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.
Again, what do you mean by "didn't really work out at all"?
I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.
You're probably right. So, if they are not global, the only way to
make them persistent is to make them dynamic and place them into some
kind of a collection, which probably needs to be global anyway. It
might not "work out at all", of course. Whatever that means.

Anybody have any ideas? Hints? Suggestions?


Here is a suggestion: be more specific. Give an example of your
library function and your attempt at a wrapper. Give a _good_ example
of how that function _would_ be used and how you propose to use your
wrapper. Perhaps while trying to describe it to us you will find some
suitable way in which this whole thing is going to work... If not, do
post your interface and we could talk implementation (given that you
attempt to create one first).

Victor
Jul 22 '05 #2

P: n/a

"Christopher Jastram" <ce*@mailinator.com> wrote in message
news:38**************************@posting.google.c om...
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 different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).


Perhaps it would be helpful if you illustrated what you mean by 'the
controlling program didn't even see the callbacks' with some sort of code
snippet.

It should be possible to get member functions to work providing the API you
are working with has been well designed. So give us an sample of what you
are working with.

John
Jul 22 '05 #3

P: n/a
Christopher Jastram wrote:
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 different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).

Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.

I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.

Anybody have any ideas? Hints? Suggestions?


I'm going to assume you want to call back a method of an object, and
that the callback function allows you to specify a void* as a parameter.
Given those assumptions, try something like the following:

class C;
extern "C" {
void c_callback(void*);
void api_with_callback(void (*)(void*), void*);
}

class C {
public:
// yada yada yada
void set_callback()
{
::api_with_callback(&::c_callback, this);
}
void my_callback()
{
}
};

void c_callback(void *param)
{
C* p = static_cast<C*>(param);
p->my_callback();
}
Jul 22 '05 #4

P: n/a
Christopher Jastram wrote:

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 different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).

Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.

I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.

Anybody have any ideas? Hints? Suggestions?

Thanks!

( btw, don't reply to the mailinator.com address.
If you must reply via email, my email addr is:
My email is cej AT i n t e c h . c o m )

Christopher Jastram
Programmer, Technical Instructor
Integrated Technologies, Inc.

--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 22 '05 #5

P: n/a
Christopher Jastram wrote:

If I understand you correctly than the FAQ might
be of some help.
Look up

http://www.parashift.com/c++-faq-lit...o-members.html

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.