Reflect.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Reflect {
  26. public static function hasField( o : Dynamic, field : String ) : Bool untyped {
  27. return o!=null && o.__HasField(field);
  28. }
  29. public static function field( o : Dynamic, field : String ) : Dynamic untyped {
  30. return (o==null) ? null : o.__Field(field,false);
  31. }
  32. public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
  33. if (o!=null)
  34. o.__SetField(field,value,false);
  35. }
  36. public static inline function getProperty( o : Dynamic, field : String ) : Dynamic {
  37. return (o==null) ? null : o.__Field(field,true);
  38. }
  39. public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
  40. if (o!=null)
  41. o.__SetField(field,value,true);
  42. }
  43. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
  44. if (func!=null && func.__GetType()==__global__.vtString)
  45. func = o.__Field(func,true);
  46. untyped func.__SetThis(o);
  47. return untyped func.__Run(args);
  48. }
  49. public static function fields( o : Dynamic ) : Array<String> untyped {
  50. if( o == null ) return new Array();
  51. var a : Array<String> = [];
  52. o.__GetFields(a);
  53. return a;
  54. }
  55. public static function isFunction( f : Dynamic ) : Bool untyped {
  56. return f!=null && f.__GetType() == __global__.vtFunction;
  57. }
  58. public static function compare<T>( a : T, b : T ) : Int {
  59. return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
  60. }
  61. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
  62. if( f1 == f2 )
  63. return true;
  64. if( !isFunction(f1) || !isFunction(f2) )
  65. return false;
  66. return untyped __global__.__hxcpp_same_closure(f1,f2);
  67. }
  68. public static function isObject( v : Dynamic ) : Bool untyped {
  69. if (v==null) return false;
  70. var t:Int = v.__GetType();
  71. return t == __global__.vtObject || t==__global__.vtClass || t==__global__.vtString ||
  72. t==__global__.vtArray;
  73. }
  74. public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
  75. if (o==null) return false;
  76. return untyped __global__.__hxcpp_anon_remove(o,f);
  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",true)();
  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. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
  89. return untyped __global__.__hxcpp_create_var_args(f);
  90. }
  91. }