Reflect.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C)2005-2019 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. import cpp.ObjectType;
  23. @:coreApi
  24. @:analyzer(ignore)
  25. 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, untyped __cpp__("::hx::paccNever"));
  31. }
  32. public static function setField(o:Dynamic, field:String, value:Dynamic):Void untyped {
  33. if (o != null)
  34. o.__SetField(field, value, untyped __cpp__("::hx::paccNever"));
  35. }
  36. public static function getProperty(o:Dynamic, field:String):Dynamic {
  37. return (o == null) ? null : o.__Field(field, untyped __cpp__("::hx::paccAlways"));
  38. }
  39. public static function setProperty(o:Dynamic, field:String, value:Dynamic):Void {
  40. if (o != null)
  41. o.__SetField(field, value, untyped __cpp__("::hx::paccAlways"));
  42. }
  43. public static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic untyped {
  44. if (func != null && func.__GetType() == ObjectType.vtString) {
  45. if (o == null)
  46. throw cpp.ErrorConstants.invalidObject;
  47. func = o.__Field(func, untyped __cpp__("::hx::paccDynamic"));
  48. }
  49. if (func == null)
  50. throw cpp.ErrorConstants.nullFunctionPointer;
  51. untyped func.__SetThis(o);
  52. return untyped func.__Run(args);
  53. }
  54. public static function fields(o:Dynamic):Array<String> untyped {
  55. if (o == null)
  56. return new Array();
  57. var a:Array<String> = [];
  58. o.__GetFields(a);
  59. return a;
  60. }
  61. public static function isFunction(f:Dynamic):Bool untyped {
  62. return f != null && f.__GetType() == ObjectType.vtFunction;
  63. }
  64. public static function compare<T>(a:T, b:T):Int {
  65. return (a == b) ? 0 : (((a : Dynamic) > (b : Dynamic)) ? 1 : -1);
  66. }
  67. public static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
  68. if (f1 == f2)
  69. return true;
  70. if (!isFunction(f1) || !isFunction(f2))
  71. return false;
  72. return untyped __global__.__hxcpp_same_closure(f1, f2);
  73. }
  74. public static function isObject(v:Dynamic):Bool untyped {
  75. if (v == null)
  76. return false;
  77. var t:Int = v.__GetType();
  78. return t == ObjectType.vtObject || t == ObjectType.vtClass || t == ObjectType.vtString || t == ObjectType.vtArray;
  79. }
  80. public static function isEnumValue(v:Dynamic):Bool untyped {
  81. return v != null && v.__GetType() == ObjectType.vtEnum;
  82. }
  83. public static function deleteField(o:Dynamic, field:String):Bool untyped {
  84. if (o == null)
  85. return false;
  86. return untyped __global__.__hxcpp_anon_remove(o, field);
  87. }
  88. public static function copy<T>(o:Null<T>):Null<T> {
  89. if (o == null)
  90. return null;
  91. if (untyped o.__GetType() == ObjectType.vtString)
  92. return o;
  93. if (untyped o.__GetType() == ObjectType.vtArray)
  94. return untyped o.__Field("copy", untyped __cpp__("::hx::paccDynamic"))();
  95. var o2:Dynamic = {};
  96. for (f in Reflect.fields(o))
  97. Reflect.setField(o2, f, Reflect.field(o, f));
  98. return o2;
  99. }
  100. public static function makeVarArgs<T>(f:Array<Dynamic>->T):Dynamic {
  101. return untyped __global__.__hxcpp_create_var_args(f);
  102. }
  103. }