I've created a way to have the option of using both function pointers and class method pointers as callbacks. It's a hybrid between the classic C function pointer method and the static object caller method. I wrote dummy program to demonstrate it.
First I have the caller class that gets registered
Caller.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#pragma once
#include <stdio.h>
#include "Callbacker.h"
class Caller
{
public:
Caller();
~Caller(void);
void update(int);
static void callbackObject(void* object,int value);
};
| |
Caller.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include "Caller.h"
Caller::Caller()
{
}
Caller::~Caller(void)
{
}
void Caller::update(int thing)
{
printf("The value is %d\n",thing);
}
void Caller::callbackObject(void* object,int value)
{
Caller* This = (Caller*) object;
This->update(value);
}
| |
In this class,
update is the real callback method and
callbackObject is the pseudo-callback method that will be registered.
Next there is callbacker class that will call either an objects pseudo-callback or a normal function callback depending on which is registered.
Callbacker.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <stdio.h>
class Callbacker
{
public:
Callbacker(void);
~Callbacker(void);
void setCallback(void (*cb)(int));
void setCallback(void (*cb)(void*,int),void* obj);
void call();
private:
void (*callback)(int);
void (*callback_obj)(void*,int);
void* object;
};
| |
Callbacker.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include "Callbacker.h"
Callbacker::Callbacker(void)
{
callback = NULL;
callback_obj = NULL;
object = NULL;
}
Callbacker::~Callbacker(void)
{
}
void Callbacker::setCallback(void (*cb)(int))
{
callback_obj = NULL;
object = NULL;
callback = cb;
}
void Callbacker::setCallback(void (*cb)(void*,int),void* obj)
{
callback = NULL;
object = obj;
callback_obj = cb;
}
void Callbacker::call()
{
if(callback != NULL)
{
callback(9001);
}
else if((callback_obj != NULL) && (object != NULL))
{
callback_obj(object,9002);
}
}
| |
In this class there are two
setCallback methods. The first one is for registering a C function callback and the other is for registering a classes pseudo-callback for an object. The call function will use the callback that isn't NULL.
Then we have our main function that tests all of it.
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
include <stdio.h>
#include <stdlib.h>
#include "Callbacker.h"
#include "Caller.h"
void callMe(int thing)//For testing a C-style callback
{
printf("The value is %d\n",thing);
}
int main()
{
printf("Dummy Callback Test\n");
Callbacker* back = new Callbacker;
Caller* caller = new Caller();
back->setCallback(Caller::CallbackObject,caller);
back->call();
back->setCallback(callMe);
back->call();
system("pause");//To keep the console open
return 0;
}
| |
In this file, both the pseudo-callback of an object and a C-style callback are used.
If anyone can think of a circumstance were this method would fail, please let me know and give me a possible solution.