How to cast type from typeinfo in runtime?
badmadevil
badmadevil at gmail.com
Sun Feb 17 08:53:47 PST 2008
Hi, i want to format complex number by Tango's Layout class.
As a sub-problem, I need to convert Arg p to a proper typed variable u.
It can be done if convert p in _all_ 6 cases (like the ifloat case).
But can the type (of p) used in casting be directly derived from argument _type_ (of Typeinfo)?
Thank for help.
----------
class ComplexLayout : Layout!(char) {
protected override char[] unknown (char[] output, char[] format, TypeInfo type, Arg p) {
char[] s ;
creal u ;
// p is of type Arg, alias from void*
char tc = type.classinfo.name[9] ;
switch(tc) {
case 'q','r','c','j','p': // complex & imagery
u = cast(creal)( *cast(typeof(type.classinfo.create())*)p ) ; // failed try
// u = cast(creal)( *cast(typeof(Object.factory(type.toString))*)p ) ; // failed try
break ;
case 'o': // ifloat
u = cast(creal)( *cast(ifloat*)p ) ;
break ;
default:
return "{unhandled argument type: " ~ type.toString ~ '}';
}
// format u into s
return s ;
}
}
More information about the Digitalmars-d-learn
mailing list