Created
August 8, 2010 10:29
-
-
Save sinfu/513866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
void main() | |
{ | |
C c1, c2; | |
C r1, r2; | |
r1 = c1 = new C(1); | |
r2 = c2 = new C(2); | |
assert(c1.id == 1); | |
assert(c2.id == 2); | |
writeln("moving..."); | |
moveObject(c1, c2); | |
writeln("done"); | |
assert(c1.id == 0); | |
assert(r1.id == 0); | |
assert(c2.id == 1); | |
assert(r2.id == 1); | |
} | |
class C | |
{ | |
int id = 0; | |
this(int id) { writeln("-> ", this.id = id); } | |
~this() { writeln("<- ", this.id ); } | |
} | |
//----------------------------------------------------------------------------// | |
extern(C) @safe Object _d_newclass(ClassInfo); | |
@system void moveObject(From, To)(ref From from, ref To to) | |
if (is(From == class) && is(To == class)) | |
in | |
{ | |
assert(from !is null); | |
assert(from !is to); | |
} | |
out | |
{ | |
assert(from !is to ); | |
assert(to !is null); | |
} | |
body | |
{ | |
if (to is null) | |
{ | |
to = cast(To) _d_newclass(from.classinfo); | |
} | |
else | |
{ | |
assert(from.classinfo is to.classinfo); | |
auto sacrifice = to; | |
clear(sacrifice); | |
} | |
assert(from.classinfo is to.classinfo); | |
layout(to)[] = layout(from)[]; | |
initialize(from); | |
} | |
@system void initialize(Object obj) | |
{ | |
auto ci = obj.classinfo; | |
const init = ci.init; | |
auto buf = (cast(void*) obj)[0 .. init.length]; | |
buf[] = init[]; | |
if (auto defaultCtor = cast(void function(Object)) ci.defaultConstructor) | |
defaultCtor(obj); | |
} | |
@system void[] layout(Object obj) | |
{ | |
auto ci = obj.classinfo; | |
const init = ci.init; | |
return (cast(void*) obj)[0 .. init.length]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment