BsMonoClass.h 7.7 KB

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