BsManagedSerializableObject.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "Reflection/BsIReflectable.h"
  6. #include "Serialization/BsManagedSerializableField.h"
  7. namespace bs
  8. {
  9. /** @addtogroup SBansheeEngine
  10. * @{
  11. */
  12. /**
  13. * Allows access to an underlying managed object, or a cached version of that object that can be
  14. * serialized/deserialized.
  15. *
  16. * @note
  17. * This class can be in two states:
  18. * - Linked - When the object has a link to a managed object. This is the default state when a new instance
  19. * of ManagedSerializableObject is created. Any operations during this state will operate directly
  20. * on the linked managed object. A GC handle will be kept to the linked managed object. The handle can
  21. * be freed by transfering to serialized mode or by destroying this object.
  22. * - Serialized - When the object has no link to the managed object but instead just contains cached object
  23. * and field data that may be used for initializing a managed object. Any operations during
  24. * this state will operate only on the cached internal data.
  25. *
  26. * You can transfer an object in linked state to serialized state by calling serialize(). If an object is in serialized
  27. * state you can call deserialize() to populated a managed object from the cached data.
  28. */
  29. class BS_SCR_BE_EXPORT ManagedSerializableObject : public IReflectable
  30. {
  31. private:
  32. struct ConstructPrivately {};
  33. /** Generates a hash value for field key data identifying a single field in the object. */
  34. struct BS_SCR_BE_EXPORT Hash
  35. {
  36. inline size_t operator()(const ManagedSerializableFieldKey& x) const;
  37. };
  38. /** Compares two field key objects. */
  39. struct BS_SCR_BE_EXPORT Equals
  40. {
  41. inline bool operator()(const ManagedSerializableFieldKey& a, const ManagedSerializableFieldKey& b) const;
  42. };
  43. public:
  44. ManagedSerializableObject(const ConstructPrivately& dummy, SPtr<ManagedSerializableObjectInfo> objInfo, MonoObject* managedInstance);
  45. ManagedSerializableObject(const ConstructPrivately& dummy);
  46. ~ManagedSerializableObject();
  47. /**
  48. * Returns the internal managed instance of the object. This will return null if the object is in serialized mode.
  49. */
  50. MonoObject* getManagedInstance() const;
  51. /** Returns the type information for the internal object. */
  52. SPtr<ManagedSerializableObjectInfo> getObjectInfo() const { return mObjInfo; }
  53. /**
  54. * Sets a new value of the specified field. Operates on managed object if in linked state, or on cached data
  55. * otherwise.
  56. *
  57. * @param[in] fieldInfo Object describing the field to which to set the value. This field must belong to the
  58. * type this object is initialized with.
  59. * @param[in] val Wrapper around the value to store in the field.
  60. */
  61. void setFieldData(const SPtr<ManagedSerializableMemberInfo>& fieldInfo, const SPtr<ManagedSerializableFieldData>& val);
  62. /**
  63. * Returns the value of the specified field. Operates on managed object if in linked state, or on cached data
  64. * otherwise.
  65. *
  66. * @param[in] fieldInfo Object describing the field to which to set the value. This field must belong to the
  67. * type this object is initialized with.
  68. * @return A wrapper around the value of the field.
  69. */
  70. SPtr<ManagedSerializableFieldData> getFieldData(const SPtr<ManagedSerializableMemberInfo>& fieldInfo) const;
  71. /**
  72. * Serializes the internal managed object into a set of cached data that can be saved in memory/disk and can be
  73. * deserialized later. The internal managed object will be freed (if no other references to it). Calling serialize()
  74. * again will have no result.
  75. */
  76. void serialize();
  77. /**
  78. * Deserializes a set of cached data into a managed object. This action may fail in case the cached data contains a
  79. * type that no longer exists in which case null is returned.
  80. *
  81. * @return Newly created object initialized with the cached data.
  82. */
  83. MonoObject* deserialize();
  84. /**
  85. * Deserializes a set of cached data into an existing managed object. Caller must ensure the provided object is of
  86. * proper type.
  87. *
  88. * @param[in] instance Existing managed instance of the same type this serializable object represents.
  89. * @param[in] objInfo Serializable object info for the managed object type.
  90. */
  91. void deserialize(MonoObject* instance, const SPtr<ManagedSerializableObjectInfo>& objInfo);
  92. /**
  93. * Creates a managed serializable object that references an existing managed object. Created object will be in
  94. * linked mode.
  95. *
  96. * @param[in] managedInstance Constructed managed instance of the object to link with.
  97. */
  98. static SPtr<ManagedSerializableObject> createFromExisting(MonoObject* managedInstance);
  99. /**
  100. * Creates a managed serializable object that creates and references a brand new managed object instance.
  101. *
  102. * @param[in] type Type of the object to create.
  103. */
  104. static SPtr<ManagedSerializableObject> createNew(const SPtr<ManagedSerializableTypeInfoObject>& type);
  105. /**
  106. * Creates a managed object instance.
  107. *
  108. * @param[in] type Type of the object to create.
  109. */
  110. static MonoObject* createManagedInstance(const SPtr<ManagedSerializableTypeInfoObject>& type);
  111. protected:
  112. uint32_t mGCHandle = 0;
  113. SPtr<ManagedSerializableObjectInfo> mObjInfo;
  114. UnorderedMap<ManagedSerializableFieldKey, SPtr<ManagedSerializableFieldData>, Hash, Equals> mCachedData;
  115. /************************************************************************/
  116. /* RTTI */
  117. /************************************************************************/
  118. /** Creates an empty and uninitialized object used for serialization purposes. */
  119. static SPtr<ManagedSerializableObject> createEmpty();
  120. public:
  121. friend class ManagedSerializableObjectRTTI;
  122. static RTTITypeBase* getRTTIStatic();
  123. RTTITypeBase* getRTTI() const override;
  124. };
  125. /** @} */
  126. }