BsSerializedObject.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsIReflectable.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Serialization
  7. * @{
  8. */
  9. /** Base class for intermediate representations of objects that are being decoded with BinarySerializer. */
  10. struct BS_UTILITY_EXPORT SerializedInstance : IReflectable
  11. {
  12. virtual ~SerializedInstance() { }
  13. /**
  14. * Performs a deep clone of this object any any potential child objects.
  15. *
  16. * @param[in] cloneData If true the data contained by the objects will be cloned as well, instead of just
  17. * meta-data. If false then both the original and the cloned instances will point to the
  18. * same instances of data. The original will retain data ownership and it will go out of
  19. * scope when the original does.
  20. */
  21. virtual SPtr<SerializedInstance> clone(bool cloneData = true) = 0;
  22. /************************************************************************/
  23. /* RTTI */
  24. /************************************************************************/
  25. public:
  26. friend class SerializedInstanceRTTI;
  27. static RTTITypeBase* getRTTIStatic();
  28. virtual RTTITypeBase* getRTTI() const override;
  29. };
  30. /** An intermediate serialized data for a single field in an object. */
  31. struct BS_UTILITY_EXPORT SerializedEntry : IReflectable
  32. {
  33. SerializedEntry()
  34. :fieldId(0), serialized(nullptr)
  35. { }
  36. UINT32 fieldId;
  37. SPtr<SerializedInstance> serialized;
  38. /************************************************************************/
  39. /* RTTI */
  40. /************************************************************************/
  41. public:
  42. friend class SerializedEntryRTTI;
  43. static RTTITypeBase* getRTTIStatic();
  44. virtual RTTITypeBase* getRTTI() const override;
  45. };
  46. /** A serialized value representing a single entry in an array. */
  47. struct BS_UTILITY_EXPORT SerializedArrayEntry : IReflectable
  48. {
  49. SerializedArrayEntry()
  50. :index(0), serialized(nullptr)
  51. { }
  52. UINT32 index;
  53. SPtr<SerializedInstance> serialized;
  54. /************************************************************************/
  55. /* RTTI */
  56. /************************************************************************/
  57. public:
  58. friend class SerializedArrayEntryRTTI;
  59. static RTTITypeBase* getRTTIStatic();
  60. virtual RTTITypeBase* getRTTI() const override;
  61. };
  62. /**
  63. * A serialized portion of an object belonging to a specific class in a class hierarchy. Consists of multiple entries,
  64. * one for each field.
  65. */
  66. struct BS_UTILITY_EXPORT SerializedSubObject : IReflectable
  67. {
  68. SerializedSubObject()
  69. :typeId(0)
  70. { }
  71. UINT32 typeId;
  72. UnorderedMap<UINT32, SerializedEntry> entries;
  73. /************************************************************************/
  74. /* RTTI */
  75. /************************************************************************/
  76. public:
  77. friend class SerializedSubObjectRTTI;
  78. static RTTITypeBase* getRTTIStatic();
  79. virtual RTTITypeBase* getRTTI() const override;
  80. };
  81. /** A serialized object consisting of multiple sub-objects, one for each inherited class. */
  82. struct BS_UTILITY_EXPORT SerializedObject : SerializedInstance
  83. {
  84. /** Returns the RTTI type ID for the most-derived class of this object. */
  85. UINT32 getRootTypeId() const { return subObjects[0].typeId; }
  86. /** @copydoc SerializedInstance::clone */
  87. SPtr<SerializedInstance> clone(bool cloneData = true) override;
  88. Vector<SerializedSubObject> subObjects;
  89. /************************************************************************/
  90. /* RTTI */
  91. /************************************************************************/
  92. public:
  93. friend class SerializedObjectRTTI;
  94. static RTTITypeBase* getRTTIStatic();
  95. virtual RTTITypeBase* getRTTI() const override;
  96. };
  97. /** Contains data for a serialized value of a specific field or array entry. */
  98. struct BS_UTILITY_EXPORT SerializedField : SerializedInstance
  99. {
  100. SerializedField()
  101. :value(nullptr), size(0), ownsMemory(false)
  102. {
  103. }
  104. ~SerializedField()
  105. {
  106. if (ownsMemory && value != nullptr)
  107. bs_free(value);
  108. }
  109. /** @copydoc SerializedInstance::clone */
  110. SPtr<SerializedInstance> clone(bool cloneData = true) override;
  111. UINT8* value;
  112. UINT32 size;
  113. bool ownsMemory;
  114. /************************************************************************/
  115. /* RTTI */
  116. /************************************************************************/
  117. public:
  118. friend class SerializedFieldRTTI;
  119. static RTTITypeBase* getRTTIStatic();
  120. virtual RTTITypeBase* getRTTI() const override;
  121. };
  122. /** A serialized array containing a list of all its entries. */
  123. struct BS_UTILITY_EXPORT SerializedArray : SerializedInstance
  124. {
  125. SerializedArray()
  126. :numElements(0)
  127. { }
  128. /** @copydoc SerializedInstance::clone */
  129. SPtr<SerializedInstance> clone(bool cloneData = true) override;
  130. UnorderedMap<UINT32, SerializedArrayEntry> entries;
  131. UINT32 numElements;
  132. /************************************************************************/
  133. /* RTTI */
  134. /************************************************************************/
  135. public:
  136. friend class SerializedArrayRTTI;
  137. static RTTITypeBase* getRTTIStatic();
  138. virtual RTTITypeBase* getRTTI() const override;
  139. };
  140. /** @} */
  141. }