BsManagedSerializableList.h 6.6 KB

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