Type.hx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (C)2005-2012 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. /**
  23. The diffent possible runtime types of a value.
  24. See [Type] for the haXe Reflection API.
  25. **/
  26. enum ValueType {
  27. TNull;
  28. TInt;
  29. TFloat;
  30. TBool;
  31. TObject;
  32. TFunction;
  33. TClass( c : Class<Dynamic> );
  34. TEnum( e : Enum<Dynamic> );
  35. TUnknown;
  36. }
  37. /**
  38. The haxe Reflection API allows retrieval of type information at runtime.
  39. This class complements the more lightweight Reflect class, with a focus on
  40. class and enum instances.
  41. **/
  42. extern class Type {
  43. /**
  44. Returns the class of [o], if [o] is a class instance.
  45. If [o] is null or of a different type, null is returned.
  46. In general, type parameter information cannot be obtained at runtime.
  47. **/
  48. public static function getClass<T>( o : T ) : Class<T>;
  49. /**
  50. Returns the enum of enum instance [o].
  51. An enum instance is the result of using an enum constructor. Given an
  52. enum Color { Red; }, getEnum(Red) returns Enum<Color>.
  53. If [o] is null, null is returned.
  54. In general, type parameter information cannot be obtained at runtime.
  55. **/
  56. public static function getEnum( o : EnumValue ) : Enum<Dynamic>;
  57. /**
  58. Returns the super-class of class [c].
  59. If [c] has no super class, null is returned.
  60. If [c] is null, the result is unspecified.
  61. In general, type parameter information cannot be obtained at runtime.
  62. **/
  63. public static function getSuperClass( c : Class<Dynamic> ) : Class<Dynamic>;
  64. /**
  65. Returns the name of class [c], including its path.
  66. If [c] is inside a package, the package structure is returned dot-
  67. separated, with another dot separating the class name:
  68. pack1.pack2.(...).packN.ClassName
  69. If [c] is a sub-type of a haxe module, that module is not part of the
  70. package structure.
  71. If [c] has no package, the class name is returned.
  72. If [c] is null, the result is unspecified.
  73. The class name does not include any type parameters.
  74. **/
  75. public static function getClassName( c : Class<Dynamic> ) : String;
  76. /**
  77. Returns the name of enum [e], including its path.
  78. If [e] is inside a package, the package structure is returned dot-
  79. separated, with another dot separating the enum name:
  80. pack1.pack2.(...).packN.EnumName
  81. If [e] is a sub-type of a haxe module, that module is not part of the
  82. package structure.
  83. If [e] has no package, the enum name is returned.
  84. If [e] is null, the result is unspecified.
  85. The enum name does not include any type parameters.
  86. **/
  87. public static function getEnumName( e : Enum<Dynamic> ) : String;
  88. /**
  89. Resolves a class by name.
  90. If [name] is the path of an existing class, that class is returned.
  91. Otherwise null is returned.
  92. If [name] is null or the path to a different type, the result is
  93. unspecified.
  94. The class name must not include any type parameters.
  95. **/
  96. public static function resolveClass( name : String ) : Class<Dynamic>;
  97. /**
  98. Resolves an enum by name.
  99. If [name] is the path of an existing enum, that enum is returned.
  100. Otherwise null is returned.
  101. If [name] is null the result is unspecified.
  102. If [name] is the path to a different type, null is returned.
  103. The enum name must not include any type parameters.
  104. **/
  105. public static function resolveEnum( name : String ) : Enum<Dynamic>;
  106. /**
  107. Creates an instance of class [cl], using [args] as arguments to the
  108. class constructor.
  109. This function guarantees that the class constructor is called.
  110. Default values of constructors arguments are not guaranteed to be
  111. taken into account.
  112. If [cl] or [args] are null, or if the number of elements in [args] does
  113. not match the expected number of constructor arguments, or if any
  114. argument has an invalid type, or if [cl] has no own constructor, the
  115. result is unspecified.
  116. In particular, default values of constructor arguments are not
  117. guaranteed to be taken into account.
  118. **/
  119. public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T;
  120. /**
  121. Creates an instance of class [cl].
  122. This function guarantees that the class constructor is not called.
  123. If [cl] is null, the result is unspecified.
  124. **/
  125. public static function createEmptyInstance<T>( cl : Class<T> ) : T;
  126. /**
  127. Creates an instance of enum [e] by calling its constructor [constr] with
  128. arguments [params].
  129. If [e] or [constr] is null, or if enum [e] has no constructor named
  130. [constr], or if the number of elements in [params] does not match the
  131. expected number of constructor arguments, or if any argument has an
  132. invalid type, the result is unspecified.
  133. **/
  134. public static function createEnum<T>( e : Enum<T>, constr : String, ?params : Array<Dynamic> ) : T;
  135. /**
  136. Creates an instance of enum [e] by calling its constructor number
  137. [index] with arguments [params].
  138. The constructor indices are preserved from haxe syntax, so the first
  139. declared is index 0, the next index 1 etc.
  140. If [e] or [constr] is null, or if enum [e] has no constructor named
  141. [constr], or if the number of elements in [params] does not match the
  142. expected number of constructor arguments, or if any argument has an
  143. invalid type, the result is unspecified.
  144. **/
  145. public static function createEnumIndex<T>( e : Enum<T>, index : Int, ?params : Array<Dynamic> ) : T;
  146. /**
  147. Returns a list of the instance fields of class [c].
  148. This only includes fields which are known at compile-time. In
  149. particular, using getInstanceFields(getClass(obj)) will not include
  150. any fields which were added to obj at runtime.
  151. The order of the fields in the returned Array is unspecified.
  152. If [c] is null, the result is unspecified.
  153. (As3) This method only returns instance fields that are public.
  154. **/
  155. public static function getInstanceFields( c : Class<Dynamic> ) : Array<String>;
  156. /**
  157. Returns a list of static fields of class [c].
  158. This does not include static fields of parent classes.
  159. The order of the fields in the returned Array is unspecified.
  160. If [c] is null, the result is unspecified.
  161. (As3) This method only returns class fields that are public.
  162. **/
  163. public static function getClassFields( c : Class<Dynamic> ) : Array<String>;
  164. /**
  165. Returns a list of the names of all constructors of enum [e].
  166. The order of the constructor names in the returned Array is preserved
  167. from the original syntax.
  168. If [c] is null, the result is unspecified.
  169. **/
  170. public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String>;
  171. /**
  172. Returns the runtime type of value [v].
  173. The result corresponds to the type [v] has at runtime, which may vary
  174. per platform. Assumptions regarding this should be minimized to avoid
  175. surprises.
  176. **/
  177. public static function typeof( v : Dynamic ) : ValueType;
  178. /**
  179. Recursively compares two enum instances [a] and [b] by value.
  180. Unlike [a] == [b], this function performs a deep equality check on the
  181. arguments of the constructors, if exists.
  182. If [a] or [b] are null, the result is unspecified.
  183. **/
  184. public static function enumEq<T:EnumValue>( a : T, b : T ) : Bool;
  185. /**
  186. Returns the constructor name of enum instance [e].
  187. The result String does not contain any constructor arguments.
  188. If [e] is null, the result is unspecified.
  189. **/
  190. public static function enumConstructor( e : EnumValue ) : String;
  191. /**
  192. Returns a list of the constructor arguments of enum instance [e].
  193. If [e] has no arguments, the result is [].
  194. Otherwise the result are the values that were used as arguments to [e],
  195. in the order of their declaration.
  196. If [e] is null, the result is unspecified.
  197. **/
  198. public static function enumParameters( e : EnumValue ) : Array<Dynamic>;
  199. /**
  200. Returns the index of enum instance [e].
  201. This corresponds to the original syntactic position of [e]. The index of
  202. the first declared constructor is 0, the next one is 1 etc.
  203. If [e] is null, the result is unspecified.
  204. **/
  205. public static function enumIndex( e : EnumValue ) : Int;
  206. /**
  207. Returns a list of all constructors of enum [e] that require no
  208. arguments.
  209. This may return the empty Array [] if all constructors of [e] require
  210. arguments.
  211. Otherwise an instance of [e] constructed through each of its non-
  212. argument constructors is returned, in the order of the constructor
  213. declaration.
  214. If [e] is null, the result is unspecified.
  215. **/
  216. public static function allEnums<T>( e : Enum<T> ) : Array<T>;
  217. }