/// <summary> /// Reads a basic value from the stream. /// </summary> /// <param name="typeInfo">The type to be deserialized.</param> /// <param name="reader">The stream containing serialized data.</param> /// <param name="baseValue">The default value for this field.</param> /// <returns>The deserialized value.</returns> internal static object ReadValue(TypeInfo typeInfo, IReader reader, object baseValue) { object result = null; int n; var info = typeInfo.info & SerializationTypeInfo.VALUE_MASK; switch (info) { case SerializationTypeInfo.UserDefined: n = reader.ReadInt32(); if (n >= 0) { var itemType = typeInfo.type; if (baseValue == null) { result = ConstructorDelegator.CreateInstance(itemType); } else { result = baseValue; } FastSerializationManager.GetFastDeserializationMapping(itemType). Deserialize(result, reader); } break; case SerializationTypeInfo.SByte: result = reader.ReadSByte(); break; case SerializationTypeInfo.Byte: result = reader.ReadByte(); break; case SerializationTypeInfo.Boolean: result = reader.ReadByte() == 1; break; case SerializationTypeInfo.Int16: result = reader.ReadInt16(); break; case SerializationTypeInfo.UInt16: result = reader.ReadUInt16(); break; case SerializationTypeInfo.Int32: result = reader.ReadInt32(); break; case SerializationTypeInfo.UInt32: result = reader.ReadUInt32(); break; case SerializationTypeInfo.Int64: result = reader.ReadInt64(); break; case SerializationTypeInfo.UInt64: result = reader.ReadUInt64(); break; case SerializationTypeInfo.Single: result = reader.ReadSingle(); break; case SerializationTypeInfo.Double: result = reader.ReadDouble(); break; case SerializationTypeInfo.String: result = reader.ReadKleiString(); break; case SerializationTypeInfo.Enumeration: result = Enum.ToObject(typeInfo.type, reader.ReadInt32()); break; case SerializationTypeInfo.Vector2I: result = reader.ReadVector2I(); break; case SerializationTypeInfo.Vector2: result = reader.ReadVector2(); break; case SerializationTypeInfo.Vector3: result = reader.ReadVector3(); break; case SerializationTypeInfo.Array: reader.ReadInt32(); result = ReadArray(typeInfo, reader, true); break; case SerializationTypeInfo.Pair: n = reader.ReadInt32(); if (n >= 0) { TypeInfo keyType = typeInfo.subTypes[0], valueType = typeInfo.subTypes[1]; object key = ReadValue(keyType, reader, null); object value = ReadValue(valueType, reader, null); result = KeyValuePairDelegator.GetDelegates(keyType.type, valueType.type). CreateInstance(key, value); } break; case SerializationTypeInfo.Dictionary: reader.ReadInt32(); n = reader.ReadInt32(); if (n >= 0) { // Preallocate dictionary to correct size result = DictionaryDelegator.CreateInstance(typeInfo. genericInstantiationType, n); var dict = result as System.Collections.IDictionary; var keyType = typeInfo.subTypes[0]; var valueType = typeInfo.subTypes[1]; var values = ListPool <object, FastDeserializationMapping> .Allocate(); for (int i = 0; i < n; i++) { values.Add(ReadValue(valueType, reader, null)); } for (int i = 0; i < n; i++) { dict.Add(ReadValue(keyType, reader, null), values[i]); } values.Recycle(); } break; case SerializationTypeInfo.HashSet: reader.ReadInt32(); result = ReadArray(typeInfo, reader, false); if (result != null) { result = CollectionDelegator.CreateInstance(typeInfo. genericInstantiationType, result); } break; case SerializationTypeInfo.List: case SerializationTypeInfo.Queue: reader.ReadInt32(); result = ReadArray(typeInfo, reader, true); if (result != null) { // Due to how POD lists/queues are encoded, must go through a temporary // array to make best usage of ReadArrayFast, sorry memory usage result = CollectionDelegator.CreateInstance(typeInfo. genericInstantiationType, result); } break; case SerializationTypeInfo.Colour: result = reader.ReadColour(); break; default: throw new ArgumentException("Unknown type " + info); } return(result); }
/// <summary> /// Creates an instance of the mapped type. /// </summary> /// <returns>An instance of that type.</returns> public object CreateInstance() { return(ConstructorDelegator.CreateInstance(targetType)); }