BsIReflectable.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsAny.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Interface implemented by classes that provide run time type information.
  8. *
  9. * @note Any class implementing this interface must implement the "getRTTI" method, as well as
  10. * a static "getRTTIStatic" method, returning the same value as "getRTTI". Object returned by those
  11. * methods is used for retrieving actual RTTI data about the class.
  12. */
  13. class BS_UTILITY_EXPORT IReflectable
  14. {
  15. public:
  16. virtual ~IReflectable() {}
  17. /**
  18. * @brief Returns an interface you can use to access class' Run Time Type Information.
  19. *
  20. * @note You must derive your own version of RTTITypeBase, in which you
  21. * may encapsulate all reflection specific operations.
  22. */
  23. virtual RTTITypeBase* getRTTI() const = 0;
  24. /**
  25. * @brief Returns all classes deriving directly from IReflectable.
  26. */
  27. static Vector<RTTITypeBase*>& getDerivedClasses()
  28. {
  29. static Vector<RTTITypeBase*> mRTTIDerivedClasses;
  30. return mRTTIDerivedClasses;
  31. }
  32. /**
  33. * @brief Returns true if current RTTI class is derived from "base".
  34. * (Or if it is the same type as base)
  35. */
  36. bool isDerivedFrom(RTTITypeBase* base);
  37. /**
  38. * @brief Returns an unique type identifier of the class.
  39. */
  40. UINT32 getTypeId() const;
  41. /**
  42. * @brief Returns the type name of the class.
  43. *
  44. * @note Name is not necessarily unique.
  45. */
  46. const String& getTypeName() const;
  47. /**
  48. * @brief Creates an empty instance of a class from a type identifier.
  49. */
  50. static std::shared_ptr<IReflectable> createInstanceFromTypeId(UINT32 rttiTypeId);
  51. /**
  52. * @brief Internal method. Called by each type deriving from IReflectable,
  53. * on program load.
  54. */
  55. static void _registerDerivedClass(RTTITypeBase* derivedClass);
  56. /**
  57. * @brief Internal method. Returns class' RTTI type from type id.
  58. */
  59. static RTTITypeBase* _getRTTIfromTypeId(UINT32 rttiTypeId);
  60. /**
  61. * @brief Internal method. Checks if the provided type id is unique.
  62. */
  63. static bool _isTypeIdDuplicate(UINT32 typeId);
  64. protected:
  65. Any mRTTIData; // Temporary per-instance data storage used during various RTTI operations.
  66. // Needed since there is one RTTI class instance per type and sometimes we need per-instance data.
  67. };
  68. }