BsIReflectable.h 3.4 KB

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