2
0

SerializableDictionary.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Runtime.CompilerServices;
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Serialization
  10. * @{
  11. */
  12. #pragma warning disable 649
  13. /// <summary>
  14. /// Allows you to access meta-data about a managed dictionary and its children. Similar to Reflection but simpler and
  15. /// faster.
  16. /// </summary>
  17. public sealed class SerializableDictionary : ScriptObject
  18. {
  19. private SerializableProperty.FieldType keyPropertyType;
  20. private SerializableProperty.FieldType valuePropertyType;
  21. private Type keyType;
  22. private Type valueType;
  23. private SerializableProperty parentProperty;
  24. /// <summary>
  25. /// Type of serializable property used for the keys stored in the dictionary.
  26. /// </summary>
  27. public SerializableProperty.FieldType KeyPropertyType
  28. {
  29. get { return keyPropertyType; }
  30. }
  31. /// <summary>
  32. /// Type of serializable property used for the values stored in the dictionary.
  33. /// </summary>
  34. public SerializableProperty.FieldType ValuePropertyType
  35. {
  36. get { return valuePropertyType; }
  37. }
  38. /// <summary>
  39. /// Type used for of keys stored in the dictionary.
  40. /// </summary>
  41. public Type KeyType
  42. {
  43. get { return keyType; }
  44. }
  45. /// <summary>
  46. /// Type used for values stored in the dictionary.
  47. /// </summary>
  48. public Type ValueType
  49. {
  50. get { return valueType; }
  51. }
  52. /// <summary>
  53. /// Constructor for use by the runtime only.
  54. /// </summary>
  55. /// <param name="keyType">C# type of the keys in the dictionary.</param>
  56. /// <param name="valueType">C# type of the values in the dictionary.</param>
  57. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  58. private SerializableDictionary(Type keyType, Type valueType, SerializableProperty parentProperty)
  59. {
  60. this.parentProperty = parentProperty;
  61. this.keyType = keyType;
  62. this.valueType = valueType;
  63. keyPropertyType = SerializableProperty.DetermineFieldType(keyType);
  64. valuePropertyType = SerializableProperty.DetermineFieldType(valueType);
  65. }
  66. /// <summary>
  67. /// Returns a serializable property for the specified entry.
  68. /// </summary>
  69. /// <param name="key">Dictionary key for the value to retrieve.</param>
  70. /// <returns>Serializable property that allows you to manipulate contents of the dictionary entry.</returns>
  71. public KeyValuePair<SerializableProperty, SerializableProperty> GetProperty(object key)
  72. {
  73. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  74. if (dictionary == null || !dictionary.Contains(key))
  75. return new KeyValuePair<SerializableProperty, SerializableProperty>(null, null);
  76. SerializableProperty keyProperty;
  77. {
  78. SerializableProperty.Getter getter = () => key;
  79. SerializableProperty.Setter setter = (object value) => {};
  80. keyProperty = Internal_CreateKeyProperty(mCachedPtr);
  81. keyProperty.Construct(KeyPropertyType, keyType, getter, setter);
  82. }
  83. SerializableProperty valueProperty;
  84. {
  85. SerializableProperty.Getter getter = () =>
  86. {
  87. IDictionary dict = parentProperty.GetValue<IDictionary>();
  88. if (dict != null)
  89. return dict[key];
  90. else
  91. return null;
  92. };
  93. SerializableProperty.Setter setter = (object value) =>
  94. {
  95. IDictionary dict = parentProperty.GetValue<IDictionary>();
  96. if (dict != null)
  97. dict[key] = value;
  98. };
  99. valueProperty = Internal_CreateValueProperty(mCachedPtr);
  100. valueProperty.Construct(ValuePropertyType, valueType, getter, setter);
  101. }
  102. return new KeyValuePair<SerializableProperty, SerializableProperty>(keyProperty, valueProperty);
  103. }
  104. /// <summary>
  105. /// Returns the total number of elements in the dictionary.
  106. /// </summary>
  107. /// <returns>Total number of elements in the dictionary.</returns>
  108. public int GetLength()
  109. {
  110. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  111. if (dictionary != null)
  112. return dictionary.Count;
  113. else
  114. return 0;
  115. }
  116. [MethodImpl(MethodImplOptions.InternalCall)]
  117. private static extern SerializableProperty Internal_CreateKeyProperty(IntPtr nativeInstance);
  118. [MethodImpl(MethodImplOptions.InternalCall)]
  119. private static extern SerializableProperty Internal_CreateValueProperty(IntPtr nativeInstance);
  120. }
  121. /** @} */
  122. }