BsIReflectable.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Utility/BsAny.h"
  6. namespace bs
  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 SPtr<IReflectable> createInstanceFromTypeId(UINT32 rttiTypeId);
  48. /** @name Internal
  49. * @{
  50. */
  51. /** Called by each type deriving from IReflectable, on program load. */
  52. static void _registerDerivedClass(RTTITypeBase* derivedClass);
  53. /** Returns class' RTTI type from type id. */
  54. static RTTITypeBase* _getRTTIfromTypeId(UINT32 rttiTypeId);
  55. /** Checks if the provided type id is unique. */
  56. static bool _isTypeIdDuplicate(UINT32 typeId);
  57. /**
  58. * Iterates over all RTTI types and reports any circular references (for example one type having a field referencing
  59. * another type, and that type having a field referencing the first type). Circular references are problematic
  60. * because when serializing the system cannot determine in which order they should be resolved. In that case user
  61. * should use RTTI_Flag_WeakRef to mark one of the references as weak. This flags tells the system that the reference
  62. * may be resolved in an undefined order, but also no longer guarantees that object assigned to that field during
  63. * deserialization will be fully deserialized itself, as that might be delayed to a later time.
  64. */
  65. static void _checkForCircularReferences();
  66. /** Returns an interface you can use to access class' Run Time Type Information. */
  67. static RTTITypeBase* getRTTIStatic();
  68. /** @} */
  69. protected:
  70. Any mRTTIData; /**< Temporary per-instance data storage used during various RTTI operations.
  71. Needed since there is one RTTI class instance per type and sometimes we need per-instance data. */
  72. };
  73. /** @} */
  74. }