1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "Prerequisites/BsPrerequisitesUtil.h"
6
7namespace bs
8{
9 /** @addtogroup Serialization
10 * @{
11 */
12
13 /** Helper class that performs cloning of an object that implements RTTI. */
14 class BS_UTILITY_EXPORT BinaryCloner
15 {
16 public:
17
18 /**
19 * Returns a copy of the provided object with identical data.
20 *
21 * @param[in] object Object to clone.
22 * @param[in] shallow If false then all referenced objects will be cloned as well, otherwise the references
23 * to the original objects will be kept.
24 */
25 static SPtr<IReflectable> clone(IReflectable* object, bool shallow = false);
26
27 private:
28 struct ObjectReferenceData;
29
30 /** Identifier representing a single field or an array entry in an object. */
31 struct FieldId
32 {
33 RTTIField* field;
34 INT32 arrayIdx;
35 };
36
37 /** A saved reference to an object with a field identifier that owns it. */
38 struct ObjectReference
39 {
40 FieldId fieldId;
41 SPtr<IReflectable> object;
42 };
43
44 /**
45 * Contains all object references in a portion of an object belonging to a specific class (base and derived
46 * classes count as separate sub-objects).
47 */
48 struct SubObjectReferenceData
49 {
50 RTTITypeBase* rtti;
51 Vector<ObjectReference> references;
52 Vector<ObjectReferenceData> children;
53 };
54
55 /**
56 * Contains all object references in an entire object, as well as the identifier of the field owning this object.
57 */
58 struct ObjectReferenceData
59 {
60 FieldId fieldId;
61 Vector<SubObjectReferenceData> subObjectData;
62 };
63
64 /**
65 * Iterates over the provided object hierarchy and retrieves all object references which are returned in
66 * @p referenceData output parameter, also in a hierarchical format for easier parsing.
67 */
68 static void gatherReferences(IReflectable* object, FrameAlloc& alloc, ObjectReferenceData& referenceData);
69
70 /**
71 * Restores a set of references retrieved by gatherReferences() and applies them to a specific object. Type of the
72 * object must be the same as the type that was used when calling gatherReferences().
73 */
74 static void restoreReferences(IReflectable* object, FrameAlloc& alloc, const ObjectReferenceData& referenceData);
75 };
76
77 /** @} */
78}