BsMonoClass.h 7.5 KB

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