Get class size of object
JS
js.mdnq at gmail.com
Sun Aug 11 11:33:18 PDT 2013
I think you're missing the point to some degree(I realize there
is a diff between an object and a type, but I should be able to
easily get the class size of an object at run time regardless if
the object is typed as a base class). The code below does this,
but at a cost of verbosity.
Here is code that exactly demonstrates what I am essentially
trying to do. The only drawback now is having to mixin the
template for each class which I would want to automate and it
would be nice to wrap the RT and CT methods in subclasses without
overhead.
http://dpaste.dzfl.pl/757eeb05
Note, I can easily get the class size, name, etc... by using the
object or the type.
import std.stdio;
mixin template tObject(alias T)
{
override @property int classSize() { return
__traits(classInstanceSize, T); };
override @property string className() { return T.stringof; };
immutable static int ClassSize = __traits(classInstanceSize, T);
immutable static string ClassName = T.stringof;
static T New(Args...)(Args args) { writeln(T.ClassSize); return
new T(args); }
}
interface iObject(T)
{
@property int classSize();
@property string className();
}
class A : iObject!A
{
double x;
mixin tObject!A;
}
class B : A
{
double y;
mixin tObject!B;
double z, t;
}
void main()
{
A a = A.New();
A b = B.New();
writeln(a.classSize);
writeln(b.classSize);
readln;
}
More information about the Digitalmars-d-learn
mailing list