This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
AsciiType.cs
105 lines (80 loc) · 1.89 KB
/
AsciiType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
namespace FluentCassandra.Types
{
public class AsciiType : CassandraObject
{
private static readonly AsciiTypeConverter Converter = new AsciiTypeConverter();
#region Implimentation
protected override object GetValueInternal(Type type)
{
return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
_value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
{
return Converter.ToBigEndian(_value);
}
public override void SetValueFromBigEndian(byte[] value)
{
_value = Converter.FromBigEndian(value);
}
protected override TypeCode TypeCode
{
get { return TypeCode.String; }
}
public override string ToString()
{
return _value;
}
#endregion
protected override object GetRawValue() { return _value; }
private string _value;
#region Equality
public override bool Equals(object obj)
{
if (obj is AsciiType)
return _value == ((AsciiType)obj)._value;
return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
#endregion
#region Conversion
public static implicit operator string(AsciiType type)
{
return type._value;
}
public static implicit operator AsciiType(string o)
{
return new AsciiType {
_value = o
};
}
public static implicit operator AsciiType(UTF8Type o)
{
return new AsciiType {
_value = o.GetValue<string>()
};
}
public static implicit operator AsciiType(byte[] o) { return ConvertFrom(o); }
public static implicit operator byte[](AsciiType o) { return ConvertTo<byte[]>(o); }
private static T ConvertTo<T>(AsciiType type)
{
if (type == null)
return default(T);
return type.GetValue<T>();
}
private static AsciiType ConvertFrom(object o)
{
var type = new AsciiType();
type.SetValue(o);
return type;
}
#endregion
}
}