Skip to content

Commit

Permalink
Avoid string conversions in FieldMap:
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsan31 committed Aug 18, 2023
1 parent 81ba7ad commit 8eb3c3f
Showing 1 changed file with 67 additions and 65 deletions.
132 changes: 67 additions & 65 deletions QuickFIXn/Message/FieldMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,18 +324,17 @@ public Group GetGroup(int num, Group group)
/// <exception cref="FieldNotFoundException" />
public int GetInt(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
if (fld.GetType() == typeof(IntField))
return ((IntField)fld).Obj;
else
return IntConverter.Convert(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is FieldBase<int> intField)
{
throw new FieldNotFoundException(tag);
return intField.Obj;
}

return IntConverter.Convert(fld.ToString());
}

/// <summary>
Expand Down Expand Up @@ -366,25 +365,29 @@ public ulong GetULong(int tag)
/// <param name="tag">the FIX tag</param>
/// <returns>the DateTime value</returns>
/// <exception cref="FieldNotFoundException" />
public System.DateTime GetDateTime(int tag)
public DateTime GetDateTime(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
Type fldTyp = fld.GetType();
if (fldTyp == typeof(DateTimeField))
return ((DateTimeField)(fld)).Obj;
if (fldTyp == typeof(DateOnlyField))
return GetDateOnly(tag);
if (fldTyp == typeof(TimeOnlyField))
return GetTimeOnly(tag);
else
return DateTimeConverter.ConvertToDateTime(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is DateOnlyField dateOnlyField)
{
throw new FieldNotFoundException(tag);
return dateOnlyField.Obj.Date;
}

if (fld is TimeOnlyField timeOnlyField)
{
return new DateTime(1980, 01, 01).Add(timeOnlyField.Obj.TimeOfDay);
}

if (fld is FieldBase<DateTime> dateTimeField)
{
return dateTimeField.Obj;
}

return DateTimeConverter.ConvertToDateTime(fld.ToString());
}

/// <summary>
Expand All @@ -393,17 +396,19 @@ public System.DateTime GetDateTime(int tag)
/// <param name="tag">the FIX tag</param>
/// <returns>the DateTime value</returns>
/// <exception cref="FieldNotFoundException" />
public System.DateTime GetDateOnly(int tag)
public DateTime GetDateOnly(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
return DateTimeConverter.ConvertToDateOnly(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is FieldBase<DateTime> dateTimeField)
{
throw new FieldNotFoundException(tag);
return dateTimeField.Obj.Date;
}

return DateTimeConverter.ConvertToDateOnly(fld.ToString());
}

/// <summary>
Expand All @@ -412,17 +417,19 @@ public System.DateTime GetDateOnly(int tag)
/// <param name="tag">the FIX tag</param>
/// <returns>the DateTime value</returns>
/// <exception cref="FieldNotFoundException" />
public System.DateTime GetTimeOnly(int tag)
public DateTime GetTimeOnly(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
return DateTimeConverter.ConvertToTimeOnly(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is FieldBase<DateTime> dateTimeField)
{
throw new FieldNotFoundException(tag);
return new DateTime(1980, 01, 01).Add(dateTimeField.Obj.TimeOfDay);
}

return DateTimeConverter.ConvertToTimeOnly(fld.ToString());
}

/// <summary>
Expand All @@ -433,18 +440,17 @@ public System.DateTime GetTimeOnly(int tag)
/// <exception cref="FieldNotFoundException" />
public bool GetBoolean(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
if (fld.GetType() == typeof(BooleanField))
return ((BooleanField)fld).Obj;
else
return BoolConverter.Convert(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is FieldBase<bool> boolField)
{
throw new FieldNotFoundException(tag);
return boolField.Obj;
}

return BoolConverter.Convert(fld.ToString());
}

/// <summary>
Expand All @@ -453,16 +459,14 @@ public bool GetBoolean(int tag)
/// <param name="tag">the FIX tag</param>
/// <returns>the string value</returns>
/// <exception cref="FieldNotFoundException" />
public String GetString(int tag)
{
try
public string GetString(int tag)
{
return _fields[tag].ToString();
}
catch (System.Collections.Generic.KeyNotFoundException)
if (!_fields.TryGetValue(tag, out IField fld))
{
throw new FieldNotFoundException(tag);
}

return fld.ToString();
}

/// <summary>
Expand All @@ -473,18 +477,17 @@ public String GetString(int tag)
/// <exception cref="FieldNotFoundException" />
public char GetChar(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
if (fld.GetType() == typeof(CharField))
return ((CharField)fld).Obj;
else
return CharConverter.Convert(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is FieldBase<char> charField)
{
throw new FieldNotFoundException(tag);
return charField.Obj;
}

return CharConverter.Convert(fld.ToString());
}

/// <summary>
Expand All @@ -493,20 +496,19 @@ public char GetChar(int tag)
/// <param name="tag">the FIX tag</param>
/// <returns>the decimal value</returns>
/// <exception cref="FieldNotFoundException" />
public Decimal GetDecimal(int tag)
public decimal GetDecimal(int tag)
{
try
if (!_fields.TryGetValue(tag, out IField fld))
{
Fields.IField fld = _fields[tag];
if (fld.GetType() == typeof(DecimalField))
return ((DecimalField)fld).Obj;
else
return DecimalConverter.Convert(fld.ToString());
throw new FieldNotFoundException(tag);
}
catch (System.Collections.Generic.KeyNotFoundException)

if (fld is FieldBase<decimal> decimalField)
{
throw new FieldNotFoundException(tag);
return decimalField.Obj;
}

return DecimalConverter.Convert(fld.ToString());
}

/// <summary>
Expand Down

0 comments on commit 8eb3c3f

Please sign in to comment.