151 lines
4.3 KiB
C#
151 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
/**
|
|
* typeCode list
|
|
*
|
|
* 0x0000 ODBNull
|
|
* 0x0001 ODBStringValue
|
|
* 0x0002 ODBList
|
|
* 0x0003 ODBGuid
|
|
*
|
|
* 0x0010 ODBInteger
|
|
* 0x0011 ODBUInteger
|
|
* 0x0012 ODBLong
|
|
* 0x0013 ODBULong
|
|
*
|
|
* 0x0018 ODBDouble
|
|
*
|
|
* 0x1000 ODBDocument
|
|
*
|
|
*
|
|
*
|
|
**/
|
|
|
|
|
|
|
|
namespace ln.types.odb.values
|
|
{
|
|
public delegate ODBValue ODBValueFactory(object value);
|
|
public delegate ODBValue ODBDeserialize(byte[] storageBytes, int offset, int length);
|
|
|
|
public abstract class ODBValue
|
|
{
|
|
int storageTypeCode;
|
|
|
|
public virtual object Value { get; protected set; }
|
|
|
|
protected ODBValue(int storageTypeCode)
|
|
{
|
|
this.storageTypeCode = storageTypeCode;
|
|
}
|
|
|
|
protected ODBValue(int storageTypeCode, object value)
|
|
: this(storageTypeCode)
|
|
{
|
|
Value = value;
|
|
}
|
|
|
|
public abstract byte[] ToStorage();
|
|
|
|
public object AsObject => Value;
|
|
|
|
public virtual string AsString => (string)Value;
|
|
|
|
public virtual bool AsBool => (bool)Value;
|
|
public virtual byte AsByte => (byte)Value;
|
|
public virtual char AsChar => (char)Value;
|
|
public virtual short AsShort => (short)Value;
|
|
public virtual int AsInt => (int)Value;
|
|
public virtual long AsLong => (long)Value;
|
|
public virtual ushort AsUShort => (ushort)Value;
|
|
public virtual uint AsUInt => (uint)Value;
|
|
public virtual ulong AsULong => (ulong)Value;
|
|
|
|
public virtual double AsDouble => (double)Value;
|
|
public virtual float AsFloat => (float)Value;
|
|
|
|
public virtual Guid AsGuid => (Guid)Value;
|
|
public virtual DateTime AsDateTime => (DateTime)Value;
|
|
public virtual TimeSpan AsTimeSpan => (TimeSpan)Value;
|
|
|
|
public virtual ODBDocument AsDocument => (ODBDocument)this;
|
|
|
|
public virtual void Store(BinaryWriter storage)
|
|
{
|
|
byte[] storageBytes = ToStorage();
|
|
|
|
storage.Write(storageTypeCode);
|
|
storage.Write(storageBytes.Length);
|
|
storage.Write(storageBytes, 0, storageBytes.Length);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return Value.GetHashCode();
|
|
}
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is ODBValue)
|
|
{
|
|
ODBValue you = obj as ODBValue;
|
|
return Value.Equals(you.Value);
|
|
}
|
|
return Value.Equals(obj);
|
|
}
|
|
|
|
|
|
|
|
//static Dictionary<Type, ODBValueFactory> valueFactories = new Dictionary<Type, ODBValueFactory>();
|
|
//public static void RegisterValueFactory(Type type, ODBValueFactory factory)
|
|
//{
|
|
// valueFactories.Add(type, factory);
|
|
//}
|
|
|
|
public static implicit operator ODBValue(ValueType v)
|
|
{
|
|
return ODBMapper.Default.MapValue(v);
|
|
}
|
|
public static implicit operator ODBValue(String v)
|
|
{
|
|
return ODBMapper.Default.MapValue(v);
|
|
}
|
|
|
|
public static ODBValue FromNative(object v)
|
|
{
|
|
return ODBMapper.Default.MapValue(v);
|
|
}
|
|
|
|
static Dictionary<int, ODBDeserialize> valueDeserializers = new Dictionary<int, ODBDeserialize>();
|
|
public static void RegisterDeserializer(int storageTypeCode, ODBDeserialize deserialize)
|
|
{
|
|
valueDeserializers.Add(storageTypeCode, deserialize);
|
|
}
|
|
|
|
public static ODBValue Deserialize(byte[] buffer, ref int offset)
|
|
{
|
|
int storageTypeCode = BitConverter.ToInt32(buffer, offset);
|
|
int storageLength = BitConverter.ToInt32(buffer, offset + 4);
|
|
ODBValue value = valueDeserializers[storageTypeCode](buffer, offset + 8, storageLength);
|
|
offset += 8 + storageLength;
|
|
return value;
|
|
}
|
|
|
|
public static ODBValue Read(Stream stream)
|
|
{
|
|
int storageTypeCode = stream.ReadInteger();
|
|
int storageLength = stream.ReadInteger();
|
|
byte[] b = new byte[storageLength];
|
|
stream.Read(b, 0, storageLength);
|
|
|
|
return valueDeserializers[storageTypeCode](b, 0, storageLength);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return String.Format("[ODBValue Value={0}]", Value);
|
|
}
|
|
}
|
|
}
|