API Callbacks into Objects?

  • Thread starter Christopher Jastram
  • Start date
C

Christopher Jastram

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.
 
V

Victor Bazarov

Christopher said:
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
 
J

John Harrison

Christopher Jastram said:
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
 
R

red floyd

Christopher said:
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();
}
 
K

Karl Heinz Buchegger

Christopher said:
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: (e-mail address removed) Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top