2
0

BsManagedSerializableArray.h 7.4 KB

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