BsManagedSerializableArray.h 7.6 KB

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