BsBinaryCloner.h 2.2 KB

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