--- gvpe/src/callback.pl 2006/05/31 00:31:47 1.9 +++ gvpe/src/callback.pl 2007/12/02 00:54:52 1.10 @@ -1,5 +1,7 @@ #!/usr/bin/perl +use strict; + print < + Copyright (C) 2003-2007 Marc Lehmann This file is part of GVPE. @@ -29,7 +31,7 @@ #ifndef CALLBACK_H__ #define CALLBACK_H__ -#define CALLBACK_H_VERSION 2 +#define CALLBACK_H_VERSION 3 template struct callback_funtype_trait; @@ -47,55 +49,40 @@ my $TYPEDEFS = join " ", map "typedef A$_ arg$_\_type;", 1..$a; my $TYPEvoid = $TYPE ? $TYPE : "void"; my $_TYPE = $TYPE ? ", $TYPE" : ""; - my $_ARG = $ARG ? ", $ARG" : ""; - my $_TYPEARG = $TYPEARG ? ", $TYPEARG" : ""; my $_TTYPE = $a ? join "", map ", typename T::arg$_\_type", 1..$a : ""; print < class callback$a { - struct object { }; - - typedef R (object::*ptr_type)($TYPE); + struct klass; // it is vital that this is never defined - void *obj; - R (object::*meth)($TYPE); + typedef R (klass::*ptr_type)($TYPE); - /* a proxy is a kind of recipe on how to call a specific class method */ - struct proxy_base { - virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const = 0; - }; - template - struct proxy : proxy_base { - virtual R call (void *obj, R (object::*meth)($TYPE)$_TYPEARG) const - { - return (R)((reinterpret_cast(obj)) ->* (reinterpret_cast(meth))) - ($ARG); - } - }; - - proxy_base *prxy; + klass *o; + R (klass::*m)($TYPE); public: template explicit callback$a (O1 *object, R (O2::*method)($TYPE)) - { - static proxy p; - obj = reinterpret_cast(object); - meth = reinterpret_cast(method); - prxy = &p; - } - + { + o = reinterpret_cast(object); + m = reinterpret_cast(method); + } + + // this works because a standards-compliant C++ compiler + // basically can't help it: it doesn't have the knowledge + // required to miscompile (klass is not defined anywhere + // and nothing is known about the constructor arguments) :) R call($TYPEARG) const - { - return prxy->call (obj, meth$_ARG); - } + { + return (o->*m) ($ARG); + } R operator ()($TYPEARG) const - { - return call ($ARG); - } + { + return call ($ARG); + } }; template @@ -131,9 +118,9 @@ template explicit callback (O object, M method) - : base_type (object, method) - { - } + : base_type (object, method) + { + } }; #endif