forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldBase.cs
115 lines (104 loc) · 2.82 KB
/
FieldBase.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
106
107
108
109
110
111
112
113
114
115
using System;
namespace QuickFix.Fields
{
/// <summary>
/// Base class for all field types
/// </summary>
/// <typeparam name="T">Internal storage type</typeparam>
public abstract class FieldBase<T> : IField
{
/// <summary>
/// Constructs a new field with the specified tag and value
/// </summary>
/// <param name="tag">the FIX tag number</param>
/// <param name="obj">the value of the field</param>
public FieldBase(int tag, T obj)
{
_tag = tag;
_obj = obj;
_changed = true;
}
#region Properties
public T Obj
{
get { return _obj; }
set
{
_obj = value;
_changed = true;
}
}
/// <summary>
/// the FIX tag number
/// </summary>
public override int Tag
{
get { return _tag; }
set
{
_tag = value;
_changed = true;
}
}
#endregion
/// <summary>
/// returns full fix string (e.g. "tag=val")
/// </summary>
public override string toStringField()
{
if (_changed.Equals(true))
makeStringFields();
return _stringField;
}
/// <summary>
/// returns field value formatted for fix
/// </summary>
public override string ToString()
{
if (_changed)
makeStringFields();
return _stringVal;
}
/// <summary>
/// length of formatted field (including tag=val\001)
/// </summary>
public override int getLength()
{
if (_changed)
makeStringFields();
return System.Text.Encoding.UTF8.GetByteCount(_stringField) + 1; // +1 for SOH
}
/// <summary>
/// checksum
/// </summary>
public override int getTotal()
{
if (_changed)
makeStringFields();
int sum = 0;
byte[] array = System.Text.Encoding.UTF8.GetBytes(_stringField);
foreach (byte b in array)
{
sum += b;
}
return (sum + 1); // +1 for SOH
}
protected abstract string makeString();
/// <summary>
/// returns tag=val
/// </summary>
private void makeStringFields()
{
_stringVal = makeString();
_stringField = Tag + "=" + _stringVal;
_changed = false;
}
#region Private members
private string _stringField;
private bool _changed;
private T _obj;
private int _tag;
private string _stringVal;
#endregion
}
}