using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
#pragma warning disable 649
///
/// Allows you to access meta-data about a managed dictionary and its children. Similar to Reflection but simpler and
/// faster.
///
public sealed class SerializableDictionary : ScriptObject
{
private SerializableProperty.FieldType keyType;
private SerializableProperty.FieldType valueType;
private Type internalKeyType;
private Type internalValueType;
private SerializableProperty parentProperty;
///
/// Type of keys stored in the dictionary.
///
public SerializableProperty.FieldType KeyType
{
get { return keyType; }
}
///
/// Type of values stored in the dictionary.
///
public SerializableProperty.FieldType ValueType
{
get { return valueType; }
}
///
/// Constructor for use by the runtime only.
///
/// C# type of the keys in the dictionary.
/// C# type of the values in the dictionary.
/// Property used for retrieving this entry.
private SerializableDictionary(Type internalKeyType, Type internalValueType, SerializableProperty parentProperty)
{
this.parentProperty = parentProperty;
this.internalKeyType = internalKeyType;
this.internalValueType = internalValueType;
keyType = SerializableProperty.DetermineFieldType(internalKeyType);
valueType = SerializableProperty.DetermineFieldType(internalValueType);
}
///
/// Returns a serializable property for the specified entry.
///
/// Dictionary key for the value to retrieve.
/// Serializable property that allows you to manipulate contents of the dictionary entry.
public KeyValuePair GetProperty(object key)
{
IDictionary dictionary = parentProperty.GetValue();
if (dictionary == null || !dictionary.Contains(key))
return new KeyValuePair(null, null);
SerializableProperty keyProperty;
{
SerializableProperty.Getter getter = () => key;
SerializableProperty.Setter setter = (object value) => {};
keyProperty = Internal_CreateKeyProperty(mCachedPtr);
keyProperty.Construct(KeyType, internalKeyType, getter, setter);
}
SerializableProperty valueProperty;
{
SerializableProperty.Getter getter = () =>
{
IDictionary dict = parentProperty.GetValue();
if (dict != null)
return dict[key];
else
return null;
};
SerializableProperty.Setter setter = (object value) =>
{
IDictionary dict = parentProperty.GetValue();
if (dict != null)
dict[key] = value;
};
valueProperty = Internal_CreateValueProperty(mCachedPtr);
valueProperty.Construct(ValueType, internalValueType, getter, setter);
}
return new KeyValuePair(keyProperty, valueProperty);
}
///
/// Returns the total number of elements in the dictionary.
///
/// Total number of elements in the dictionary.
public int GetLength()
{
IDictionary dictionary = parentProperty.GetValue();
if (dictionary != null)
return dictionary.Count;
else
return 0;
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern SerializableProperty Internal_CreateKeyProperty(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern SerializableProperty Internal_CreateValueProperty(IntPtr nativeInstance);
}
}