BsManagedSerializableArray.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 array, or a cached version of that array 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 ManagedSerializableArray 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. */
  28. class BS_SCR_BE_EXPORT ManagedSerializableArray : public IReflectable
  29. {
  30. private:
  31. struct ConstructPrivately {};
  32. public:
  33. ManagedSerializableArray(const ConstructPrivately& dummy, const SPtr<ManagedSerializableTypeInfoArray>& typeInfo, MonoObject* managedInstance);
  34. ManagedSerializableArray(const ConstructPrivately& dummy);
  35. ~ManagedSerializableArray();
  36. /**
  37. * Returns the internal managed instance of the array. This will return null if the object is in serialized mode.
  38. */
  39. MonoObject* getManagedInstance() const;
  40. /** Returns the type information for the internal array. */
  41. SPtr<ManagedSerializableTypeInfoArray> getTypeInfo() const { return mArrayTypeInfo; }
  42. /**
  43. * Changes the size of the array. Operates on managed object if in linked state, or on cached data otherwise. If
  44. * in linked state the new instance of the managed object will be created and must be retrieved by
  45. * getManagedInstance().
  46. *
  47. * @param[in] newSizes Array of sizes, one per array dimension. Number of sizes must match number of array
  48. * dimensions as specified by its type.
  49. */
  50. void resize(const Vector<UINT32>& newSizes);
  51. /**
  52. * Returns the size of a specific dimension of the array. Operates on managed object if in linked state, or on
  53. * cached data otherwise.
  54. */
  55. UINT32 getLength(UINT32 dimension) const { return mNumElements[dimension]; }
  56. /**
  57. * Returns the sizes of a all dimensions of the array. Operates on managed object if in linked state, or on cached
  58. * data otherwise.
  59. */
  60. Vector<UINT32> getLengths() const { return mNumElements; }
  61. /**
  62. * Returns the total of all sizes of all dimensions of the array. Operates on managed object if in linked state, or
  63. * on cached data otherwise.
  64. */
  65. UINT32 getTotalLength() const;
  66. /**
  67. * Sets a new element value at the specified array index. Operates on managed object if in linked state, or on
  68. * cached data otherwise.
  69. *
  70. * @param[in] arrayIdx Index at which to set the value.
  71. * @param[in] val Wrapper around the value to store in the array. Must be of the array element type.
  72. */
  73. void setFieldData(UINT32 arrayIdx, const SPtr<ManagedSerializableFieldData>& val);
  74. /**
  75. * Returns the element value at the specified array index. Operates on managed object if in linked state, or on
  76. * cached data otherwise.
  77. *
  78. * @param[in] arrayIdx Index at which to retrieve the value.
  79. * @return A wrapper around the element value in the array.
  80. */
  81. SPtr<ManagedSerializableFieldData> getFieldData(UINT32 arrayIdx);
  82. /**
  83. * Serializes the internal managed object into a set of cached data that can be saved in memory/disk and can be
  84. * deserialized later. The internal managed object will be freed (if no other references to it). Calling serialize()
  85. * again will have no result.
  86. */
  87. void serialize();
  88. /**
  89. * Deserializes a set of cached data into a managed object. This action may fail in case the cached data contains a
  90. * type that no longer exists in which case null is returned.
  91. *
  92. * @return Newly created object initialized with the cached data.
  93. */
  94. MonoObject* deserialize();
  95. /**
  96. * Creates a managed serializable array that references an existing managed array. Created object will be in linked
  97. * mode.
  98. *
  99. * @param[in] managedInstance Constructed managed instance of the array to link with. Its type must correspond
  100. * with the provided type info.
  101. * @param[in] typeInfo Type information for the array and its elements.
  102. */
  103. static SPtr<ManagedSerializableArray> createFromExisting(MonoObject* managedInstance, const SPtr<ManagedSerializableTypeInfoArray>& typeInfo);
  104. /**
  105. * Creates a managed serializable array that creates and references a brand new managed array instance.
  106. *
  107. * @param[in] typeInfo Type of the array to create.
  108. * @param[in] sizes Array of sizes, one per array dimension. Number of sizes must match number of array
  109. * dimensions as specified by its type.
  110. */
  111. static SPtr<ManagedSerializableArray> createNew(const SPtr<ManagedSerializableTypeInfoArray>& typeInfo, const Vector<UINT32>& sizes);
  112. /**
  113. * Creates a managed array instance.
  114. *
  115. * @param[in] typeInfo Type of the array to create.
  116. * @param[in] sizes Array of sizes, one per array dimension. Number of sizes must match number of array
  117. * dimensions as specified by its type.
  118. */
  119. static MonoObject* createManagedInstance(const SPtr<ManagedSerializableTypeInfoArray>& typeInfo, const Vector<UINT32>& sizes);
  120. protected:
  121. /**
  122. * Retrieves needed Mono types and methods. Should be called before performing any operations with the managed
  123. * object.
  124. */
  125. void initMonoObjects();
  126. /** Returns the size of the specified dimension of the array. Operates on the internal managed object. */
  127. UINT32 getLengthInternal(UINT32 dimension) const;
  128. /**
  129. * Sets a new element value at the specified array index. Operates on the provided managed instance.
  130. *
  131. * @param[in] obj Managed instance in which to set the data in.
  132. * @param[in] arrayIdx Index at which to set the value.
  133. * @param[in] val Wrapper around the value to store in the array. Must be of the array element type.
  134. */
  135. void setFieldData(MonoArray* obj, UINT32 arrayIdx, const SPtr<ManagedSerializableFieldData>& val);
  136. /** Sets a value at the specified index in the array. Operates on the provided managed object. */
  137. void setValueInternal(MonoArray* obj, UINT32 arrayIdx, void* val);
  138. /** Converts a multi-dimensional array index into a sequential one-dimensional index. */
  139. UINT32 toSequentialIdx(const Vector<UINT32>& idx) const;
  140. uint32_t mGCHandle = 0;
  141. ::MonoClass* mElementMonoClass = nullptr;
  142. MonoMethod* mCopyMethod = nullptr;
  143. SPtr<ManagedSerializableTypeInfoArray> mArrayTypeInfo;
  144. Vector<SPtr<ManagedSerializableFieldData>> mCachedEntries;
  145. Vector<UINT32> mNumElements;
  146. UINT32 mElemSize = 0;
  147. /************************************************************************/
  148. /* RTTI */
  149. /************************************************************************/
  150. /** Creates an empty and uninitialized object used for serialization purposes. */
  151. static SPtr<ManagedSerializableArray> createNew();
  152. public:
  153. friend class ManagedSerializableArrayRTTI;
  154. static RTTITypeBase* getRTTIStatic();
  155. RTTITypeBase* getRTTI() const override;
  156. };
  157. /** @} */
  158. }