Skip to content

Instantly share code, notes, and snippets.

@gamedevsam
Created July 26, 2013 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamedevsam/6091318 to your computer and use it in GitHub Desktop.
Save gamedevsam/6091318 to your computer and use it in GitHub Desktop.
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