/// <summary> /// Generates types for sequence fields /// </summary> /// <param name="emitter">The emitter to write to</param> /// <param name="def">The sequence definition</param> private void _generateSequenceFields(CSharpEmitter emitter, SequenceDefinition def) { foreach(var field in def.Fields) { var effectiveFieldName = _sequenceFieldEffectiveNameForType(field); _generateDefinition(emitter, null, effectiveFieldName, field.Type); } }
/// <summary> /// Generates a sequence type /// </summary> /// <param name="typeName">The name of the type</param> /// <param name="def">The sequence definition</param> private void _generateSequence(CSharpEmitter emitter, string typeName, string fieldName, SequenceDefinition def, bool root = false, string tag = null, string choiceBase = null) { string[] bases = choiceBase == null ? new string[0] { } : new string[] { choiceBase }; typeName = _getDefinitionName(typeName, fieldName, def); using(var cls = emitter.Class(typeName, false, bases)) { if(tag != null) { cls.OverrideProperty("Tag", "Tags", "return Tags." + tag + ";"); cls.WriteLine(); } foreach (var field in def.Fields) { var fieldName2 = _transformFieldName(field.Name); var typeName2 = _sequenceFieldTypeName(field); cls.Property(fieldName2, typeName2); cls.WriteLine(); } var cparams = def.Fields.Select(f => new Parameter( _sequenceFieldTypeName(f), _fieldNameToTempName(f.Name))) .ToArray(); using (var cons = cls.Constructor(typeName, cparams, Access.Public)) { foreach (var field in def.Fields) { cons.WriteLine("this.{0} = {1};", _transformFieldName(field.Name), _fieldNameToTempName(field.Name)); } } cls.WriteLine(); var schemaStr = "new SequenceSchema(false, " + Environment.NewLine + string.Join("," + Environment.NewLine, def.Fields.Select(f => cls.IndentString(1) + "new FieldSchema(\"" + _transformFieldName(f.Name) + "\", " + f.Tag + ", Value<" + _sequenceFieldTypeName(f) + ">.Schema)").ToArray()) + ")"; cls.StaticReadonlyField("Schema", "ISchema", schemaStr, @new:!string.IsNullOrEmpty(choiceBase)); cls.WriteLine(); using (var load = cls.StaticMethod("Load", typeName, new Parameter[] { new Parameter("IValueStream", "stream") }, @new:!string.IsNullOrEmpty(choiceBase))) { load.WriteLine("stream.EnterSequence();"); foreach(var field in def.Fields) { load.WriteLine("var {0} = Value<{1}>.Load(stream);", _fieldNameToTempName(field.Name), _sequenceFieldTypeName(field)); } load.WriteLine("stream.LeaveSequence();"); load.WriteLine("return new " + typeName + "(" + string.Join(", ", def.Fields.Select(f => _fieldNameToTempName(f.Name)).ToArray()) + ");"); } cls.WriteLine(); using (var save = cls.StaticMethod("Save", "void", new Parameter[] { new Parameter("IValueSink", "sink"), new Parameter(typeName, "value") })) { save.WriteLine("sink.EnterSequence();"); foreach (var field in def.Fields) { save.WriteLine("Value<{0}>.Save(sink, value.{1});", _sequenceFieldTypeName(field), _transformFieldName(field.Name)); } save.WriteLine("sink.LeaveSequence();"); } if (root) _generateSequenceFields(cls, def); } if (!root) _generateSequenceFields(emitter, def); }