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