BsSerializedObject.h 5.7 KB

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