Object Cast

Namespace rswhite4 at googlemail.com
Thu Jun 21 03:33:39 PDT 2012


To solve the problem of converting a Object to a specific 
template class i wrote this little function.
But the mixin disturbs me. How i can get the type (in this 
example Vector2D) without to mix a mixin and T.stringof?

[code]
T object_cast(T : Object)(Object value) {
	T val = cast(T) value;
	
	if (val !is null) {
		return val;
	}
	
	// if cast fails it is a template class
	
	import std.typetuple;
	
	foreach (Type; TypeTuple!(byte, ubyte, short, ushort, int, uint, 
long, ulong, float, double, real)) {
		mixin("
		if (auto vec = cast(" ~ T.stringof ~ "!(" ~ Type.stringof ~ 
"))(value)) {
			return cast(T) vec;
		}");
	}
	
	return null;
}
[/code]


More information about the Digitalmars-d-learn mailing list