BsManagedSerializableObject.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "BsIReflectable.h"
  6. #include "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.
  21. * - Serialized - When the object has no link to the managed object but instead just contains cached object
  22. * and field data that may be used for initializing a managed object. Any operations during
  23. * this state will operate only on the cached internal data.
  24. * You can transfer between these states by calling serialize(linked->serialized) & deserialize (serialized->linked).
  25. *
  26. */
  27. class BS_SCR_BE_EXPORT ManagedSerializableObject : public IReflectable
  28. {
  29. private:
  30. struct ConstructPrivately {};
  31. /** Generates a hash value for field key data identifying a single field in the object. */
  32. struct BS_SCR_BE_EXPORT Hash
  33. {
  34. inline size_t operator()(const ManagedSerializableFieldKey& x) const;
  35. };
  36. /** Compares two field key objects. */
  37. struct BS_SCR_BE_EXPORT Equals
  38. {
  39. inline bool operator()(const ManagedSerializableFieldKey& a, const ManagedSerializableFieldKey& b) const;
  40. };
  41. public:
  42. ManagedSerializableObject(const ConstructPrivately& dummy, SPtr<ManagedSerializableObjectInfo> objInfo, MonoObject* managedInstance);
  43. ManagedSerializableObject(const ConstructPrivately& dummy);
  44. /**
  45. * Returns the internal managed instance of the object. This will return null if the object is in serialized mode.
  46. */
  47. MonoObject* getManagedInstance() const { return mManagedInstance; }
  48. /** Returns the type information for the internal object. */
  49. SPtr<ManagedSerializableObjectInfo> getObjectInfo() const { return mObjInfo; }
  50. /**
  51. * Sets a new value of the specified field. Operates on managed object if in linked state, or on cached data
  52. * otherwise.
  53. *
  54. * @param[in] fieldInfo Object describing the field to which to set the value. This field must belong to the
  55. * type this object is initialized with.
  56. * @param[in] val Wrapper around the value to store in the field.
  57. */
  58. void setFieldData(const SPtr<ManagedSerializableMemberInfo>& fieldInfo, const SPtr<ManagedSerializableFieldData>& val);
  59. /**
  60. * Returns the value of the specified field. Operates on managed object if in linked state, or on cached data
  61. * otherwise.
  62. *
  63. * @param[in] fieldInfo Object describing the field to which to set the value. This field must belong to the
  64. * type this object is initialized with.
  65. * @return A wrapper around the value of the field.
  66. */
  67. SPtr<ManagedSerializableFieldData> getFieldData(const SPtr<ManagedSerializableMemberInfo>& fieldInfo) const;
  68. /**
  69. * Serializes the internal managed object into a set of cached data that can be saved in memory/disk and can be
  70. * deserialized later. Does nothing if object is already is serialized mode. When in serialized mode the reference
  71. * to the managed instance will be lost.
  72. */
  73. void serialize();
  74. /**
  75. * Deserializes a set of cached data into a managed object. This action may fail in case the cached data contains a
  76. * type that no longer exists. You may check if it completely successfully if getManagedInstance() returns non-null
  77. * after.
  78. *
  79. * This action transfers the object into linked mode. All further operations will operate directly on the managed
  80. * instance and the cached data will be cleared. If you call this method on an already linked object the old object
  81. * will be replaced and initialized with empty data (since cached data does not exist).
  82. */
  83. void 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. * This action transfers the object into linked mode. All further operations will operate directly on the managed
  89. * instance and the cached data will be cleared. If you call this method on an already linked object the old object
  90. * will be replaced and initialized with empty data (since cached data does not exist).
  91. *
  92. * @param[in] instance Existing managed instance of the same type this serializable object represents.
  93. * @param[in] objInfo Serializable object info for the managed object type.
  94. */
  95. void deserialize(MonoObject* instance, const SPtr<ManagedSerializableObjectInfo>& objInfo);
  96. /**
  97. * Creates a managed serializable object that references an existing managed object. Created object will be in
  98. * linked mode.
  99. *
  100. * @param[in] managedInstance Constructed managed instance of the object to link with.
  101. */
  102. static SPtr<ManagedSerializableObject> createFromExisting(MonoObject* managedInstance);
  103. /**
  104. * Creates a managed serializable object that creates and references a brand new managed object instance.
  105. *
  106. * @param[in] type Type of the object to create.
  107. */
  108. static SPtr<ManagedSerializableObject> createNew(const SPtr<ManagedSerializableTypeInfoObject>& type);
  109. /**
  110. * Creates a managed object instance.
  111. *
  112. * @param[in] type Type of the object to create.
  113. */
  114. static MonoObject* createManagedInstance(const SPtr<ManagedSerializableTypeInfoObject>& type);
  115. protected:
  116. MonoObject* mManagedInstance;
  117. SPtr<ManagedSerializableObjectInfo> mObjInfo;
  118. UnorderedMap<ManagedSerializableFieldKey, SPtr<ManagedSerializableFieldData>, Hash, Equals> mCachedData;
  119. /************************************************************************/
  120. /* RTTI */
  121. /************************************************************************/
  122. /** Creates an empty and uninitialized object used for serialization purposes. */
  123. static SPtr<ManagedSerializableObject> createEmpty();
  124. public:
  125. friend class ManagedSerializableObjectRTTI;
  126. static RTTITypeBase* getRTTIStatic();
  127. virtual RTTITypeBase* getRTTI() const override;
  128. };
  129. /** @} */
  130. }