public BinaryFormatterFactory(BinaryFormatType formatType, Encoding encoding) { ArgumentAssert.IsDefinedInEnum(typeof(BinaryFormatType), formatType, "formatType"); ArgumentAssert.IsNotNull(encoding, "encoding"); _formatType = formatType; _encoding = encoding; }
public static Byte IntegerTypeByteCount(this BinaryFormatType integerType) { switch (integerType) { case BinaryFormatType.Byte: return(1); case BinaryFormatType.UInt16: return(2); case BinaryFormatType.UInt24: return(3); case BinaryFormatType.UInt32: return(4); case BinaryFormatType.UInt64: return(8); case BinaryFormatType.SByte: return(1); case BinaryFormatType.Int16: return(2); case BinaryFormatType.Int24: return(3); case BinaryFormatType.Int32: return(4); case BinaryFormatType.Int64: return(8); default: throw new InvalidOperationException(String.Format("Type '{0}' is not an integer type", integerType)); } }
public static Boolean IntegerTypeIsUnsigned(this BinaryFormatType type) { if (type <= BinaryFormatType.UInt64) { return(true); } if (type <= BinaryFormatType.Int64) { return(false); } throw new InvalidOperationException(String.Format("Cannot call this method on a non-integer type '{0}'", type)); }
static void ParseEnumOrFlagsDefinition(BinaryFormatFile file, LfdLineReader reader, NamedObjectDefinition currentObjectDefinition, LfdLine enumDefinitionLine, out LfdLine nextLine, Boolean isFlagsDefinition) { String enumOrFlagsString = isFlagsDefinition ? "Flags" : "Enum"; VerifyFieldCount(enumDefinitionLine, 2); String underlyingIntegerTypeString = enumDefinitionLine.fields[0]; BinaryFormatType underlyingIntegerType = BinaryFormatTypeExtensions.ParseIntegerType(underlyingIntegerTypeString); EnumOrFlagsDefinition definition; try { definition = new EnumOrFlagsDefinition(file, isFlagsDefinition, currentObjectDefinition, underlyingIntegerType, enumDefinitionLine.fields[1]); } catch (FormatException e) { throw new ParseException(enumDefinitionLine, e.Message); } Debug(" Entering {0} '{1}' (IntegerType={2})", enumOrFlagsString, enumDefinitionLine.id, definition.underlyingIntegerType); // // Read enum values // nextLine = reader.ReadLineIgnoreComments(); while (nextLine != null && nextLine.parent == enumDefinitionLine) { LfdLine enumValueLine = nextLine; VerifyFieldCount(enumValueLine, 1); Debug(" {0} {1} {2}", enumOrFlagsString, enumValueLine.id, enumValueLine.fields[0]); if (isFlagsDefinition) { definition.Add(new FlagsValueDefinition(enumValueLine.fields[0], Byte.Parse(enumValueLine.id))); } else { definition.Add(new EnumValueDefinition(enumValueLine.id, enumValueLine.fields[0])); } nextLine = reader.ReadLineIgnoreComments(); } Debug(" Exiting {0} '{1}'", enumOrFlagsString, definition.typeName); }
public EnumOrFlagsDefinition(BinaryFormatFile file, Boolean isFlagsDefinition, NamedObjectDefinition objectDefinedIn, BinaryFormatType underlyingIntegerType, String typeName) { this.isFlagsDefinition = isFlagsDefinition; this.isGlobalType = (objectDefinedIn == null); this.underlyingIntegerType = underlyingIntegerType; this.byteCount = underlyingIntegerType.IntegerTypeByteCount(); this.typeName = typeName; this.typeNameLowerInvariantCase = typeName.ToLowerInvariant(); this.globalReferenceNameLowerInvariant = (objectDefinedIn == null) ? typeNameLowerInvariantCase : (objectDefinedIn.globalReferenceNameLowerInvariant + "." + typeNameLowerInvariantCase); // // Add the definition to the static flags or enum definitions list // if (isFlagsDefinition) { enumValues = null; flagValues = new List <FlagsValueDefinition>(); } else { enumValues = new List <EnumValueDefinition>(); flagValues = null; } // // Add the defintion to the object and the file // if (objectDefinedIn != null) { objectDefinedIn.AddEnumOrFlagDefinition(this); } file.AddEnumOrFlagsDefinition(this); }
public void CreateWriterTest() { // all arguments are valid BinaryFormatType formatType = BinaryFormatType.BigEndian; Encoding encoding = Encoding.Default; BinaryFormatterFactory target = new BinaryFormatterFactory(formatType, encoding); using (Stream stream = new MemoryStream()) { IBinaryWriter actual = target.CreateWriter(new StreamAdapter(stream)); Assert.IsInstanceOfType(actual, typeof(BigEndianBinaryWriter)); Assert.AreEqual(encoding, actual.Encoding); } // invalid stream object try { target.CreateWriter(null); Assert.Fail("ArgumentException exception must be thrown in constructor"); } catch (ArgumentException) { // test passed } target = new BinaryFormatterFactory(BinaryFormatType.None, encoding); try { target.CreateWriter(null); Assert.Fail("NotSupportedException exception must be thrown in constructor"); } catch (NotSupportedException) { // test passed } }
public static Boolean IsIntegerType(this BinaryFormatType type) { return(type <= BinaryFormatType.Int64); }
public static Boolean IsValidUnderlyingEnumIntegerType(this BinaryFormatType type) { return(type <= BinaryFormatType.UInt64); }
public IntegerTypeReference(BinaryFormatType integerType, BinaryFormatArrayType arrayType) : base(integerType.ToString(), integerType, arrayType) { this.byteCount = integerType.IntegerTypeByteCount(); }
public TypeReference(String relativeTypeReferenceString, BinaryFormatType type, BinaryFormatArrayType arrayType) { this.relativeTypeReferenceString = relativeTypeReferenceString; this.type = type; this.arrayType = arrayType; }