80 lines
4.4 KiB
C#
80 lines
4.4 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace BCG
|
|
{
|
|
/// <summary>
|
|
/// Deserializes primitive types. Contructs a hash based scheme based on the index, to make everythign O(1)
|
|
/// rather than searching it up and making it O(n)
|
|
/// </summary>
|
|
public class PrimitiveDeserializationHelper
|
|
{
|
|
private delegate object DeserializeDelegate (int colNo, BinaryReader br);
|
|
private DeserializeDelegate[] myDispatchTable;
|
|
private ColumnInfo[] myCis;
|
|
|
|
public PrimitiveDeserializationHelper (IBlueTableSchema schema)
|
|
: this (schema.columnInfo)
|
|
{}
|
|
|
|
public PrimitiveDeserializationHelper (ColumnInfo[] cis)
|
|
{
|
|
myDispatchTable = new DeserializeDelegate [(myCis = cis).Length];
|
|
foreach (ColumnInfo ci in cis)
|
|
myDispatchTable[ci.colNo] = myGetDispatchMethod (ci.colType);
|
|
}
|
|
|
|
public object deserializeColumn (int colNo, BinaryReader br)
|
|
{
|
|
return myDispatchTable[colNo] (colNo, br);
|
|
}
|
|
|
|
private DeserializeDelegate myGetDispatchMethod (Type t)
|
|
{
|
|
if (t.Equals (BlueDataValidTypes.INT)) return new DeserializeDelegate (myDeserializeInt32);
|
|
else if (t.Equals (BlueDataValidTypes.DOUBLE)) return new DeserializeDelegate (myDeserializeDouble);
|
|
else if (t.Equals (BlueDataValidTypes.STRING)) return new DeserializeDelegate (myDeserializeString);
|
|
else if (t.Equals (BlueDataValidTypes.BYTEARRAY)) return new DeserializeDelegate (myDeserializeByteArray);
|
|
else if (t.Equals (BlueDataValidTypes.CHAR)) return new DeserializeDelegate (myDeserializeChar);
|
|
else if (t.Equals (BlueDataValidTypes.LONG)) return new DeserializeDelegate (myDeserializeLong);
|
|
else if (t.Equals (BlueDataValidTypes.SBYTE)) return new DeserializeDelegate (myDeserializeSByte);
|
|
else if (t.Equals (BlueDataValidTypes.BOOL)) return new DeserializeDelegate (myDeserializeBool);
|
|
else if (t.Equals (BlueDataValidTypes.BYTE)) return new DeserializeDelegate (myDeserializeByte);
|
|
else if (t.Equals (BlueDataValidTypes.DECIMAL)) return new DeserializeDelegate (myDeserializeDecimal);
|
|
else if (t.Equals (BlueDataValidTypes.UINT)) return new DeserializeDelegate (myDeserializeUInt);
|
|
else if (t.Equals (BlueDataValidTypes.ULONG)) return new DeserializeDelegate (myDeserializeULong);
|
|
else if (t.Equals (BlueDataValidTypes.DATETIME)) return new DeserializeDelegate (myDeserializeDateTime);
|
|
else if (t.Equals (BlueDataValidTypes.SINGLE)) return new DeserializeDelegate (myDeserializeSingle);
|
|
else if (SerializationUtilityMethods.isSerializedType (t)) return new DeserializeDelegate (myDeserializeISerializedType);
|
|
else throw new ApplicationException (t.FullName + " is not a supported type");
|
|
}
|
|
|
|
private object myDeserializeInt32 (int colNo, BinaryReader br) {return br.ReadInt32 ();}
|
|
private object myDeserializeISerializedType (int colNo, BinaryReader br)
|
|
{
|
|
ISerializedType o = Activator.CreateInstance (myCis[colNo].colType)
|
|
as ISerializedType;
|
|
o.deserialze (br);
|
|
return o;
|
|
}
|
|
private object myDeserializeDouble (int colNo, BinaryReader br) {return br.ReadDouble ();}
|
|
private object myDeserializeString (int colNo, BinaryReader br) {return br.ReadString ();}
|
|
private object myDeserializeByteArray (int colNo, BinaryReader br) {return br.ReadBytes (br.ReadInt32 ());}
|
|
private object myDeserializeChar (int colNo, BinaryReader br) {return br.ReadChar ();}
|
|
private object myDeserializeLong (int colNo, BinaryReader br) {return br.ReadInt64 ();}
|
|
private object myDeserializeSByte (int colNo, BinaryReader br) {return br.ReadSByte ();}
|
|
private object myDeserializeBool (int colNo, BinaryReader br) {return br.ReadBoolean ();}
|
|
private object myDeserializeByte (int colNo, BinaryReader br) {return br.ReadByte ();}
|
|
private object myDeserializeDecimal (int colNo, BinaryReader br) {return br.ReadDecimal ();}
|
|
private object myDeserializeUInt (int colNo, BinaryReader br) {return br.ReadUInt32 ();}
|
|
private object myDeserializeDateTime (int colNo, BinaryReader br)
|
|
{
|
|
return new DateTime(br.ReadInt64());
|
|
//return DateTime.FromOADate (br.ReadDouble ());
|
|
//return new DateTime(br.ReadInt64());
|
|
}
|
|
private object myDeserializeULong (int colNo, BinaryReader br) {return br.ReadUInt64 ();}
|
|
private object myDeserializeSingle (int colNo, BinaryReader br) {return br.ReadSingle ();}
|
|
}
|
|
}
|