BsIReflectable.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsAny.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup RTTI
  7. * @{
  8. */
  9. /**
  10. * Interface implemented by classes that provide run time type information.
  11. *
  12. * @note
  13. * Any class implementing this interface must implement the getRTTI() method, as well as a static getRTTIStatic()
  14. * method, returning the same value as getRTTI(). Object returned by those methods is used for retrieving actual RTTI
  15. * data about the class.
  16. */
  17. class BS_UTILITY_EXPORT IReflectable
  18. {
  19. public:
  20. virtual ~IReflectable() {}
  21. /**
  22. * Returns an interface you can use to access class' Run Time Type Information.
  23. *
  24. * @note
  25. * You must derive your own version of RTTITypeBase, in which you may encapsulate all reflection specific operations.
  26. */
  27. virtual RTTITypeBase* getRTTI() const = 0;
  28. /** Returns all classes deriving directly from IReflectable. */
  29. static Vector<RTTITypeBase*>& getDerivedClasses()
  30. {
  31. static Vector<RTTITypeBase*> mRTTIDerivedClasses;
  32. return mRTTIDerivedClasses;
  33. }
  34. /** Returns true if current RTTI class is derived from @p base (Or if it is the same type as base). */
  35. bool isDerivedFrom(RTTITypeBase* base);
  36. /** Returns an unique type identifier of the class. */
  37. UINT32 getTypeId() const;
  38. /**
  39. * Returns the type name of the class.
  40. *
  41. * @note Name is not necessarily unique.
  42. */
  43. const String& getTypeName() const;
  44. /** Creates an empty instance of a class from a type identifier. */
  45. static std::shared_ptr<IReflectable> createInstanceFromTypeId(UINT32 rttiTypeId);
  46. /**
  47. * Called by each type deriving from IReflectable, on program load.
  48. *
  49. * @note Internal method.
  50. */
  51. static void _registerDerivedClass(RTTITypeBase* derivedClass);
  52. /**
  53. * Returns class' RTTI type from type id.
  54. *
  55. * @note Internal method.
  56. */
  57. static RTTITypeBase* _getRTTIfromTypeId(UINT32 rttiTypeId);
  58. /**
  59. * Checks if the provided type id is unique.
  60. *
  61. * @note Internal method.
  62. */
  63. static bool _isTypeIdDuplicate(UINT32 typeId);
  64. /**
  65. * Iterates over all RTTI types and reports any circular references (e.g. one type having a field referencing
  66. * another type, and that type having a field referencing the first type). Circular references are problematic
  67. * because when serializing the system cannot determine in which order they should be resolved. In that case user
  68. * should use RTTI_Flag_WeakRef to mark one of the references as weak. This flags tells the system that the reference
  69. * may be resolved in an undefined order, but also no longer guarantees that object assigned to that field during
  70. * deserialization will be fully deserialized itself, as that might be delayed to a later time.
  71. */
  72. static void _checkForCircularReferences();
  73. protected:
  74. Any mRTTIData; /**< Temporary per-instance data storage used during various RTTI operations.
  75. Needed since there is one RTTI class instance per type and sometimes we need per-instance data. */
  76. };
  77. /** @} */
  78. }