Re: How do I create a function in my library for passing user callback function
- From: "Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx>
- Date: Mon, 14 Apr 2008 02:24:52 GMT
Angus <nospam@xxxxxxxxx> wrote in message
news:fttgg7$g7k$1$8302bc10@xxxxxxxxxxxxxxxxxxx
Hello
I am writing a library which will write data to a user defined callback
function. The function the user of my library will supply is:
int (*callbackfunction)(const char*);
In my libary do I create a function where user passes this callback
function? How would I define the function?
I tried this
callbackfunction clientfunction;
void SpecifyCallbackfunction(cbFunction cbFn)
{
clientfunction = cbFn;
}
Then called like this:
clientfunction(sz); // sz is a C-string.
But program crashes with access violation when attempt to call
clientfunction
What am I doing wrong?
Well, just about everything, and most pertinently, asking a question
here, the land of the technically-incompetent trolls...but here's how
you do it:
In the header file for your library, declare the function as follows:
extern void my_library_function(int (*)(const char*));
(Note: as somebody may tell you, "extern" is a redundant
linkage specifier for function declarations, but I use it anyway
and therefore you should too!)
Now write your library function that takes the callback as
a parameter in the source file for your library:
void my_library_function(int my_callback_function(const char*)) {
int my_callback_return;
char *my_string;
... /* generic stuff done here, probably "build up" my_string */
my_callback_return=my_callback_function(my_string);
... /* more generic stuff maybe, maybe check my_callback_return */
}
Now, for any source file that you want to use that generic
my_library_function(), you can call it by first #include'ing the
library header file, then defining a specific callback function that
matches the declaration in the header file:
int my_specific_function(const char* my_string) {
... /* do something with string, probably print it, right? */
}
Then you can call your library function with the callback anywhere
in your source file, as well as any other functions that you have defined
that match the callback signature:
void my_function(void) {
... /* stuff happens here, whatever, maybe nothing, who knows */
my_library_function(my_specific_function);
... /* and whatever else */
}
And that's "all" there is to it...not that bad once you get the hang of
it, just follow the pattern above, sometimes you have to really "think"
about what the perfect "signature" will be for all the various callbacks
you want for a generic library function, what all data you need to
pass for all possible conditions...
---
William Ernest Reid
.
- Follow-Ups:
- Re: How do I create a function in my library for passing user callback function
- From: Ben Bacarisse
- Re: How do I create a function in my library for passing user callback function
- From: Nick Keighley
- Re: How do I create a function in my library for passing user callback function
- From: Richard Heathfield
- Re: How do I create a function in my library for passing user callback function
- References:
- Prev by Date: Re: Problem with atoi and strlod
- Next by Date: Re: Write two separated objects?
- Previous by thread: Re: How do I create a function in my library for passing user callback function
- Next by thread: Re: How do I create a function in my library for passing user callback function
- Index(es):
Relevant Pages
|