BsManagedSerializableArray.h 7.4 KB

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