BsMonoClass.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsMonoPrerequisites.h"
  5. #include <mono/jit/jit.h>
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Contains information about a single Mono (i.e. managed) class.
  10. */
  11. class BS_MONO_EXPORT MonoClass
  12. {
  13. /**
  14. * @brief Used for uniquely identifying a method in a managed class, normally for use
  15. * in containers.
  16. */
  17. struct MethodId
  18. {
  19. struct Hash
  20. {
  21. inline size_t operator()(const MethodId& v) const;
  22. };
  23. struct Equals
  24. {
  25. inline bool operator()(const MethodId &a, const MethodId &b) const;
  26. };
  27. MethodId(const String& name, UINT32 numParams);
  28. String name;
  29. UINT32 numParams;
  30. };
  31. public:
  32. ~MonoClass();
  33. /**
  34. * @brief Returns the namespace of this class.
  35. */
  36. const String& getNamespace() const { return mNamespace; }
  37. /**
  38. * @brief Returns the type name of this class.
  39. */
  40. const String& getTypeName() const { return mTypeName; }
  41. /**
  42. * @brief Returns the full name (Namespace::TypeName) of this class.
  43. */
  44. const String& getFullName() const { return mFullName; }
  45. /**
  46. * @brief Returns an object referencing a method with the specified name and number of parameters.
  47. *
  48. * @note If the method is overloaded then you should use "getMethodExact".
  49. * Does not query base class methods.
  50. * Returns null if method cannot be found.
  51. */
  52. MonoMethod* getMethod(const String& name, UINT32 numParams = 0) const;
  53. /**
  54. * @brief Returns an object referencing a field with the specified name.
  55. * Does not query base class fields.
  56. * Returns null if field cannot be found.
  57. */
  58. MonoField* getField(const String& name) const;
  59. /**
  60. * @brief Returns an object referencing a property with the specified name.
  61. * Does not query base class properties.
  62. * Throws an exception if the property cannot be found.
  63. */
  64. MonoProperty& getProperty(const String& name);
  65. /**
  66. * @brief Returns an instance of an attribute of the specified "monoClass" that is part
  67. * of this class. Returns null if this class type does not have that type of attribute.
  68. */
  69. MonoObject* getAttribute(MonoClass* monoClass) const;
  70. /**
  71. * @brief Returns the base class of this class. Null if this class has no base.
  72. */
  73. MonoClass* getBaseClass() const;
  74. /**
  75. * @brief Returns an object referencing a method, expects exact method name with parameters.
  76. *
  77. * @note Does not query base class methods.
  78. * Returns null if method cannot be found.
  79. * Example: name = "CreateInstance", signature = "Vector2,int[]"
  80. */
  81. MonoMethod* getMethodExact(const String& name, const String& signature) const;
  82. /**
  83. * @brief Returns all fields belonging to this class.
  84. *
  85. * @note Be aware this will not include the fields of any base classes.
  86. */
  87. const Vector<MonoField*>& getAllFields() const;
  88. /**
  89. * @brief Returns all methods belonging to this class.
  90. *
  91. * @note Be aware this will not include the methods of any base classes.
  92. */
  93. const Vector<MonoMethod*>& getAllMethods() const;
  94. /**
  95. * @brief Gets all attributes applied to this class.
  96. */
  97. Vector<MonoClass*> getAllAttributes() const;
  98. /**
  99. * @brief Check if this class has an attribute of the type "monoClass".
  100. */
  101. bool hasAttribute(MonoClass* monoClass) const;
  102. /**
  103. * @brief Check if this class has a field with the specified name. Does not
  104. * check base classes.
  105. */
  106. bool hasField(const String& name) const;
  107. /**
  108. * @brief Checks if this class is a sub class of the specified class.
  109. */
  110. bool isSubClassOf(const MonoClass* monoClass) const;
  111. /**
  112. * @brief Checks is the provided object instance of this class' type.
  113. */
  114. bool isInstanceOfType(MonoObject* object) const;
  115. /**
  116. * @brief Shortcut for invoking a method on a class. Invokes a method with the provided name
  117. * and number of parameters.
  118. *
  119. * @param name Name of the method to invoke (no parameter list or brackets.
  120. * @param instance Object instance on invoke the method on. Null if method is static.
  121. * @param params Array containing pointers to method parameters. Array length must be equal to number of parameters.
  122. * Can be null if method has no parameters. For value types parameters should be pointers to the values
  123. * and for reference types they should be pointers to MonoObject.
  124. * @param numParams Number of parameters the method accepts.
  125. *
  126. * @note You cannot use this to call overloaded methods that have the same number of parameters. Use "getMethodExact" and then
  127. * invoke the method from the returned method object.
  128. */
  129. MonoObject* invokeMethod(const String& name, MonoObject* instance = nullptr, void** params = nullptr, UINT32 numParams = 0);
  130. /**
  131. * @brief Hooks up an internal call that will trigger the provided method callback when the managed method with the
  132. * specified name is called. If name is not valid this will silently fail.
  133. */
  134. void addInternalCall(const String& name, const void* method);
  135. /**
  136. * @brief Returns the internal mono representation of the class.
  137. */
  138. ::MonoClass* _getInternalClass() const { return mClass; }
  139. /**
  140. * @brief Creates a new instance of this class and optionally constructs it.
  141. * If you don't construct the instance then you should invoke the ".ctor" method manually
  142. * afterwards.
  143. */
  144. MonoObject* createInstance(bool construct = true) const;
  145. /**
  146. * @brief Creates a new instance of this class and then constructs it using the constructor
  147. * with the specified number of parameters.
  148. *
  149. * @param params Array containing pointers to constructor parameters. Array length must be equal to number of parameters.
  150. * @param numParams Number of parameters the constructor accepts.
  151. *
  152. * @note If the class have multiple constructors with the same number of parameters use the other "createInstance" overload
  153. * that allows you to provide exact signature.
  154. */
  155. MonoObject* createInstance(void** params, UINT32 numParams);
  156. /**
  157. * @brief Creates a new instance of this class and then constructs it using the constructor
  158. * with the specified signature.
  159. *
  160. * @param ctorSignature Method signature. Example: "Vector2,int[]"
  161. * @param params Array containing pointers to constructor parameters. Array length must be equal to number of parameters.
  162. */
  163. MonoObject* createInstance(const String& ctorSignature, void** params);
  164. private:
  165. friend class MonoAssembly;
  166. /**
  167. * @brief Constructs a new mono class object.
  168. *
  169. * @param ns Namespace the class belongs to.
  170. * @param type Type name of the class.
  171. * @param monoClass Internal mono class.
  172. * @param parentAssembly Assembly to which this class belongs.
  173. */
  174. MonoClass(const String& ns, const String& type, ::MonoClass* monoClass, const MonoAssembly* parentAssembly);
  175. const MonoAssembly* mParentAssembly;
  176. ::MonoClass* mClass;
  177. String mNamespace;
  178. String mTypeName;
  179. String mFullName;
  180. mutable UnorderedMap<MethodId, MonoMethod*, MethodId::Hash, MethodId::Equals> mMethods;
  181. mutable UnorderedMap<String, MonoField*> mFields;
  182. UnorderedMap<String, MonoProperty*> mProperties;
  183. mutable bool mHasCachedFields;
  184. mutable Vector<MonoField*> mCachedFieldList;
  185. mutable bool mHasCachedMethods;
  186. mutable Vector<MonoMethod*> mCachedMethodList;
  187. };
  188. }