Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 65CF518C97 for ; Wed, 11 Nov 2015 09:15:35 +0000 (UTC) Received: (qmail 99243 invoked by uid 500); 11 Nov 2015 09:15:35 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 99101 invoked by uid 500); 11 Nov 2015 09:15:35 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 98478 invoked by uid 99); 11 Nov 2015 09:15:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Nov 2015 09:15:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B6FA0DFCF2; Wed, 11 Nov 2015 09:15:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: vozerov@apache.org To: commits@ignite.apache.org Date: Wed, 11 Nov 2015 09:15:49 -0000 Message-Id: <24f0cf3dee0949da91dc8b1f1a31f9a2@git.apache.org> In-Reply-To: <37765ef9ca8d4fd982e96cc2f5979295@git.apache.org> References: <37765ef9ca8d4fd982e96cc2f5979295@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [16/26] ignite git commit: IGNITE-1845: Adopted new binary API in .Net. http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs new file mode 100644 index 0000000..2c10d6a --- /dev/null +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs @@ -0,0 +1,832 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Core.Impl.Binary +{ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Diagnostics; + using System.Diagnostics.CodeAnalysis; + using Apache.Ignite.Core.Impl.Binary.IO; + using Apache.Ignite.Core.Impl.Common; + + /// + /// Write delegate. + /// + /// Write context. + /// Object to write. + internal delegate void BinarySystemWriteDelegate(BinaryWriter writer, object obj); + + /** + * Collection of predefined handlers for various system types. + */ + internal static class BinarySystemHandlers + { + /** Write handlers. */ + private static volatile Dictionary _writeHandlers = + new Dictionary(); + + /** Mutex for write handlers update. */ + private static readonly object WriteHandlersMux = new object(); + + /** Read handlers. */ + private static readonly IBinarySystemReader[] ReadHandlers = new IBinarySystemReader[255]; + + /** Type ids. */ + private static readonly Dictionary TypeIds = new Dictionary + { + {typeof (bool), BinaryUtils.TypeBool}, + {typeof (byte), BinaryUtils.TypeByte}, + {typeof (sbyte), BinaryUtils.TypeByte}, + {typeof (short), BinaryUtils.TypeShort}, + {typeof (ushort), BinaryUtils.TypeShort}, + {typeof (char), BinaryUtils.TypeChar}, + {typeof (int), BinaryUtils.TypeInt}, + {typeof (uint), BinaryUtils.TypeInt}, + {typeof (long), BinaryUtils.TypeLong}, + {typeof (ulong), BinaryUtils.TypeLong}, + {typeof (float), BinaryUtils.TypeFloat}, + {typeof (double), BinaryUtils.TypeDouble}, + {typeof (string), BinaryUtils.TypeString}, + {typeof (decimal), BinaryUtils.TypeDecimal}, + {typeof (Guid), BinaryUtils.TypeGuid}, + {typeof (Guid?), BinaryUtils.TypeGuid}, + {typeof (ArrayList), BinaryUtils.TypeCollection}, + {typeof (Hashtable), BinaryUtils.TypeDictionary}, + {typeof (DictionaryEntry), BinaryUtils.TypeMapEntry}, + {typeof (bool[]), BinaryUtils.TypeArrayBool}, + {typeof (byte[]), BinaryUtils.TypeArrayByte}, + {typeof (sbyte[]), BinaryUtils.TypeArrayByte}, + {typeof (short[]), BinaryUtils.TypeArrayShort}, + {typeof (ushort[]), BinaryUtils.TypeArrayShort}, + {typeof (char[]), BinaryUtils.TypeArrayChar}, + {typeof (int[]), BinaryUtils.TypeArrayInt}, + {typeof (uint[]), BinaryUtils.TypeArrayInt}, + {typeof (long[]), BinaryUtils.TypeArrayLong}, + {typeof (ulong[]), BinaryUtils.TypeArrayLong}, + {typeof (float[]), BinaryUtils.TypeArrayFloat}, + {typeof (double[]), BinaryUtils.TypeArrayDouble}, + {typeof (string[]), BinaryUtils.TypeArrayString}, + {typeof (decimal?[]), BinaryUtils.TypeArrayDecimal}, + {typeof (Guid?[]), BinaryUtils.TypeArrayGuid}, + {typeof (object[]), BinaryUtils.TypeArray} + }; + + /// + /// Initializes the class. + /// + [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", + Justification = "Readability.")] + static BinarySystemHandlers() + { + // 1. Primitives. + ReadHandlers[BinaryUtils.TypeBool] = new BinarySystemReader(s => s.ReadBool()); + ReadHandlers[BinaryUtils.TypeByte] = new BinarySystemReader(s => s.ReadByte()); + ReadHandlers[BinaryUtils.TypeShort] = new BinarySystemReader(s => s.ReadShort()); + ReadHandlers[BinaryUtils.TypeChar] = new BinarySystemReader(s => s.ReadChar()); + ReadHandlers[BinaryUtils.TypeInt] = new BinarySystemReader(s => s.ReadInt()); + ReadHandlers[BinaryUtils.TypeLong] = new BinarySystemReader(s => s.ReadLong()); + ReadHandlers[BinaryUtils.TypeFloat] = new BinarySystemReader(s => s.ReadFloat()); + ReadHandlers[BinaryUtils.TypeDouble] = new BinarySystemReader(s => s.ReadDouble()); + ReadHandlers[BinaryUtils.TypeDecimal] = new BinarySystemReader(BinaryUtils.ReadDecimal); + + // 2. Date. + ReadHandlers[BinaryUtils.TypeTimestamp] = new BinarySystemReader(BinaryUtils.ReadTimestamp); + + // 3. String. + ReadHandlers[BinaryUtils.TypeString] = new BinarySystemReader(BinaryUtils.ReadString); + + // 4. Guid. + ReadHandlers[BinaryUtils.TypeGuid] = new BinarySystemReader(BinaryUtils.ReadGuid); + + // 5. Primitive arrays. + ReadHandlers[BinaryUtils.TypeArrayBool] = new BinarySystemReader(BinaryUtils.ReadBooleanArray); + + ReadHandlers[BinaryUtils.TypeArrayByte] = + new BinarySystemDualReader(BinaryUtils.ReadByteArray, BinaryUtils.ReadSbyteArray); + + ReadHandlers[BinaryUtils.TypeArrayShort] = + new BinarySystemDualReader(BinaryUtils.ReadShortArray, + BinaryUtils.ReadUshortArray); + + ReadHandlers[BinaryUtils.TypeArrayChar] = + new BinarySystemReader(BinaryUtils.ReadCharArray); + + ReadHandlers[BinaryUtils.TypeArrayInt] = + new BinarySystemDualReader(BinaryUtils.ReadIntArray, BinaryUtils.ReadUintArray); + + ReadHandlers[BinaryUtils.TypeArrayLong] = + new BinarySystemDualReader(BinaryUtils.ReadLongArray, + BinaryUtils.ReadUlongArray); + + ReadHandlers[BinaryUtils.TypeArrayFloat] = + new BinarySystemReader(BinaryUtils.ReadFloatArray); + + ReadHandlers[BinaryUtils.TypeArrayDouble] = + new BinarySystemReader(BinaryUtils.ReadDoubleArray); + + ReadHandlers[BinaryUtils.TypeArrayDecimal] = + new BinarySystemReader(BinaryUtils.ReadDecimalArray); + + // 6. Date array. + ReadHandlers[BinaryUtils.TypeArrayTimestamp] = + new BinarySystemReader(BinaryUtils.ReadTimestampArray); + + // 7. String array. + ReadHandlers[BinaryUtils.TypeArrayString] = new BinarySystemTypedArrayReader(); + + // 8. Guid array. + ReadHandlers[BinaryUtils.TypeArrayGuid] = new BinarySystemTypedArrayReader(); + + // 9. Array. + ReadHandlers[BinaryUtils.TypeArray] = new BinarySystemReader(ReadArray); + + // 11. Arbitrary collection. + ReadHandlers[BinaryUtils.TypeCollection] = new BinarySystemReader(ReadCollection); + + // 13. Arbitrary dictionary. + ReadHandlers[BinaryUtils.TypeDictionary] = new BinarySystemReader(ReadDictionary); + + // 15. Map entry. + ReadHandlers[BinaryUtils.TypeMapEntry] = new BinarySystemReader(ReadMapEntry); + + // 16. Enum. + ReadHandlers[BinaryUtils.TypeEnum] = new BinarySystemReader(BinaryUtils.ReadEnum); + ReadHandlers[BinaryUtils.TypeArrayEnum] = new BinarySystemReader(ReadEnumArray); + } + + /// + /// Try getting write handler for type. + /// + /// + /// + public static BinarySystemWriteDelegate GetWriteHandler(Type type) + { + BinarySystemWriteDelegate res; + + var writeHandlers0 = _writeHandlers; + + // Have we ever met this type? + if (writeHandlers0 != null && writeHandlers0.TryGetValue(type, out res)) + return res; + + // Determine write handler for type and add it. + res = FindWriteHandler(type); + + if (res != null) + AddWriteHandler(type, res); + + return res; + } + + /// + /// Find write handler for type. + /// + /// Type. + /// Write handler or NULL. + private static BinarySystemWriteDelegate FindWriteHandler(Type type) + { + // 1. Well-known types. + if (type == typeof(string)) + return WriteString; + if (type == typeof(decimal)) + return WriteDecimal; + if (type == typeof(DateTime)) + return WriteDate; + if (type == typeof(Guid)) + return WriteGuid; + if (type == typeof (BinaryObject)) + return WriteBinary; + if (type == typeof (ArrayList)) + return WriteArrayList; + if (type == typeof(Hashtable)) + return WriteHashtable; + if (type == typeof(DictionaryEntry)) + return WriteMapEntry; + if (type.IsArray) + { + // We know how to write any array type. + Type elemType = type.GetElementType(); + + // Primitives. + if (elemType == typeof (bool)) + return WriteBoolArray; + if (elemType == typeof(byte)) + return WriteByteArray; + if (elemType == typeof(short)) + return WriteShortArray; + if (elemType == typeof(char)) + return WriteCharArray; + if (elemType == typeof(int)) + return WriteIntArray; + if (elemType == typeof(long)) + return WriteLongArray; + if (elemType == typeof(float)) + return WriteFloatArray; + if (elemType == typeof(double)) + return WriteDoubleArray; + // Non-CLS primitives. + if (elemType == typeof(sbyte)) + return WriteSbyteArray; + if (elemType == typeof(ushort)) + return WriteUshortArray; + if (elemType == typeof(uint)) + return WriteUintArray; + if (elemType == typeof(ulong)) + return WriteUlongArray; + // Special types. + if (elemType == typeof (decimal?)) + return WriteDecimalArray; + if (elemType == typeof(string)) + return WriteStringArray; + if (elemType == typeof(Guid?)) + return WriteGuidArray; + // Enums. + if (elemType.IsEnum) + return WriteEnumArray; + + // Object array. + if (elemType == typeof (object)) + return WriteArray; + } + + if (type.IsEnum) + // We know how to write enums. + return WriteEnum; + + if (type.IsSerializable) + return WriteSerializable; + + return null; + } + + /// + /// Find write handler for type. + /// + /// Type. + /// Write handler or NULL. + public static byte GetTypeId(Type type) + { + byte res; + + if (TypeIds.TryGetValue(type, out res)) + return res; + + if (type.IsEnum) + return BinaryUtils.TypeEnum; + + if (type.IsArray && type.GetElementType().IsEnum) + return BinaryUtils.TypeArrayEnum; + + return BinaryUtils.TypeObject; + } + + /// + /// Add write handler for type. + /// + /// + /// + private static void AddWriteHandler(Type type, BinarySystemWriteDelegate handler) + { + lock (WriteHandlersMux) + { + if (_writeHandlers == null) + { + Dictionary writeHandlers0 = + new Dictionary(); + + writeHandlers0[type] = handler; + + _writeHandlers = writeHandlers0; + } + else if (!_writeHandlers.ContainsKey(type)) + { + Dictionary writeHandlers0 = + new Dictionary(_writeHandlers); + + writeHandlers0[type] = handler; + + _writeHandlers = writeHandlers0; + } + } + } + + /// + /// Reads an object of predefined type. + /// + public static T ReadSystemType(byte typeId, BinaryReader ctx) + { + var handler = ReadHandlers[typeId]; + + Debug.Assert(handler != null, "Cannot find predefined read handler: " + typeId); + + return handler.Read(ctx); + } + + /// + /// Write decimal. + /// + /// Context. + /// Value. + private static void WriteDecimal(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeDecimal); + + BinaryUtils.WriteDecimal((decimal)obj, ctx.Stream); + } + + /// + /// Write date. + /// + /// Context. + /// Value. + private static void WriteDate(BinaryWriter ctx, object obj) + { + ctx.Write(new DateTimeHolder((DateTime) obj)); + } + + /// + /// Write string. + /// + /// Context. + /// Object. + private static void WriteString(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeString); + + BinaryUtils.WriteString((string)obj, ctx.Stream); + } + + /// + /// Write Guid. + /// + /// Context. + /// Value. + private static void WriteGuid(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeGuid); + + BinaryUtils.WriteGuid((Guid)obj, ctx.Stream); + } + + /// + /// Write boolaen array. + /// + /// Context. + /// Value. + private static void WriteBoolArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayBool); + + BinaryUtils.WriteBooleanArray((bool[])obj, ctx.Stream); + } + + /// + /// Write byte array. + /// + /// Context. + /// Value. + private static void WriteByteArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayByte); + + BinaryUtils.WriteByteArray((byte[])obj, ctx.Stream); + } + + /// + /// Write sbyte array. + /// + /// Context. + /// Value. + private static void WriteSbyteArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayByte); + + BinaryUtils.WriteByteArray((byte[])(Array)obj, ctx.Stream); + } + + /// + /// Write short array. + /// + /// Context. + /// Value. + private static void WriteShortArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayShort); + + BinaryUtils.WriteShortArray((short[])obj, ctx.Stream); + } + + /// + /// Write ushort array. + /// + /// Context. + /// Value. + private static void WriteUshortArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayShort); + + BinaryUtils.WriteShortArray((short[])(Array)obj, ctx.Stream); + } + + /// + /// Write char array. + /// + /// Context. + /// Value. + private static void WriteCharArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayChar); + + BinaryUtils.WriteCharArray((char[])obj, ctx.Stream); + } + + /// + /// Write int array. + /// + /// Context. + /// Value. + private static void WriteIntArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayInt); + + BinaryUtils.WriteIntArray((int[])obj, ctx.Stream); + } + + /// + /// Write uint array. + /// + /// Context. + /// Value. + private static void WriteUintArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayInt); + + BinaryUtils.WriteIntArray((int[])(Array)obj, ctx.Stream); + } + + /// + /// Write long array. + /// + /// Context. + /// Value. + private static void WriteLongArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayLong); + + BinaryUtils.WriteLongArray((long[])obj, ctx.Stream); + } + + /// + /// Write ulong array. + /// + /// Context. + /// Value. + private static void WriteUlongArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayLong); + + BinaryUtils.WriteLongArray((long[])(Array)obj, ctx.Stream); + } + + /// + /// Write float array. + /// + /// Context. + /// Value. + private static void WriteFloatArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayFloat); + + BinaryUtils.WriteFloatArray((float[])obj, ctx.Stream); + } + + /// + /// Write double array. + /// + /// Context. + /// Value. + private static void WriteDoubleArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayDouble); + + BinaryUtils.WriteDoubleArray((double[])obj, ctx.Stream); + } + + /// + /// Write decimal array. + /// + /// Context. + /// Value. + private static void WriteDecimalArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayDecimal); + + BinaryUtils.WriteDecimalArray((decimal?[])obj, ctx.Stream); + } + + /// + /// Write string array. + /// + /// Context. + /// Value. + private static void WriteStringArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayString); + + BinaryUtils.WriteStringArray((string[])obj, ctx.Stream); + } + + /// + /// Write nullable GUID array. + /// + /// Context. + /// Value. + private static void WriteGuidArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayGuid); + + BinaryUtils.WriteGuidArray((Guid?[])obj, ctx.Stream); + } + + /** + * Write enum array. + */ + private static void WriteEnumArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArrayEnum); + + BinaryUtils.WriteArray((Array)obj, ctx); + } + + /** + * Write array. + */ + private static void WriteArray(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeArray); + + BinaryUtils.WriteArray((Array)obj, ctx); + } + + /** + * Write ArrayList. + */ + private static void WriteArrayList(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeCollection); + + BinaryUtils.WriteCollection((ICollection)obj, ctx, BinaryUtils.CollectionArrayList); + } + + /** + * Write Hashtable. + */ + private static void WriteHashtable(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeDictionary); + + BinaryUtils.WriteDictionary((IDictionary)obj, ctx, BinaryUtils.MapHashMap); + } + + /** + * Write map entry. + */ + private static void WriteMapEntry(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeMapEntry); + + BinaryUtils.WriteMapEntry(ctx, (DictionaryEntry)obj); + } + + /** + * Write binary object. + */ + private static void WriteBinary(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeBinary); + + BinaryUtils.WriteBinary(ctx.Stream, (BinaryObject)obj); + } + + /// + /// Write enum. + /// + private static void WriteEnum(BinaryWriter ctx, object obj) + { + ctx.Stream.WriteByte(BinaryUtils.TypeEnum); + + BinaryUtils.WriteEnum(ctx.Stream, (Enum)obj); + } + + /// + /// Writes serializable. + /// + /// The writer. + /// The object. + private static void WriteSerializable(BinaryWriter writer, object o) + { + writer.Write(new SerializableObjectHolder(o)); + } + + /** + * Read enum array. + */ + private static object ReadEnumArray(BinaryReader ctx, Type type) + { + return BinaryUtils.ReadTypedArray(ctx, true, type.GetElementType()); + } + + /** + * Read array. + */ + private static object ReadArray(BinaryReader ctx, Type type) + { + var elemType = type.IsArray ? type.GetElementType() : typeof(object); + + return BinaryUtils.ReadTypedArray(ctx, true, elemType); + } + + /** + * Read collection. + */ + private static object ReadCollection(BinaryReader ctx, Type type) + { + return BinaryUtils.ReadCollection(ctx, null, null); + } + + /** + * Read dictionary. + */ + private static object ReadDictionary(BinaryReader ctx, Type type) + { + return BinaryUtils.ReadDictionary(ctx, null); + } + + /** + * Read map entry. + */ + private static object ReadMapEntry(BinaryReader ctx, Type type) + { + return BinaryUtils.ReadMapEntry(ctx); + } + + /** + * Add element to array list. + * Array list. + * Element. + */ + + + /** + * Read delegate. + * Read context. + * Type. + */ + private delegate object BinarySystemReadDelegate(BinaryReader ctx, Type type); + + /// + /// System type reader. + /// + private interface IBinarySystemReader + { + /// + /// Reads a value of specified type from reader. + /// + T Read(BinaryReader ctx); + } + + /// + /// System type generic reader. + /// + private interface IBinarySystemReader + { + /// + /// Reads a value of specified type from reader. + /// + T Read(BinaryReader ctx); + } + + /// + /// Default reader with boxing. + /// + private class BinarySystemReader : IBinarySystemReader + { + /** */ + private readonly BinarySystemReadDelegate _readDelegate; + + /// + /// Initializes a new instance of the class. + /// + /// The read delegate. + public BinarySystemReader(BinarySystemReadDelegate readDelegate) + { + Debug.Assert(readDelegate != null); + + _readDelegate = readDelegate; + } + + /** */ + public T Read(BinaryReader ctx) + { + return (T)_readDelegate(ctx, typeof(T)); + } + } + + /// + /// Reader without boxing. + /// + private class BinarySystemReader : IBinarySystemReader + { + /** */ + private readonly Func _readDelegate; + + /// + /// Initializes a new instance of the class. + /// + /// The read delegate. + public BinarySystemReader(Func readDelegate) + { + Debug.Assert(readDelegate != null); + + _readDelegate = readDelegate; + } + + /** */ + public TResult Read(BinaryReader ctx) + { + return TypeCaster.Cast(_readDelegate(ctx.Stream)); + } + } + + /// + /// Reader without boxing. + /// + private class BinarySystemTypedArrayReader : IBinarySystemReader + { + public TResult Read(BinaryReader ctx) + { + return TypeCaster.Cast(BinaryUtils.ReadArray(ctx, false)); + } + } + + /// + /// Reader with selection based on requested type. + /// + private class BinarySystemDualReader : IBinarySystemReader, IBinarySystemReader + { + /** */ + private readonly Func _readDelegate1; + + /** */ + private readonly Func _readDelegate2; + + /// + /// Initializes a new instance of the class. + /// + /// The read delegate1. + /// The read delegate2. + public BinarySystemDualReader(Func readDelegate1, Func readDelegate2) + { + Debug.Assert(readDelegate1 != null); + Debug.Assert(readDelegate2 != null); + + _readDelegate1 = readDelegate1; + _readDelegate2 = readDelegate2; + } + + /** */ + T2 IBinarySystemReader.Read(BinaryReader ctx) + { + return _readDelegate2(ctx.Stream); + } + + /** */ + public T Read(BinaryReader ctx) + { + // Can't use "as" because of variance. + // For example, IBinarySystemReader can be cast to IBinarySystemReader, which + // will cause incorrect behavior. + if (typeof (T) == typeof (T2)) + return ((IBinarySystemReader) this).Read(ctx); + + return TypeCaster.Cast(_readDelegate1(ctx.Stream)); + } + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/894057e5/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemTypeSerializer.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemTypeSerializer.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemTypeSerializer.cs new file mode 100644 index 0000000..cc145e9 --- /dev/null +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemTypeSerializer.cs @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Core.Impl.Binary +{ + using System; + using System.Diagnostics; + using Apache.Ignite.Core.Binary; + + /// + /// Binary serializer for system types. + /// + /// Object type. + internal class BinarySystemTypeSerializer : IBinarySystemTypeSerializer where T : IBinaryWriteAware + { + /** Ctor delegate. */ + private readonly Func _ctor; + + /// + /// Initializes a new instance of the class. + /// + /// Constructor delegate. + public BinarySystemTypeSerializer(Func ctor) + { + Debug.Assert(ctor != null); + + _ctor = ctor; + } + + /** */ + public void WriteBinary(object obj, IBinaryWriter writer) + { + ((T) obj).WriteBinary(writer); + } + + /** */ + public void ReadBinary(object obj, IBinaryReader reader) + { + throw new NotSupportedException("System serializer does not support ReadBinary."); + } + + /** */ + public object ReadInstance(BinaryReader reader) + { + return _ctor(reader); + } + } +} \ No newline at end of file