Reflect.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. @:coreApi class Reflect {
  23. public static function hasField( o : Dynamic, field : String ) : Bool untyped {
  24. return o!=null && o.__HasField(field);
  25. }
  26. public static function field( o : Dynamic, field : String ) : Dynamic untyped {
  27. return (o==null) ? null : o.__Field(field,untyped __cpp__("hx::paccNever") );
  28. }
  29. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  30. if (o!=null)
  31. o.__SetField(field,value,untyped __cpp__("hx::paccNever") );
  32. }
  33. public static function getProperty( o : Dynamic, field : String ) : Dynamic {
  34. return (o==null) ? null : o.__Field(field,untyped __cpp__("hx::paccAlways") );
  35. }
  36. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  37. if (o!=null)
  38. o.__SetField(field,value,untyped __cpp__("hx::paccAlways") );
  39. }
  40. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic untyped {
  41. if (func!=null && func.__GetType()==__global__.vtString)
  42. func = o.__Field(func,untyped __cpp__("hx::paccDynamic"));
  43. untyped func.__SetThis(o);
  44. return untyped func.__Run(args);
  45. }
  46. public static function fields( o : Dynamic ) : Array<String> untyped {
  47. if( o == null ) return new Array();
  48. var a : Array<String> = [];
  49. o.__GetFields(a);
  50. return a;
  51. }
  52. public static function isFunction( f : Dynamic ) : Bool untyped {
  53. return f!=null && f.__GetType() == __global__.vtFunction;
  54. }
  55. public static function compare<T>( a : T, b : T ) : Int {
  56. return ( a == b ) ? 0 : (((a:Dynamic) > (b:Dynamic)) ? 1 : -1);
  57. }
  58. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  59. if( f1 == f2 )
  60. return true;
  61. if( !isFunction(f1) || !isFunction(f2) )
  62. return false;
  63. return untyped __global__.__hxcpp_same_closure(f1,f2);
  64. }
  65. public static function isObject( v : Dynamic ) : Bool untyped {
  66. if (v==null) return false;
  67. var t:Int = v.__GetType();
  68. return t == __global__.vtObject || t==__global__.vtClass || t==__global__.vtString ||
  69. t==__global__.vtArray;
  70. }
  71. public static function isEnumValue( v : Dynamic ) : Bool untyped {
  72. return v!=null && v.__GetType() == __global__.vtEnum;
  73. }
  74. public static function deleteField( o : Dynamic, field : String ) : Bool untyped {
  75. if (o==null) return false;
  76. return untyped __global__.__hxcpp_anon_remove(o,field);
  77. }
  78. public static function copy<T>( o : T ) : T {
  79. if (o==null) return null;
  80. if(untyped o.__GetType()==__global__.vtString ) return o;
  81. if(untyped o.__GetType()==__global__.vtArray )
  82. return untyped o.__Field("copy", untyped __cpp__("hx::paccDynamic"))();
  83. var o2 : Dynamic = {};
  84. for( f in Reflect.fields(o) )
  85. Reflect.setField(o2,f,Reflect.field(o,f));
  86. return o2;
  87. }
  88. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  89. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  90. return untyped __global__.__hxcpp_create_var_args(f);
  91. }
  92. }