BsManagedSerializableObject.h 6.6 KB

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