BsBinaryCloner.h 2.3 KB

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