BsManagedSerializableDictionary.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /** Contains key and value data used for serializing a managed dictionary. */
  13. struct BS_SCR_BE_EXPORT ManagedSerializableDictionaryKeyValue : public IReflectable
  14. {
  15. ManagedSerializableDictionaryKeyValue() {}
  16. ManagedSerializableDictionaryKeyValue(const ManagedSerializableFieldDataPtr& key,
  17. const ManagedSerializableFieldDataPtr& value);
  18. ManagedSerializableFieldDataPtr key;
  19. ManagedSerializableFieldDataPtr value;
  20. /************************************************************************/
  21. /* RTTI */
  22. /************************************************************************/
  23. public:
  24. friend class ManagedSerializableDictionaryKeyValueRTTI;
  25. static RTTITypeBase* getRTTIStatic();
  26. virtual RTTITypeBase* getRTTI() const override;
  27. };
  28. /**
  29. * Allows access to an underlying managed dictionary, or a cached version of that dictionary that can be
  30. * serialized/deserialized.
  31. *
  32. * @note
  33. * This class can be in two states:
  34. * - Linked - When the object has a link to a managed object. This is the default state when a new instance
  35. * of ManagedSerializableObject is created. Any operations during this state will operate directly
  36. * on the linked managed object.
  37. * - Serialized - When the object has no link to the managed object but instead just contains cached object
  38. * and field data that may be used for initializing a managed object. Any operations during
  39. * this state will operate only on the cached internal data.
  40. * You can transfer between these states by calling serialize(linked->serialized) & deserialize (serialized->linked).
  41. */
  42. class BS_SCR_BE_EXPORT ManagedSerializableDictionary : public IReflectable
  43. {
  44. private:
  45. struct ConstructPrivately {};
  46. /** Generates a hash value for field data. */
  47. struct BS_SCR_BE_EXPORT Hash
  48. {
  49. inline size_t operator()(const ManagedSerializableFieldDataPtr& x) const;
  50. };
  51. /** Compares two field data objects. */
  52. struct BS_SCR_BE_EXPORT Equals
  53. {
  54. inline bool operator()(const ManagedSerializableFieldDataPtr& a, const ManagedSerializableFieldDataPtr& b) const;
  55. };
  56. public:
  57. typedef UnorderedMap<ManagedSerializableFieldDataPtr, ManagedSerializableFieldDataPtr> CachedEntriesMap;
  58. /**
  59. * Helper class that enumerates over all entires in a managed dictionary. Operates on managed object if the parent
  60. * dictionary is in linked state, or on cached data otherwise.
  61. */
  62. class Enumerator
  63. {
  64. public:
  65. /**
  66. * Constructs a new enumerator for the provided managed object.
  67. *
  68. * @param[in] instance Managed instance of type System.Collections.Generic.Dictionary.
  69. * @param[in] parent Serializable parent of the managed instance.
  70. */
  71. Enumerator(MonoObject* instance, const ManagedSerializableDictionary* parent);
  72. /**
  73. * Returns the wrapped key data at the current enumerator position. Only valid to call this if enumerator is
  74. * valid (meaning last call to moveNext() returned true).
  75. */
  76. ManagedSerializableFieldDataPtr getKey() const;
  77. /**
  78. * Returns the wrapped value data at the current enumerator position. Only valid to call this if enumerator is
  79. * valid (meaning last call to moveNext() returned true).
  80. */
  81. ManagedSerializableFieldDataPtr getValue() const;
  82. /**
  83. * Moves the enumerator to the next position. Initially enumerator is at an invalid position and must be called
  84. * at least once before accesing its data.
  85. *
  86. * @return Returns if the enumerator is at valid position. When the enumerator returns false it means there are
  87. * no more elements to enumerate.
  88. */
  89. bool moveNext();
  90. private:
  91. MonoObject* mInstance;
  92. MonoObject* mCurrent;
  93. CachedEntriesMap::const_iterator mCachedIter;
  94. bool mIteratorInitialized;
  95. const ManagedSerializableDictionary* mParent;
  96. };
  97. public:
  98. ManagedSerializableDictionary(const ConstructPrivately& dummy, const ManagedSerializableTypeInfoDictionaryPtr& typeInfo, MonoObject* managedInstance);
  99. ManagedSerializableDictionary(const ConstructPrivately& dummy);
  100. /**
  101. * Returns the internal managed instance of the dictionary. This will return null if the object is in serialized
  102. * mode.
  103. */
  104. MonoObject* getManagedInstance() const { return mManagedInstance; }
  105. /** Returns the type information for the internal dictionary. */
  106. ManagedSerializableTypeInfoDictionaryPtr getTypeInfo() const { return mDictionaryTypeInfo; }
  107. /**
  108. * Returns the dictionary value at the specified key. If the key doesn't exist the default value for the type is
  109. * returned. Operates on managed object if in linked state, or on cached data otherwise.
  110. *
  111. * @param[in] key Wrapper around the key data at which to retrieve the value.
  112. * @return A wrapper around the value in the dictionary at the specified key.
  113. */
  114. ManagedSerializableFieldDataPtr getFieldData(const ManagedSerializableFieldDataPtr& key);
  115. /**
  116. * Sets the dictionary value at the specified key. Operates on managed object if in linked state, or on cached data
  117. * otherwise.
  118. *
  119. * @param[in] key Wrapper around the key data at which to set the value.
  120. * @param[in] val Wrapper around the value to set at the specified key.
  121. */
  122. void setFieldData(const ManagedSerializableFieldDataPtr& key, const ManagedSerializableFieldDataPtr& val);
  123. /**
  124. * Deletes the key/value pair at the specified key. If the key doesn't exist this operation does nothing. Operates
  125. * on managed object if in linked state, or on cached data otherwise.
  126. *
  127. * @param[in] key Wrapper around the key data at which to delete the value.
  128. */
  129. void removeFieldData(const ManagedSerializableFieldDataPtr& key);
  130. /**
  131. * Checks if the dictionary contains the specified key. Operates on managed object if in linked state, or on cached
  132. * data otherwise.
  133. *
  134. * @param[in] key Wrapper around the key data which to check.
  135. */
  136. bool contains(const ManagedSerializableFieldDataPtr& key) const;
  137. /** Returns an enumerator object that allows you to iterate over all key/value pairs in the dictionary. */
  138. Enumerator getEnumerator() const;
  139. /**
  140. * Serializes the internal managed object into a set of cached data that can be saved in memory/disk and can be
  141. * deserialized later. Does nothing if object is already is serialized mode. When in serialized mode the reference
  142. * to the managed instance will be lost.
  143. */
  144. void serialize();
  145. /**
  146. * Deserializes a set of cached data into a managed object. This action may fail in case the cached data contains a
  147. * type that no longer exists. You may check if it completely successfully if getManagedInstance() returns non-null
  148. * after.
  149. *
  150. * This action transfers the object into linked mode. All further operations will operate directly on the managed
  151. * instance and the cached data will be cleared. If you call this method on an already linked object the old object
  152. * will be replaced and initialized with empty data (since cached data does not exist).
  153. */
  154. void deserialize();
  155. /**
  156. * Creates a managed serializable dictionary that references an existing managed dictionary. Created object will be
  157. * in linked mode.
  158. *
  159. * @param[in] managedInstance Constructed managed instance of the dictionary to link with. Its type must
  160. * correspond with the provided type info.
  161. * @param[in] typeInfo Type information for the dictionary and its key/value pair.
  162. */
  163. static ManagedSerializableDictionaryPtr createFromExisting(MonoObject* managedInstance, const ManagedSerializableTypeInfoDictionaryPtr& typeInfo);
  164. /**
  165. * Creates a managed serializable dictionary that creates and references a brand new managed dictionary instance.
  166. *
  167. * @param[in] typeInfo Type of the dictionary to create.
  168. */
  169. static ManagedSerializableDictionaryPtr createNew(const ManagedSerializableTypeInfoDictionaryPtr& typeInfo);
  170. /**
  171. * Creates a managed dictionary instance.
  172. *
  173. * @param[in] typeInfo Type of the dictionary to create.
  174. */
  175. static MonoObject* createManagedInstance(const ManagedSerializableTypeInfoDictionaryPtr& typeInfo);
  176. protected:
  177. /**
  178. * Retrieves needed Mono types and methods. Should be called before performing any operations with the managed
  179. * object.
  180. */
  181. void initMonoObjects(MonoClass* dictionaryClass);
  182. MonoObject* mManagedInstance;
  183. MonoMethod* mAddMethod;
  184. MonoMethod* mRemoveMethod;
  185. MonoMethod* mTryGetValueMethod;
  186. MonoMethod* mContainsKeyMethod;
  187. MonoMethod* mGetEnumerator;
  188. MonoMethod* mEnumMoveNext;
  189. MonoProperty* mEnumCurrentProp;
  190. MonoProperty* mKeyProp;
  191. MonoProperty* mValueProp;
  192. ManagedSerializableTypeInfoDictionaryPtr mDictionaryTypeInfo;
  193. CachedEntriesMap mCachedEntries;
  194. /************************************************************************/
  195. /* RTTI */
  196. /************************************************************************/
  197. /** Creates an empty and uninitialized object used for serialization purposes. */
  198. static ManagedSerializableDictionaryPtr createEmpty();
  199. public:
  200. friend class ManagedSerializableDictionaryRTTI;
  201. static RTTITypeBase* getRTTIStatic();
  202. virtual RTTITypeBase* getRTTI() const override;
  203. };
  204. /** @} */
  205. }