BsManagedSerializableList.h 6.5 KB

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