__traits and determining all overridable functions
Steven Schveighoffer
schveiguy at yahoo.com
Mon Nov 12 06:57:17 PST 2007
Hi,
I was thinking of making a template that derives from an object, but
intercepts all calls to virtual functions automatically. This could be used
for something like RMI or .net Remoting to allow a call to either execute a
method locally or in an object on a server if connected.
I thought maybe I could use __traits to determine all virtual functions,
then override them using a mixin. I thought __traits was a compile-time
function, and so I thought I could create a compile-time-function that
returns a string defining all virtual functions.
However, in doing so I find that the function does not work correctly, and
in addition, the argument to __traits for isVirtualFunction does not take a
string, but the result of __traits allMembers is an array of strings. I
think it would be more useful if isVirtualFunction and the other isXX would
take either a string or the direct statement. For example:
__traits(isVirtualFunction, "A.foo")
would be the same as:
__traits(isVirtualFunction, A.foo)
Then I could do:
foreach(x; __traits(allMembers, A))
if(__traits(isVirtualfunction, "A." ~ x))
// append to string that redefines virtual function.
Second, the mixin doesn't work. I hard-coded the function to output the
string I was trying to build. Therefore, the compiler should be able to
execute the method in compile time, and pre-determine the string to return.
But the mixin just defines the function name that I am calling as a member.
i.e., I define my function above as:
string redefFunctions(T)();
and my mixin in the class is:
mixin(redefFunctions!(A)());
But when I build my class, there is a member redefFunctions!(A) instead of
the list of virtual functions I expected to override.
Any ideas? Is there another way this will work? If not, is it possible to
make it so it would work :) ?
-Steve
More information about the Digitalmars-d
mailing list