Menu
Home
Create new Paste
Log in
Nested Structs (Solution)
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
module main; import std.stdio; import std.metastrings; static template StructNestType(string S, string name, alias C = "__ClassNameFix", alias dummy = "__NestLevelFix") { enum string Temp1 = `mixin(StructNestType2!("`~S~`", "`~name~`", `; static if (C == "__ClassNameFix") static if (dummy == "__NestLevelFix") enum string StructNestType = Temp1~C~`, `~dummy~`));`; else enum string StructNestType = Temp1~C~`, "`~dummy~`"));`; else static if (dummy == "__NestLevelFix") enum string StructNestType = Temp1~C~`", `~dummy~`));`; else enum string StructNestType = Temp1~C~`", "`~dummy~`"));`; } static template StructNestType2(string S, string name, alias C, alias dummy) { static if (std.string.indexOf(S, ")") > 0) { enum string S2 = S[0..S.length-1]; static if (std.string.indexOf(S2, ",") > 0) enum string StructNestType2 = "static if (!"~dummy~") "~S2~", 1000000) "~name~"; else "~S2~", "~C~"!"~dummy~")."~name~".offsetof) "~name~";"; else enum string StructNestType2 = "static if (!"~dummy~") "~S2~", 1000000) "~name~"; else "~S2~", "~C~"!"~dummy~")."~name~".offsetof) "~name~";"; } else { enum string StructNestType2 = "static if (!"~dummy~") "~S~"!(1000000) "~name~"; else "~S~"!("~C~"!"~dummy~")."~name~".offsetof) "~name~";"; } } static template StructNestParent(string name, alias C = "__ClassNameFix", alias dummy = "__NestLevelFix") { enum string Temp1 = `mixin(StructNestParent2!("`~name~`", `; static if (C == "__ClassNameFix") static if (dummy == "__NestLevelFix") enum string StructNestParent = Temp1~C~`, `~dummy~`));`; else enum string StructNestParent = Temp1~C~`, "`~dummy~`"));`; else static if (dummy == "__NestLevelFix") enum string StructNestParent = Temp1~C~`", `~dummy~`));`; else enum string StructNestParent = Temp1~C~`", "`~dummy~`"));`; } static template StructNestParent2(string name, alias C, alias dummy) { enum string StructNestParent2 = `static if (_Offset == 0) { `~C~dummy~`) `~name~`; } else { pure nothrow `~C~dummy~`) _`~name~`_func_`~name~`() { return cast(`~C~dummy~`))(cast(void *)&this - _Offset); } alias _`~name~`_func_`~name~` `~name~`; }`; } class _A(bool _NestLevel = true) { enum string __ClassNameFix = "_A!("; enum string __NestLevelFix = "_NestLevel"; public: struct B(int _Offset) { mixin(StructNestParent!("Parent")); void Do() { writeln(Parent.Name); } static if (_Offset == 0) B!(0) opAssign(T)(ref T b) { B!(0) a; a.Parent = b.Parent; this.Parent = b.Parent; return a; } else B!(_Offset) opAssign(T)(ref T b) { return cast(B!(_Offset))b; } } mixin(StructNestType!("B", "b1")); mixin(StructNestType!("B", "b2")); string Name; static if (_NestLevel) { this() { Name = "Class"; } } } alias _A!() A; int main(string[] argv) { auto asize = A.classinfo.init.length; auto bsize = A.B!(0).sizeof; A a = new A(); // Demonstrate that the two B fields in A have the same parent(yet contain no ptr to the parent) auto x = a.b1.Parent; auto y = a.b2.Parent; auto s1 = A.init.b1.offsetof; auto s2 = A.init.b2.offsetof; auto a_ptr = cast(void*)a; auto x_ptr = cast(void*)x; auto y_ptr = cast(void*)y; assert(a_ptr == x_ptr); assert(x_ptr == y_ptr); // Show that each parent of b2(a,x,y) are all the same(or at least all have the same b2.Do) auto p1 = &(a.b2.Do); auto p2 = &(x.b2.Do); auto p3 = &(y.b2.Do); assert(p1 == p2); assert(p2 == p3); a.b2.Do(); x.b2.Do(); y.b2.Do(); writeln(a.Name, x.Name, y.Name); // Nested struct to nested struct assignment with implicit cast a.b1 = a.b2; assert(a.b1.Parent == a.b2.Parent); a.b1.Do(); // "orphaned" struct which contains a ptr to parent A.B!(0) b1; b1 = a.b1; assert(b1.Parent == a.b1.Parent); b1.Do(); getchar(); return 0; }
Result:
Success
/
Return code: 0
/
Compilation time:
0.795
seconds
/
Run time:
0.086
seconds
Disassembly
Application output:
Class
Class
Class
ClassClassClass
Class
Class
Username
Message
Add comment
Paste info
Author:
js.mdnq
Views:
167
Private:
no
Expires:
Never
Uploaded:
12.12.12 22:59
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.060
Pointer size:
m32
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason