BsMonoClass.h 7.5 KB

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