BsBinaryCloner.h 2.2 KB

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