BsManagedSerializableDiff.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsScriptEnginePrerequisites.h"
  5. #include "BsIReflectable.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Handles creation and applying of managed diffs. A diff contains differences
  10. * between two objects of identical types. If the initial state of an object
  11. * is known the recorded differences can be saved and applied to the original
  12. * state to restore the modified object.
  13. *
  14. * Differences are recorded per primitive field in an object.
  15. * Complex objects are recursed. Special handling is implemented
  16. * to properly generate diffs for arrays, lists and dictionaries.
  17. *
  18. * All primitive types supported by managed serialization are supported.
  19. * (see ScriptPrimitiveType)
  20. */
  21. class BS_SCR_BE_EXPORT ManagedSerializableDiff : public IReflectable
  22. {
  23. public:
  24. /**
  25. * @brief A base class for all modifications recorded in a diff.
  26. */
  27. struct BS_SCR_BE_EXPORT Modification : public IReflectable
  28. {
  29. virtual ~Modification();
  30. /************************************************************************/
  31. /* RTTI */
  32. /************************************************************************/
  33. public:
  34. friend class ModificationRTTI;
  35. static RTTITypeBase* getRTTIStatic();
  36. virtual RTTITypeBase* getRTTI() const override;
  37. };
  38. /**
  39. * @brief Contains a modification of a specific field in an object along
  40. * with information about the field and its parent object.
  41. */
  42. struct BS_SCR_BE_EXPORT ModifiedField : public IReflectable
  43. {
  44. ModifiedField() { }
  45. ModifiedField(const ManagedSerializableTypeInfoPtr& parentType,
  46. const ManagedSerializableFieldInfoPtr& fieldType, const SPtr<Modification>& modification);
  47. ManagedSerializableTypeInfoPtr parentType; /**< Type of the parent object the field belongs to. */
  48. ManagedSerializableFieldInfoPtr fieldType; /**< Data type of the field. */
  49. SPtr<Modification> modification; /**< Recorded modification(s) on the field. */
  50. /************************************************************************/
  51. /* RTTI */
  52. /************************************************************************/
  53. public:
  54. friend class ModifiedFieldRTTI;
  55. static RTTITypeBase* getRTTIStatic();
  56. virtual RTTITypeBase* getRTTI() const override;
  57. };
  58. /**
  59. * @brief Represents a single modified array or list entry.
  60. */
  61. struct BS_SCR_BE_EXPORT ModifiedArrayEntry : public IReflectable
  62. {
  63. ModifiedArrayEntry() { }
  64. ModifiedArrayEntry(UINT32 idx, const SPtr<Modification>& modification);
  65. UINT32 idx; /**< Index of the array/list entry that is modified. */
  66. SPtr<Modification> modification; /**< Recorded modification(s) on the entry. */
  67. /************************************************************************/
  68. /* RTTI */
  69. /************************************************************************/
  70. public:
  71. friend class ModifiedArrayEntryRTTI;
  72. static RTTITypeBase* getRTTIStatic();
  73. virtual RTTITypeBase* getRTTI() const override;
  74. };
  75. /**
  76. * @brief Represents a single modified dictionary entry.
  77. */
  78. struct BS_SCR_BE_EXPORT ModifiedDictionaryEntry : public IReflectable
  79. {
  80. ModifiedDictionaryEntry() { }
  81. ModifiedDictionaryEntry(const ManagedSerializableFieldDataPtr& key, const SPtr<Modification>& modification);
  82. ManagedSerializableFieldDataPtr key; /**< Serialized value of the key for the modified entry. */
  83. SPtr<Modification> modification; /**< Recorded modification(s) on the dictionary entry value. */
  84. /************************************************************************/
  85. /* RTTI */
  86. /************************************************************************/
  87. public:
  88. friend class ModifiedArrayEntryRTTI;
  89. static RTTITypeBase* getRTTIStatic();
  90. virtual RTTITypeBase* getRTTI() const override;
  91. };
  92. /**
  93. * @brief Contains data about all modifications in a single complex object.
  94. * (aside from arrays, list, dictionaries which are handled specially).
  95. */
  96. struct BS_SCR_BE_EXPORT ModifiedObject : Modification
  97. {
  98. static SPtr<ModifiedObject> create();
  99. Vector<ModifiedField> entries; /**< A list of entries containing each modified field in the object. */
  100. /************************************************************************/
  101. /* RTTI */
  102. /************************************************************************/
  103. public:
  104. friend class ModifiedObjectRTTI;
  105. static RTTITypeBase* getRTTIStatic();
  106. virtual RTTITypeBase* getRTTI() const override;
  107. };
  108. /**
  109. * @brief Contains data about all modifications in an array or a list.
  110. */
  111. struct BS_SCR_BE_EXPORT ModifiedArray : Modification
  112. {
  113. static SPtr<ModifiedArray> create();
  114. Vector<ModifiedArrayEntry> entries; /**< A list of all modified array/list entries along with their indices. */
  115. Vector<UINT32> origSizes; /**< Original size of the array/list (one size per dimension). */
  116. Vector<UINT32> newSizes; /**< New size of the array/list (one size per dimension). */
  117. /************************************************************************/
  118. /* RTTI */
  119. /************************************************************************/
  120. public:
  121. friend class ModifiedArrayRTTI;
  122. static RTTITypeBase* getRTTIStatic();
  123. virtual RTTITypeBase* getRTTI() const override;
  124. };
  125. /**
  126. * @brief Contains data about all modifications in a dictionary.
  127. */
  128. struct BS_SCR_BE_EXPORT ModifiedDictionary : Modification
  129. {
  130. static SPtr<ModifiedDictionary> create();
  131. Vector<ModifiedDictionaryEntry> entries; /**< A list of modified entries in the dictionary. */
  132. Vector<ManagedSerializableFieldDataPtr> removed; /**< A list of keys for entries that were removed from the dictionary. */
  133. /************************************************************************/
  134. /* RTTI */
  135. /************************************************************************/
  136. public:
  137. friend class ModifiedDictionaryRTTI;
  138. static RTTITypeBase* getRTTIStatic();
  139. virtual RTTITypeBase* getRTTI() const override;
  140. };
  141. /**
  142. * @brief Contains data about modification of a primitive field.
  143. * (i.e. fields new value)
  144. */
  145. struct BS_SCR_BE_EXPORT ModifiedEntry : Modification
  146. {
  147. ModifiedEntry() { }
  148. ModifiedEntry(const ManagedSerializableFieldDataPtr& value);
  149. static SPtr<ModifiedEntry> create(const ManagedSerializableFieldDataPtr& value);
  150. ManagedSerializableFieldDataPtr value;
  151. /************************************************************************/
  152. /* RTTI */
  153. /************************************************************************/
  154. public:
  155. friend class ModifiedEntryRTTI;
  156. static RTTITypeBase* getRTTIStatic();
  157. virtual RTTITypeBase* getRTTI() const override;
  158. };
  159. public:
  160. ManagedSerializableDiff();
  161. ~ManagedSerializableDiff();
  162. /**
  163. * @brief Generates a new managed diff object by comparing two objects of the same type. Callers must
  164. * ensure both objects are not null and of identical types.
  165. *
  166. * @param oldObj Original object. This is the object you can apply the diff to to convert it to \p newObj.
  167. * @param newObj New modified object. Any values in this object that differ from the original object will be
  168. * recorded in the diff.
  169. *
  170. * @return Returns null if objects are identical.
  171. */
  172. static ManagedSerializableDiffPtr create(const ManagedSerializableObjectPtr& oldObj, const ManagedSerializableObjectPtr& newObj);
  173. /**
  174. * @brief Applies the diff data stored in this object to the specified object, modifying all
  175. * fields in the object to correspond to the stored diff data.
  176. */
  177. void apply(const ManagedSerializableObjectPtr& obj);
  178. private:
  179. /**
  180. * @brief Recursively generates a diff between all fields of the specified objects. Returns null if objects are identical.
  181. */
  182. SPtr<ModifiedObject> generateDiff(const ManagedSerializableObjectPtr& oldObj, const ManagedSerializableObjectPtr& newObj);
  183. /**
  184. * @brief Generates a diff between two fields. Fields can be of any type and the system will generate the diff appropriately.
  185. * Diff is generated recursively on all complex objects as well. Returns null if fields contain identical data.
  186. */
  187. SPtr<Modification> generateDiff(const ManagedSerializableFieldDataPtr& oldData, const ManagedSerializableFieldDataPtr& newData,
  188. UINT32 fieldTypeId);
  189. /**
  190. * @brief Applies an object modification to a managed object. Modifications are applied recursively.
  191. *
  192. * @param mod Object modification to apply.
  193. * @param obj Object to apply the modification to.
  194. *
  195. * @return New field data in the case modification needed the object to be re-created instead of just modified.
  196. */
  197. ManagedSerializableFieldDataPtr applyDiff(const SPtr<ModifiedObject>& mod, const ManagedSerializableObjectPtr& obj);
  198. /**
  199. * @brief Applies an array modification to a managed array. Modifications are applied recursively.
  200. *
  201. * @param mod Array modification to apply.
  202. * @param obj Array to apply the modification to.
  203. *
  204. * @return New field data in the case modification needed the array to be re-created instead of just modified.
  205. */
  206. ManagedSerializableFieldDataPtr applyDiff(const SPtr<ModifiedArray>& mod, const ManagedSerializableArrayPtr& obj);
  207. /**
  208. * @brief Applies an list modification to a managed list. Modifications are applied recursively.
  209. *
  210. * @param mod List modification to apply.
  211. * @param obj List to apply the modification to.
  212. *
  213. * @return New field data in the case modification needed the list to be re-created instead of just modified.
  214. */
  215. ManagedSerializableFieldDataPtr applyDiff(const SPtr<ModifiedArray>& mod, const ManagedSerializableListPtr& obj);
  216. /**
  217. * @brief Applies an dictionary modification to a managed dictionary. Modifications are applied recursively.
  218. *
  219. * @param mod Dictionary modification to apply.
  220. * @param obj Dictionary to apply the modification to.
  221. *
  222. * @return New field data in the case modification needed the dictionary to be re-created instead of just modified.
  223. */
  224. ManagedSerializableFieldDataPtr applyDiff(const SPtr<ModifiedDictionary>& mod, const ManagedSerializableDictionaryPtr& obj);
  225. /**
  226. * @brief Applies a modification to a single field. Field type is determined and the modification is applied
  227. * to the specific field type as needed. Modifications are applied recursively.
  228. *
  229. * @param mod Modification to apply.
  230. * @param fieldType Type of the field we're applying the modification to.
  231. * @param origData Original data of the field.
  232. *
  233. * @return New field data in the case modification needed the field data to be re-created instead of just modified.
  234. */
  235. ManagedSerializableFieldDataPtr applyDiff(const SPtr<Modification>& mod, const ManagedSerializableTypeInfoPtr& fieldType,
  236. const ManagedSerializableFieldDataPtr& origData);
  237. SPtr<ModifiedObject> mModificationRoot;
  238. /************************************************************************/
  239. /* RTTI */
  240. /************************************************************************/
  241. public:
  242. friend class ManagedSerializableDiffRTTI;
  243. static RTTITypeBase* getRTTIStatic();
  244. virtual RTTITypeBase* getRTTI() const override;
  245. };
  246. }