Skip to content

Commit

Permalink
Avoid string conversions in FieldMap.Get{FieldType} where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-Hague authored and gbirchmeier committed Jan 20, 2024
1 parent 58023d3 commit 4c0d16b
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 @@ -326,18 +326,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 @@ -368,25 +367,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 @@ -395,17 +398,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 @@ -414,17 +419,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 @@ -435,18 +442,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 @@ -455,16 +461,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)
public string GetString(int tag)
{
try
{
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 @@ -475,18 +479,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 @@ -495,20 +498,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 4c0d16b

Please sign in to comment.