BsManagedSerializableList.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 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. class BS_SCR_BE_EXPORT ManagedSerializableList : public IReflectable
  25. {
  26. private:
  27. struct ConstructPrivately {};
  28. public:
  29. ManagedSerializableList(const ConstructPrivately& dummy, const SPtr<ManagedSerializableTypeInfoList>& typeInfo, MonoObject* managedInstance);
  30. ManagedSerializableList(const ConstructPrivately& dummy);
  31. /** Returns the internal managed instance of the list. This will return null if the object is in serialized mode. */
  32. MonoObject* getManagedInstance() const { return mManagedInstance; }
  33. /** Returns the type information for the internal list. */
  34. SPtr<ManagedSerializableTypeInfoList> getTypeInfo() const { return mListTypeInfo; }
  35. /** Changes the size of the list. Operates on managed object if in linked state, or on cached data otherwise. */
  36. void resize(UINT32 newSize);
  37. /**
  38. * Sets a new element value at the specified list index. Operates on managed object if in linked state, or on cached
  39. * data otherwise.
  40. *
  41. * @param[in] arrayIdx Index at which to set the value.
  42. * @param[in] val Wrapper around the value to store in the list. Must be of the list element type.
  43. */
  44. void setFieldData(UINT32 arrayIdx, const SPtr<ManagedSerializableFieldData>& val);
  45. /**
  46. * Returns the element value at the specified list index. Operates on managed object if in linked state, or on
  47. * cached data otherwise.
  48. *
  49. * @param[in] arrayIdx Index at which to retrieve the value.
  50. * @return A wrapper around the element value in the list.
  51. */
  52. SPtr<ManagedSerializableFieldData> getFieldData(UINT32 arrayIdx);
  53. /** Returns the size of the list. Operates on managed object if in linked state, or on cached data otherwise. */
  54. UINT32 getLength() const { return mNumElements; }
  55. /**
  56. * Serializes the internal managed object into a set of cached data that can be saved in memory/disk and can be
  57. * deserialized later. Does nothing if object is already is serialized mode. When in serialized mode the reference
  58. * to the managed instance will be lost.
  59. */
  60. void serialize();
  61. /**
  62. * Deserializes a set of cached data into a managed object. This action may fail in case the cached data contains a
  63. * type that no longer exists. You may check if it completely successfully if getManagedInstance() returns non-null
  64. * after.
  65. *
  66. * This action transfers the object into linked mode. All further operations will operate directly on the managed
  67. * instance and the cached data will be cleared. If you call this method on an already linked object the old object
  68. * will be replaced and initialized with empty data (since cached data does not exist).
  69. */
  70. void deserialize();
  71. /**
  72. * Creates a managed serializable list that references an existing managed list. Created object will be in linked
  73. * mode.
  74. *
  75. * @param[in] managedInstance Constructed managed instance of the list to link with. Its type must correspond with
  76. * the provided type info.
  77. * @param[in] typeInfo Type information for the list and its elements.
  78. */
  79. static SPtr<ManagedSerializableList> createFromExisting(MonoObject* managedInstance,
  80. const SPtr<ManagedSerializableTypeInfoList>& typeInfo);
  81. /**
  82. * Creates a managed serializable list that creates and references a brand new managed list instance.
  83. *
  84. * @param[in] typeInfo Type of the list to create.
  85. * @param[in] size Initial size of the list.
  86. */
  87. static SPtr<ManagedSerializableList> createNew(const SPtr<ManagedSerializableTypeInfoList>& typeInfo, UINT32 size);
  88. /**
  89. * Creates a managed list instance.
  90. *
  91. * @param[in] typeInfo Type of the list to create.
  92. * @param[in] size Initial size of the list.
  93. */
  94. static MonoObject* createManagedInstance(const SPtr<ManagedSerializableTypeInfoList>& typeInfo, UINT32 size);
  95. protected:
  96. /**
  97. * Retrieves needed Mono types and methods. Should be called before performing any operations with the managed
  98. * object.
  99. */
  100. void initMonoObjects(MonoClass* listClass);
  101. /** Returns the size of the list. Operates on the internal managed object. */
  102. UINT32 getLengthInternal() const;
  103. /** Appends data to the end of the list. Operates on the internal managed object. */
  104. void addFieldDataInternal(const SPtr<ManagedSerializableFieldData>& val);
  105. MonoObject* mManagedInstance;
  106. MonoMethod* mAddMethod;
  107. MonoMethod* mAddRangeMethod;
  108. MonoMethod* mClearMethod;
  109. MonoMethod* mCopyToMethod;
  110. MonoProperty* mItemProp;
  111. MonoProperty* mCountProp;
  112. SPtr<ManagedSerializableTypeInfoList> mListTypeInfo;
  113. Vector<SPtr<ManagedSerializableFieldData>> mCachedEntries;
  114. UINT32 mNumElements;
  115. /************************************************************************/
  116. /* RTTI */
  117. /************************************************************************/
  118. /** Creates an empty and uninitialized object used for serialization purposes. */
  119. static SPtr<ManagedSerializableList> createEmpty();
  120. public:
  121. friend class ManagedSerializableListRTTI;
  122. static RTTITypeBase* getRTTIStatic();
  123. virtual RTTITypeBase* getRTTI() const override;
  124. };
  125. /** @} */
  126. }