forked from dlang/druntime
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rtti.d
102 lines (93 loc) · 2.49 KB
/
rtti.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module rtti;
public import core.traits;
struct thMemberInfo
{
string name;
TypeInfo type;
size_t offset;
thMemberInfo[]* next;
}
struct RttiAnchor
{
}
template RttiInfo(T)
{
//pragma(msg, makeRttiInfo!T());
__gshared RttiInfo = mixin(makeRttiInfo!T());
}
template instanceSize(T)
{
static if(is(T == class))
enum size_t instanceSize = __traits(classInstanceSize, T);
else
enum size_t instanceSize = T.sizeof;
}
size_t rttiMemberCount(T)()
{
size_t count = 0;
foreach(m; __traits(allMembers, T))
{
static if(isRttiMember!(__traits(getMember, T, m)))
{
count++;
}
}
return count;
}
string formatOffset(size_t offset)
{
string result = "";
do
{
auto digit = offset % 10;
result = cast(char)('0' + digit) ~ result;
offset = offset / 10;
}
while(offset > 0);
return result;
}
string makeRttiInfo(T)()
{
/*static if(is(T == struct))
pragma(msg, "struct " ~ T.stringof);*/
string result = "[thMemberInfo(\"" ~ T.mangleof ~ "\", typeid(T), instanceSize!T, null)";
size_t i=0;
foreach(m; __traits(allMembers, T))
{
static if(is(T == struct))
{
static if(is(typeof(__traits(getMember, T, m).offsetof)))
{
size_t offset = __traits(getMember, T, m).offsetof;
static if(is(typeof(__traits(getMember, T, m)) == struct))
result ~= ",\nthMemberInfo(" ~ m.stringof ~ ", typeid(typeOfField!(T, \"" ~ m ~ "\")), " ~ formatOffset(offset) ~ ", &RttiInfo!(typeOfField!(T, \"" ~ m ~ "\")))";
else
result ~= ",\nthMemberInfo(" ~ m.stringof ~ ", typeid(typeOfField!(T, \"" ~ m ~ "\")), " ~ formatOffset(offset) ~ ", null)";
i++;
}
}
else
{
static if(__traits(compiles, mixin("(T." ~ m ~ ").offsetof")))
{
size_t offset = mixin("(T." ~ m ~ ").offsetof");
static if(is(typeof(__traits(getMember, T, m)) == T) || !is(typeof(__traits(getMember, T, m)) == struct))
result ~= ",\nthMemberInfo(" ~ m.stringof ~ ", typeid(typeOfField!(T, \"" ~ m ~ "\")), " ~ formatOffset(offset) ~ ", null)";
else
{
//pragma(msg, "Reference for " ~ T.stringof);
result ~= ",\nthMemberInfo(" ~ m.stringof ~ ", typeid(typeOfField!(T, \"" ~ m ~ "\")), " ~ formatOffset(offset) ~ ", &RttiInfo!(typeOfField!(T, \"" ~ m ~ "\")))";
}
i++;
}
}
}
return result ~ "]";
}
const(thMemberInfo)[] getRttiInfo(const TypeInfo ti)
{
auto temp = cast(const(thMemberInfo)[]*)ti.rtInfo;
if(temp is null)
return [];
return *temp;
}