SerializableList.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Runtime.CompilerServices;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Serialization
  9. * @{
  10. */
  11. #pragma warning disable 649
  12. /// <summary>
  13. /// Allows you to access meta-data about a managed list and its children. Similar to Reflection but simpler and faster.
  14. /// </summary>
  15. public sealed class SerializableList : ScriptObject
  16. {
  17. private SerializableProperty.FieldType elementPropertyType;
  18. private Type elementType;
  19. private SerializableProperty parentProperty;
  20. /// <summary>
  21. /// Type of serializable property used for the elements stored in the list.
  22. /// </summary>
  23. public SerializableProperty.FieldType ElementPropertyType
  24. {
  25. get { return elementPropertyType; }
  26. }
  27. /// <summary>
  28. /// Type of elements stored in the list.
  29. /// </summary>
  30. public Type ElementType
  31. {
  32. get { return elementType; }
  33. }
  34. /// <summary>
  35. /// Constructor for use by the runtime only.
  36. /// </summary>
  37. /// <param name="elementType">C# type of the elements in the list.</param>
  38. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  39. private SerializableList(Type elementType, SerializableProperty parentProperty)
  40. {
  41. this.parentProperty = parentProperty;
  42. this.elementType = elementType;
  43. elementPropertyType = SerializableProperty.DetermineFieldType(elementType);
  44. }
  45. /// <summary>
  46. /// Returns a serializable property for a specific list element.
  47. /// </summary>
  48. /// <param name="elementIdx">Index of the element in the list.</param>
  49. /// <returns>Serializable property that allows you to manipulate contents of the list entry.</returns>
  50. public SerializableProperty GetProperty(int elementIdx)
  51. {
  52. SerializableProperty.Getter getter = () =>
  53. {
  54. IList list = parentProperty.GetValue<IList>();
  55. if (list != null)
  56. return list[elementIdx];
  57. else
  58. return null;
  59. };
  60. SerializableProperty.Setter setter = (object value) =>
  61. {
  62. IList list = parentProperty.GetValue<IList>();
  63. if (list != null)
  64. list[elementIdx] = value;
  65. };
  66. SerializableProperty property = Internal_CreateProperty(mCachedPtr);
  67. property.Construct(ElementPropertyType, elementType, getter, setter);
  68. return property;
  69. }
  70. /// <summary>
  71. /// Returns number of elements in the list.
  72. /// </summary>
  73. /// <returns>Number of elements in the list.</returns>
  74. public int GetLength()
  75. {
  76. IList list = parentProperty.GetValue<IList>();
  77. if (list != null)
  78. return list.Count;
  79. else
  80. return 0;
  81. }
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  84. }
  85. /** @} */
  86. }