using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
#pragma warning disable 649
///
/// Allows you to access meta-data about a managed list and its children. Similar to Reflection but simpler and faster.
///
public sealed class SerializableList : ScriptObject
{
private SerializableProperty.FieldType elementPropertyType;
private Type elementType;
private SerializableProperty parentProperty;
///
/// Type of serializable property used for the elements stored in the list.
///
public SerializableProperty.FieldType ElementPropertyType
{
get { return elementPropertyType; }
}
///
/// Type of elements stored in the list.
///
public Type ElementType
{
get { return elementType; }
}
///
/// Constructor for use by the runtime only.
///
/// C# type of the elements in the list.
/// Property used for retrieving this entry.
private SerializableList(Type elementType, SerializableProperty parentProperty)
{
this.parentProperty = parentProperty;
this.elementType = elementType;
elementPropertyType = SerializableProperty.DetermineFieldType(elementType);
}
///
/// Returns a serializable property for a specific list element.
///
/// Index of the element in the list.
/// Serializable property that allows you to manipulate contents of the list entry.
public SerializableProperty GetProperty(int elementIdx)
{
SerializableProperty.Getter getter = () =>
{
IList list = parentProperty.GetValue();
if (list != null)
return list[elementIdx];
else
return null;
};
SerializableProperty.Setter setter = (object value) =>
{
IList list = parentProperty.GetValue();
if (list != null)
list[elementIdx] = value;
};
SerializableProperty property = Internal_CreateProperty(mCachedPtr);
property.Construct(ElementPropertyType, elementType, getter, setter);
return property;
}
///
/// Returns number of elements in the list.
///
/// Number of elements in the list.
public int GetLength()
{
IList list = parentProperty.GetValue();
if (list != null)
return list.Count;
else
return 0;
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
}
}