BsManagedSerializableList.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include <mono/jit/jit.h>
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Allows access to an underlying managed list, or a cached version of that list that
  9. * can be serialized/deserialized.
  10. *
  11. * @note This class can be in two states:
  12. * - Linked - When the object has a link to a managed object. This is the default
  13. * state when a new instance of ManagedSerializableObject is created.
  14. * Any operations during this state will operate directly on the linked
  15. * managed object.
  16. * - Serialized - When the object has no link to the managed object but instead just
  17. * contains cached object and field data that may be used for initializing
  18. * a managed object. Any operations during this state will operate
  19. * only on the cached internal data.
  20. * You can transfer between these states by calling serialize(linked->serialized) &
  21. * deserialize (serialized->linked).
  22. *
  23. */
  24. class BS_SCR_BE_EXPORT ManagedSerializableList : public IReflectable
  25. {
  26. private:
  27. struct ConstructPrivately {};
  28. public:
  29. ManagedSerializableList(const ConstructPrivately& dummy, const ManagedSerializableTypeInfoListPtr& typeInfo, MonoObject* managedInstance);
  30. ManagedSerializableList(const ConstructPrivately& dummy);
  31. /**
  32. * @brief Returns the internal managed instance of the list. This will return null if
  33. * the object is in serialized mode.
  34. */
  35. MonoObject* getManagedInstance() const { return mManagedInstance; }
  36. /**
  37. * @brief Returns the type information for the internal list.
  38. */
  39. ManagedSerializableTypeInfoListPtr getTypeInfo() const { return mListTypeInfo; }
  40. /**
  41. * @brief Changes the size of the list. Operates on managed object if in linked state,
  42. * or on cached data otherwise.
  43. */
  44. void resize(UINT32 newSize);
  45. /**
  46. * @brief Sets a new element value at the specified list index. Operates on
  47. * managed object if in linked state, or on cached data otherwise.
  48. *
  49. * @param arrayIdx Index at which to set the value.
  50. * @param val Wrapper around the value to store in the list. Must be of the
  51. * list element type.
  52. */
  53. void setFieldData(UINT32 arrayIdx, const ManagedSerializableFieldDataPtr& val);
  54. /**
  55. * @brief Returns the element value at the specified list index. Operates on
  56. * managed object if in linked state, or on cached data otherwise.
  57. *
  58. * @param arrayIdx Index at which to retrieve the value.
  59. *
  60. * @return A wrapper around the element value in the list.
  61. */
  62. ManagedSerializableFieldDataPtr getFieldData(UINT32 arrayIdx);
  63. /**
  64. * @brief Returns the size of the list. Operates on managed object
  65. * if in linked state, or on cached data otherwise.
  66. */
  67. UINT32 getLength() const { return mNumElements; }
  68. /**
  69. * @brief Serializes the internal managed object into a set of cached data that can be saved
  70. * in memory/disk and can be deserialized later. Does nothing if object is already is
  71. * serialized mode. When in serialized mode the reference to the managed instance will be lost.
  72. */
  73. void serialize();
  74. /**
  75. * @brief Deserializes a set of cached data into a managed object. This action may fail in case the cached
  76. * data contains a type that no longer exists. You may check if it completely successfully if ::getManagedInstance
  77. * returns non-null after.
  78. *
  79. * This action transfers the object into linked mode. All further operations will operate directly on the managed instance
  80. * and the cached data will be cleared. If you call this method on an already linked object the old object will be
  81. * replaced and initialized with empty data (since cached data does not exist).
  82. */
  83. void deserialize();
  84. /**
  85. * @brief Creates a managed serializable list that references an existing managed list. Created object will be in linked mode.
  86. *
  87. * @param managedInstance Constructed managed instance of the list to link with. Its type must correspond with the provided type info.
  88. * @param typeInfo Type information for the list and its elements.
  89. */
  90. static ManagedSerializableListPtr createFromExisting(MonoObject* managedInstance, const ManagedSerializableTypeInfoListPtr& typeInfo);
  91. /**
  92. * @brief Creates a managed serializable list that creates and references a brand new managed list instance.
  93. *
  94. * @param typeInfo Type of the list to create.
  95. * @param size Initial size of the list.
  96. */
  97. static ManagedSerializableListPtr createNew(const ManagedSerializableTypeInfoListPtr& typeInfo, UINT32 size);
  98. /**
  99. * @brief Creates a managed list instance.
  100. *
  101. * @param typeInfo Type of the list to create.
  102. * @param size Initial size of the list.
  103. */
  104. static MonoObject* createManagedInstance(const ManagedSerializableTypeInfoListPtr& typeInfo, UINT32 size);
  105. protected:
  106. /**
  107. * @brief Retrieves needed Mono types and methods. Should be called
  108. * before performing any operations with the managed object.
  109. */
  110. void initMonoObjects(MonoClass* listClass);
  111. /**
  112. * @brief Returns the size of the list. Operates on the
  113. * internal managed object.
  114. */
  115. UINT32 getLengthInternal() const;
  116. /**
  117. * @brief Appends data to the end of the list. Operates on
  118. * the internal managed object.
  119. */
  120. void addFieldDataInternal(const ManagedSerializableFieldDataPtr& val);
  121. MonoObject* mManagedInstance;
  122. MonoMethod* mAddMethod;
  123. MonoMethod* mAddRangeMethod;
  124. MonoMethod* mClearMethod;
  125. MonoMethod* mCopyToMethod;
  126. MonoProperty* mItemProp;
  127. MonoProperty* mCountProp;
  128. ManagedSerializableTypeInfoListPtr mListTypeInfo;
  129. Vector<ManagedSerializableFieldDataPtr> mCachedEntries;
  130. UINT32 mNumElements;
  131. /************************************************************************/
  132. /* RTTI */
  133. /************************************************************************/
  134. /**
  135. * @brief Creates an empty and uninitialized object used for serialization purposes.
  136. */
  137. static ManagedSerializableListPtr createEmpty();
  138. public:
  139. friend class ManagedSerializableListRTTI;
  140. static RTTITypeBase* getRTTIStatic();
  141. virtual RTTITypeBase* getRTTI() const override;
  142. };
  143. }