private static int WriteField(BinaryWriter bw, FudgeFieldType type, object value, short?ordinal, string name, IFudgeTaxonomy taxonomy)// throws IOException { CheckOutputStream(bw); if (type == null) { throw new ArgumentNullException("Must provide the type of data encoded."); } if (value == null) { throw new ArgumentNullException("Must provide the value to encode."); } // First, normalize the name/ordinal bit NormalizeNameAndOrdinal(ref name, ref ordinal, taxonomy); // Reduce the value to minimal form value = type.Minimize(value, ref type); // Now do the field header int valueSize = type.IsVariableSize ? type.GetVariableSize(value, taxonomy) : type.FixedSize; int nWritten = WriteFieldHeader(bw, valueSize, type.IsVariableSize, type.TypeId, ordinal, name); // Finally the value if (value != null) { Debug.Assert(type != null); nWritten += WriteFieldValue(bw, type, value, valueSize, taxonomy); } return(nWritten); }
/// <inheritdoc/> public override object Minimize(object value, ref FudgeFieldType type) { // If it's a pure date we can just use a date field instead if (value == null) { return(null); } FudgeDateTime fdt = value as FudgeDateTime; if (fdt == null) { throw new ArgumentException("value must be FudgeDateTime"); } if (fdt.Precision == FudgeDateTimePrecision.Day || fdt.Time.Equals(FudgeTime.Midnight)) { // We can just use a date type = DateFieldType.Instance; return(fdt.Date); } return(fdt); }
/// <inheritdoc/> public void WriteField(string name, int?ordinal, FudgeFieldType type, object value) { foreach (var writer in writers) { writer.WriteField(name, ordinal, type, value); } }
private static object ReadFieldValue( BinaryReader br, FudgeFieldType type, int varSize) //throws IOException { Debug.Assert(type != null); Debug.Assert(br != null); // Special fast-pass for known field types switch (type.TypeId) { case FudgeTypeDictionary.BOOLEAN_TYPE_ID: return(br.ReadBoolean()); case FudgeTypeDictionary.SBYTE_TYPE_ID: return(br.ReadSByte()); case FudgeTypeDictionary.SHORT_TYPE_ID: return(br.ReadInt16()); case FudgeTypeDictionary.INT_TYPE_ID: return(br.ReadInt32()); case FudgeTypeDictionary.LONG_TYPE_ID: return(br.ReadInt64()); case FudgeTypeDictionary.FLOAT_TYPE_ID: return(br.ReadSingle()); case FudgeTypeDictionary.DOUBLE_TYPE_ID: return(br.ReadDouble()); } return(type.ReadValue(br, varSize)); }
/// <inheritdoc/> public void WriteField(string name, int?ordinal, FudgeFieldType type, object value) { writer.WriteStartElement(name); if (type != IndicatorFieldType.Instance) { writer.WriteValue(value); } writer.WriteEndElement(); }
public void SimpleTypeLookup() { FudgeFieldType type = null; type = new FudgeTypeDictionary().GetByCSharpType(typeof(bool)); Assert.NotNull(type); Assert.Equal(PrimitiveFieldTypes.BooleanType.TypeId, type.TypeId); type = new FudgeTypeDictionary().GetByCSharpType(typeof(Boolean)); Assert.NotNull(type); Assert.Equal(PrimitiveFieldTypes.BooleanType.TypeId, type.TypeId); }
public void MinimizeToIndicatorAndBack() { var baType = ByteArrayFieldType.VariableSizedInstance; FudgeFieldType type = baType; byte[] data = new byte[0]; Assert.Same(IndicatorType.Instance, baType.Minimize(data, ref type)); Assert.Same(IndicatorFieldType.Instance, type); Assert.Equal(data, baType.ConvertValueFrom(IndicatorType.Instance)); }
/// <inheritdoc/> public void WriteField(string name, int?ordinal, FudgeFieldType type, object value) { if (type == FudgeMsgFieldType.Instance) { // Got a sub-message, so we need to stream that properly var msg = (IFudgeFieldContainer)value; StartSubMessage(name, ordinal); WriteFields(msg.GetAllFields()); EndSubMessage(); } else { int nWritten = WriteField(memWriter, type, value, (short?)ordinal, name, taxonomy); stack.Peek().FinalMessageContentsSize += nWritten; } }
public void AddField(string name, FudgeFieldType type, object value) { int index; if (Indices.TryGetValue(name, out index)) { // Already got Fields[index].Value.Add(new TypedValue(type, value)); } else { // New entry var valueList = new List <TypedValue>(); valueList.Add(new TypedValue(type, value)); Indices[name] = Fields.Count; Fields.Add(new KeyValuePair <string, List <TypedValue> >(name, valueList)); } }
/// <inheritdoc/> public void WriteField(string name, int?ordinal, FudgeFieldType type, object value) { if (Depth == 0) { throw new InvalidOperationException("Cannot write a field when not in an existing message"); } if (value is IFudgeFieldContainer) { StartSubMessage(name, ordinal); WriteFields((IFudgeFieldContainer)value); EndSubMessage(); } else { string fieldName = FormName(name, ordinal); stack.Peek().AddField(fieldName, type, value); } }
// Write is called from the various StreamingMessage.Add methods - rather than it storing the value // we just write it out to the output stream private void Write(string fieldName, int?ordinal, FudgeFieldType type, object value) { if (type == null) { type = context.TypeHandler.DetermineTypeFromValue(value); } if (type == null) { WriteObject(fieldName, ordinal, value, true, true); } else { if (type == FudgeMsgFieldType.Instance) { // As references are based on the number of messages, we have to update to take this one into account currentMessageId += CountMessages((IFudgeFieldContainer)value); } writer.WriteField(fieldName, ordinal, type, value); } }
/// <summary> /// Delegate for reducing integers to the smallest encoding available. /// </summary> /// <param name="valueAsLong">value to reduce</param> /// <param name="type">original type</param> /// <returns>the original value, recast to a smaller type if reduction has taken place</returns> private static object MinimizeIntegers(long valueAsLong, ref FudgeFieldType type) { object value = valueAsLong; if ((valueAsLong >= sbyte.MinValue) && (valueAsLong <= sbyte.MaxValue)) { value = (sbyte)valueAsLong; type = PrimitiveFieldTypes.SByteType; } else if ((valueAsLong >= short.MinValue) && (valueAsLong <= short.MaxValue)) { value = (short)valueAsLong; type = PrimitiveFieldTypes.ShortType; } else if ((valueAsLong >= int.MinValue) && (valueAsLong <= int.MaxValue)) { value = (int)valueAsLong; type = PrimitiveFieldTypes.IntType; } return value; }
/// <summary> /// Delegate for reducing integers to the smallest encoding available. /// </summary> /// <param name="valueAsLong">value to reduce</param> /// <param name="type">original type</param> /// <returns>the original value, recast to a smaller type if reduction has taken place</returns> private static object MinimizeIntegers(long valueAsLong, ref FudgeFieldType type) { object value = valueAsLong; if ((valueAsLong >= sbyte.MinValue) && (valueAsLong <= sbyte.MaxValue)) { value = (sbyte)valueAsLong; type = PrimitiveFieldTypes.SByteType; } else if ((valueAsLong >= short.MinValue) && (valueAsLong <= short.MaxValue)) { value = (short)valueAsLong; type = PrimitiveFieldTypes.ShortType; } else if ((valueAsLong >= int.MinValue) && (valueAsLong <= int.MaxValue)) { value = (int)valueAsLong; type = PrimitiveFieldTypes.IntType; } return(value); }
private static TypeKind CalcKind(FudgeContext context, TypeDataCache typeCache, Type type, FudgeFieldNameConvention fieldNameConvention, out TypeData subType, out TypeData subType2, out FudgeFieldType fieldType) { // REVIEW 2010-02-14 t0rx -- There seems to be some duplication here with the FudgeSurrogateSelector, should look at joining up subType = null; subType2 = null; fieldType = context.TypeDictionary.GetByCSharpType(type); if (fieldType != null) { // Just a simple field return(TypeKind.FudgePrimitive); } // Check for arrays if (type.IsArray) { subType = typeCache.GetTypeData(type.GetElementType(), fieldNameConvention); return(TypeKind.Inline); } // Check for dictionaries Type keyType, valueType; if (DictionarySurrogate.IsDictionary(type, out keyType, out valueType)) { subType = typeCache.GetTypeData(keyType, fieldNameConvention); subType2 = typeCache.GetTypeData(valueType, fieldNameConvention); return(TypeKind.Inline); } // Check for lists Type elementType; if (ListSurrogate.IsList(type, out elementType)) { subType = typeCache.GetTypeData(elementType, fieldNameConvention); return(TypeKind.Inline); } return(TypeKind.Reference); }
public void Minimsation() { var msg = new FudgeMsg(context); var dt = new DateTime(1990, 2, 1); var fdt = new FudgeDateTime(1990, 2, 1, 0, 0, 0, 0, FudgeDateTimePrecision.Day); msg.Add("dt", dt); msg.Add("fdt", fdt); Assert.Same(DateFieldType.Instance, msg.GetByName("dt").Type); Assert.IsType <FudgeDate>(msg.GetByName("dt").Value); Assert.Same(DateFieldType.Instance, msg.GetByName("fdt").Type); Assert.IsType <FudgeDate>(msg.GetByName("fdt").Value); Assert.Equal(dt, msg.GetValue <DateTime>("dt")); Assert.Equal(fdt, msg.GetValue <FudgeDateTime>("fdt")); // Error cases FudgeFieldType type = null; Assert.Equal(null, DateTimeFieldType.Instance.Minimize(null, ref type)); Assert.Throws <ArgumentException>(() => DateTimeFieldType.Instance.Minimize("fred", ref type)); }
private static object MinimizeIntegers(int value, ref FudgeFieldType type) { return MinimizeIntegers((long)value, ref type); }
/// <summary> /// Attempts to reduce a variable length byte array to one of the standard Fudge types. The value /// is never modified, just the type changed. /// </summary> /// <param name="value">value to process</param> /// <param name="type">type definition</param> /// <returns>the value</returns> public override object Minimize(object value, ref FudgeFieldType type) { if (value == null) { return(value); } byte[] array = value as byte[]; if (array == null) { throw new ArgumentException("value must be a byte array"); } // Any size can be minimized to Indicator if (array.Length == 0) { type = IndicatorFieldType.Instance; return(IndicatorType.Instance); } if (!IsVariableSize) { // We're already fixed, so no further minimization possible return(value); } switch (array.Length) { case 4: type = Length4Instance; break; case 8: type = Length8Instance; break; case 16: type = Length16Instance; break; case 20: type = Length20Instance; break; case 32: type = Length32Instance; break; case 64: type = Length64Instance; break; case 128: type = Length128Instance; break; case 256: type = Length256Instance; break; case 512: type = Length512Instance; break; default: // Have to use variable-sized break; } return(value); }
/// <inheritdoc/> public void WriteField(string name, int? ordinal, FudgeFieldType type, object value) { if (type == FudgeMsgFieldType.Instance) { // Got a sub-message, so we need to stream that properly var msg = (IFudgeFieldContainer)value; StartSubMessage(name, ordinal); WriteFields(msg.GetAllFields()); EndSubMessage(); } else { int nWritten = WriteField(memWriter, type, value, (short?)ordinal, name, taxonomy); stack.Peek().FinalMessageContentsSize += nWritten; } }
public void Add(string name, int?ordinal, FudgeFieldType type, object value) { serializationContext.Write(name, ordinal, type, value); }
public TypedValue(FudgeFieldType type, object value) { this.Type = type; this.Value = value; }
/** * */ protected void ConsumeFieldData() //throws IOException { sbyte fieldPrefix = Reader.ReadSByte(); int typeId = Reader.ReadByte(); int nRead = 2; bool fixedWidth = FudgeFieldPrefixCodec.IsFixedWidth(fieldPrefix); bool hasOrdinal = FudgeFieldPrefixCodec.HasOrdinal(fieldPrefix); bool hasName = FudgeFieldPrefixCodec.HasName(fieldPrefix); int?ordinal = null; if (hasOrdinal) { ordinal = Reader.ReadInt16(); nRead += 2; } string name = null; if (hasName) { int nameSize = Reader.ReadByte(); nRead++; name = StringFieldType.ReadString(Reader, nameSize); nRead += nameSize; } else if (ordinal != null) { if (Taxonomy != null) { name = Taxonomy.GetFieldName((short)ordinal.Value); } } FudgeFieldType type = FudgeContext.TypeDictionary.GetByTypeId(typeId); if (type == null) { if (fixedWidth) { throw new FudgeRuntimeException("Unknown fixed width type " + typeId + " for field " + ordinal + ":" + name + " cannot be handled."); } type = FudgeContext.TypeDictionary.GetUnknownType(typeId); } int varSize = 0; if (!fixedWidth) { int varSizeBytes = FudgeFieldPrefixCodec.GetFieldWidthByteCount(fieldPrefix); switch (varSizeBytes) { case 0: varSize = 0; break; case 1: varSize = Reader.ReadByte(); nRead += 1; break; case 2: varSize = Reader.ReadInt16(); nRead += 2; break; case 4: varSize = Reader.ReadInt32(); nRead += 4; break; default: throw new FudgeRuntimeException("Illegal number of bytes indicated for variable width encoding: " + varSizeBytes); } } FieldName = name; FieldOrdinal = ordinal; FieldType = type; MessageProcessingState currMsgProcessingState = processingStack.Peek(); currMsgProcessingState.Consumed += nRead; if (typeId == FudgeTypeDictionary.FUDGE_MSG_TYPE_ID) { CurrentElement = FudgeStreamElement.SubmessageFieldStart; FieldValue = null; MessageProcessingState subState = new MessageProcessingState(); subState.MessageSize = varSize; subState.Consumed = 0; processingStack.Push(subState); } else { CurrentElement = FudgeStreamElement.SimpleField; FieldValue = ReadFieldValue(Reader, FieldType, varSize); if (fixedWidth) { currMsgProcessingState.Consumed += type.FixedSize; } else { currMsgProcessingState.Consumed += varSize; } } }
/// <inheritdoc/> public void WriteField(string name, int? ordinal, FudgeFieldType type, object value) { foreach (var writer in writers) writer.WriteField(name, ordinal, type, value); }
private void WriteValue(FudgeFieldType type, object value, string currentIndent) { if (value == null) { writer.Write(JSONConstants.NullLiteral); } else if (type == null && value is JSONObject) // Type will always be null for sub-objects, so this is a fast test compared to "is" { WriteObject((JSONObject)value, currentIndent); } else { type = context.TypeHandler.DetermineTypeFromValue(value); if (type == null) { // Unknown type, so just treat it as a string writer.Write(EscapeAndWrapString(value.ToString())); } else { switch (type.TypeId) { case FudgeTypeDictionary.BOOLEAN_TYPE_ID: writer.Write((bool)value ? JSONConstants.TrueLiteral : JSONConstants.FalseLiteral); break; case FudgeTypeDictionary.SBYTE_TYPE_ID: case FudgeTypeDictionary.SHORT_TYPE_ID: case FudgeTypeDictionary.INT_TYPE_ID: case FudgeTypeDictionary.LONG_TYPE_ID: case FudgeTypeDictionary.FLOAT_TYPE_ID: case FudgeTypeDictionary.DOUBLE_TYPE_ID: writer.Write(value.ToString()); break; case FudgeTypeDictionary.BYTE_ARRAY_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_4_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_8_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_16_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_20_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_32_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_64_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_128_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_256_TYPE_ID: case FudgeTypeDictionary.BYTE_ARR_512_TYPE_ID: case FudgeTypeDictionary.SHORT_ARRAY_TYPE_ID: case FudgeTypeDictionary.INT_ARRAY_TYPE_ID: case FudgeTypeDictionary.LONG_ARRAY_TYPE_ID: case FudgeTypeDictionary.FLOAT_ARRAY_TYPE_ID: case FudgeTypeDictionary.DOUBLE_ARRAY_TYPE_ID: WritePrimitiveArray((Array)value); break; case FudgeTypeDictionary.INDICATOR_TYPE_ID: writer.Write(JSONConstants.NullLiteral); break; default: // Anything else just treat as a string writer.Write(EscapeAndWrapString(value.ToString())); break; } } } }
//throws IOException private static object ReadFieldValue( BinaryReader br, FudgeFieldType type, int varSize) { Debug.Assert(type != null); Debug.Assert(br != null); // Special fast-pass for known field types switch (type.TypeId) { case FudgeTypeDictionary.BOOLEAN_TYPE_ID: return br.ReadBoolean(); case FudgeTypeDictionary.SBYTE_TYPE_ID: return br.ReadSByte(); case FudgeTypeDictionary.SHORT_TYPE_ID: return br.ReadInt16(); case FudgeTypeDictionary.INT_TYPE_ID: return br.ReadInt32(); case FudgeTypeDictionary.LONG_TYPE_ID: return br.ReadInt64(); case FudgeTypeDictionary.FLOAT_TYPE_ID: return br.ReadSingle(); case FudgeTypeDictionary.DOUBLE_TYPE_ID: return br.ReadDouble(); } return type.ReadValue(br, varSize); }
public TemporaryField(FudgeFieldType type, object value) { this.type = type; this.value = value; }
public void WriteField(string name, int? ordinal, FudgeFieldType type, object value) { Debug.WriteLine(string.Format("Field (\"{0}\", {1}, {2})", name, ordinal, type)); }
//throws IOException private static int WriteFieldValue(BinaryWriter bw, FudgeFieldType type, object value, int valueSize, IFudgeTaxonomy taxonomy) { // Note that we fast-path types for which at compile time we know how to handle // in an optimized way. This is because this particular method is known to // be a massive hot-spot for performance. int nWritten = 0; switch (type.TypeId) { case FudgeTypeDictionary.BOOLEAN_TYPE_ID: bw.Write((bool)value); nWritten = 1; break; case FudgeTypeDictionary.SBYTE_TYPE_ID: bw.Write((sbyte)value); nWritten = 1; break; case FudgeTypeDictionary.SHORT_TYPE_ID: bw.Write((short)value); nWritten = 2; break; case FudgeTypeDictionary.INT_TYPE_ID: bw.Write((int)value); nWritten = 4; break; case FudgeTypeDictionary.LONG_TYPE_ID: bw.Write((long)value); nWritten = 8; break; case FudgeTypeDictionary.FLOAT_TYPE_ID: bw.Write((float)value); nWritten = 4; break; case FudgeTypeDictionary.DOUBLE_TYPE_ID: bw.Write((double)value); nWritten = 8; break; } if (nWritten == 0) { if (type.IsVariableSize) { nWritten = WriteVariableSize(bw, valueSize); } else { nWritten = type.FixedSize; } if (value is IFudgeFieldContainer) { IFudgeFieldContainer subMsg = (IFudgeFieldContainer)value; WriteMsgFields(bw, subMsg, taxonomy); } else { type.WriteValue(bw, value); } } return nWritten; }
// throws IOException private static int WriteField(BinaryWriter bw, FudgeFieldType type, object value, short? ordinal, string name, IFudgeTaxonomy taxonomy) { CheckOutputStream(bw); if (type == null) { throw new ArgumentNullException("Must provide the type of data encoded."); } if (value == null) { throw new ArgumentNullException("Must provide the value to encode."); } // First, normalize the name/ordinal bit NormalizeNameAndOrdinal(ref name, ref ordinal, taxonomy); // Reduce the value to minimal form value = type.Minimize(value, ref type); // Now do the field header int valueSize = type.IsVariableSize ? type.GetVariableSize(value, taxonomy) : type.FixedSize; int nWritten = WriteFieldHeader(bw, valueSize, type.IsVariableSize, type.TypeId, ordinal, name); // Finally the value if (value != null) { Debug.Assert(type != null); nWritten += WriteFieldValue(bw, type, value, valueSize, taxonomy); } return nWritten; }
private static int WriteFieldValue(BinaryWriter bw, FudgeFieldType type, object value, int valueSize, IFudgeTaxonomy taxonomy) //throws IOException { // Note that we fast-path types for which at compile time we know how to handle // in an optimized way. This is because this particular method is known to // be a massive hot-spot for performance. int nWritten = 0; switch (type.TypeId) { case FudgeTypeDictionary.BOOLEAN_TYPE_ID: bw.Write((bool)value); nWritten = 1; break; case FudgeTypeDictionary.SBYTE_TYPE_ID: bw.Write((sbyte)value); nWritten = 1; break; case FudgeTypeDictionary.SHORT_TYPE_ID: bw.Write((short)value); nWritten = 2; break; case FudgeTypeDictionary.INT_TYPE_ID: bw.Write((int)value); nWritten = 4; break; case FudgeTypeDictionary.LONG_TYPE_ID: bw.Write((long)value); nWritten = 8; break; case FudgeTypeDictionary.FLOAT_TYPE_ID: bw.Write((float)value); nWritten = 4; break; case FudgeTypeDictionary.DOUBLE_TYPE_ID: bw.Write((double)value); nWritten = 8; break; } if (nWritten == 0) { if (type.IsVariableSize) { nWritten = WriteVariableSize(bw, valueSize); } else { nWritten = type.FixedSize; } if (value is IFudgeFieldContainer) { IFudgeFieldContainer subMsg = (IFudgeFieldContainer)value; WriteMsgFields(bw, subMsg, taxonomy); } else { type.WriteValue(bw, value); } } return(nWritten); }
/// <inheritdoc/> public void WriteField(string name, int? ordinal, FudgeFieldType type, object value) { if (Depth == 0) throw new InvalidOperationException("Cannot write a field when not in an existing message"); if (value is IFudgeFieldContainer) { StartSubMessage(name, ordinal); WriteFields((IFudgeFieldContainer)value); EndSubMessage(); } else { string fieldName = FormName(name, ordinal); stack.Peek().AddField(fieldName, type, value); } }
public void AddField(string name, FudgeFieldType type, object value) { int index; if (Indices.TryGetValue(name, out index)) { // Already got Fields[index].Value.Add(new TypedValue(type, value)); } else { // New entry var valueList = new List<TypedValue>(); valueList.Add(new TypedValue(type, value)); Indices[name] = Fields.Count; Fields.Add(new KeyValuePair<string,List<TypedValue>>(name, valueList)); } }
public void WriteField(string name, int?ordinal, FudgeFieldType type, object value) { Debug.WriteLine(string.Format("Field (\"{0}\", {1}, {2})", name, ordinal, type)); }
/// <inheritdoc/> public void WriteField(string name, int? ordinal, FudgeFieldType type, object value) { current.Add(name, ordinal, type, value); }
/// <inheritdoc/> public void WriteField(string name, int? ordinal, FudgeFieldType type, object value) { writer.WriteStartElement(name); if (type != IndicatorFieldType.Instance) { writer.WriteValue(value); } writer.WriteEndElement(); }