BsManagedSerializableList.h 6.3 KB

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