BsMonoClass.h 7.4 KB

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