From 4c0d16bb09518deaf1d380f366adbe1e5beea969 Mon Sep 17 00:00:00 2001 From: Robert Hague Date: Fri, 7 Apr 2023 13:06:53 +0200 Subject: [PATCH] Avoid string conversions in FieldMap.Get{FieldType} where possible --- QuickFIXn/Message/FieldMap.cs | 132 +++++++++++++++++----------------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/QuickFIXn/Message/FieldMap.cs b/QuickFIXn/Message/FieldMap.cs index f44172fc0..06c62adfb 100644 --- a/QuickFIXn/Message/FieldMap.cs +++ b/QuickFIXn/Message/FieldMap.cs @@ -326,18 +326,17 @@ public Group GetGroup(int num, Group group) /// 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 intField) { - throw new FieldNotFoundException(tag); + return intField.Obj; } + + return IntConverter.Convert(fld.ToString()); } /// @@ -368,25 +367,29 @@ public ulong GetULong(int tag) /// the FIX tag /// the DateTime value /// - 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 dateTimeField) + { + return dateTimeField.Obj; } + + return DateTimeConverter.ConvertToDateTime(fld.ToString()); } /// @@ -395,17 +398,19 @@ public System.DateTime GetDateTime(int tag) /// the FIX tag /// the DateTime value /// - 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 dateTimeField) { - throw new FieldNotFoundException(tag); + return dateTimeField.Obj.Date; } + + return DateTimeConverter.ConvertToDateOnly(fld.ToString()); } /// @@ -414,17 +419,19 @@ public System.DateTime GetDateOnly(int tag) /// the FIX tag /// the DateTime value /// - 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 dateTimeField) { - throw new FieldNotFoundException(tag); + return new DateTime(1980, 01, 01).Add(dateTimeField.Obj.TimeOfDay); } + + return DateTimeConverter.ConvertToTimeOnly(fld.ToString()); } /// @@ -435,18 +442,17 @@ public System.DateTime GetTimeOnly(int tag) /// 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 boolField) { - throw new FieldNotFoundException(tag); + return boolField.Obj; } + + return BoolConverter.Convert(fld.ToString()); } /// @@ -455,16 +461,14 @@ public bool GetBoolean(int tag) /// the FIX tag /// the string value /// - 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(); } /// @@ -475,18 +479,17 @@ public String GetString(int tag) /// 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 charField) { - throw new FieldNotFoundException(tag); + return charField.Obj; } + + return CharConverter.Convert(fld.ToString()); } /// @@ -495,20 +498,19 @@ public char GetChar(int tag) /// the FIX tag /// the decimal value /// - 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 decimalField) { - throw new FieldNotFoundException(tag); + return decimalField.Obj; } + + return DecimalConverter.Convert(fld.ToString()); } ///