Created
July 26, 2013 18:52
-
-
Save gamedevsam/6091318 to your computer and use it in GitHub Desktop.
Implementation taken from: http://haxe.org/forum/thread/3395#nabble-td2119917
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
package ; | |
class DeepCopy | |
{ | |
// http://haxe.org/forum/thread/3395#nabble-td2119917 | |
public static function copy<T>( v:T ) : T | |
{ | |
if (!Reflect.isObject(v)) // simple type | |
{ | |
return v; | |
} | |
else if( Std.is( v, Array ) ) // array | |
{ | |
var r = Type.createInstance(Type.getClass(v), []); | |
untyped | |
{ | |
for( ii in 0...v.length ) | |
r.push(deepCopy(v[ii])); | |
} | |
return r; | |
} | |
else if( Type.getClass(v) == null ) // anonymous object | |
{ | |
var obj : Dynamic = {}; | |
for( ff in Reflect.fields(v) ) | |
Reflect.setField(obj, ff, deepCopy(Reflect.field(v, ff))); | |
return obj; | |
} | |
else // class | |
{ | |
var obj = Type.createEmptyInstance(Type.getClass(v)); | |
for( ff in Reflect.fields(v) ) | |
Reflect.setField(obj, ff, deepCopy(Reflect.field(v, ff))); | |
return obj; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment