BsSerializedObject.h 5.4 KB

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