[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Adonthell-devel] Callback thoughts
From: |
Kai Sterker |
Subject: |
[Adonthell-devel] Callback thoughts |
Date: |
Sun, 23 Mar 2003 21:49:58 +0100 |
I spent the afternoon with that callback stuff, and here's a little
something I came up with:
First of all, whatever gets passed to the callback::run () method can't
be a template itself. So the argument should be a wrapper class that can
store any type internally, like the following:
class Arg
{
public:
template <class Type> Arg (Type arg)
{
TheArg = (void *) arg;
}
private:
void *TheArg;
};
However, once the type passed to Arg has been cast to void*, it's type
is lost and can't be restored. That's because callback::run does not
know anything about the arguments it gets. OTOH, the function or method
called by run itself knows what arguments it expects. So we could use
the function/method signature to retrieve the original type:
template <class Type> void run (void (*f)(Type)) const
{
f ((Type) TheArg);
}
With that in place, a very simple example will work already. See the
attached code. (The class A you'll find there matches base/callback,
class B would match python/callback).
Problems of the solution:
* First of all, it's not typesafe. If you pass the wrong argument,
you'll get a mess.
* Right now, each callback class is limited to a single, hard coded
callback function. Would be nice if you could pass the desired
function at runtime (when creating the callback, or before calling
it).
* In a similar way, support for more than one argument has to be added.
So you'll see that the current implementation is veeeeery basic ;).
But maybe it gives you some inspiration for something much better. If
you think it might go in the right direction, feel free to experiment
and to expand.
Kai
callback_test.cc
Description: Text document
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Adonthell-devel] Callback thoughts,
Kai Sterker <=