本文整理汇总了C#中CodeWriter.Comment方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.Comment方法的具体用法?C# CodeWriter.Comment怎么用?C# CodeWriter.Comment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.Comment方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateClass
public static void GenerateClass(ProtoMessage m, CodeWriter cw, Options options)
{
//Do not generate class code for external classes
if (m.OptionExternal)
{
cw.Comment("Written elsewhere");
cw.Comment(m.OptionAccess + " " + m.OptionType + " " + m.CsType + " {}");
return;
}
//Default class
cw.Summary(m.Comments);
cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.CsType);
GenerateEnums(m, cw);
GenerateProperties(m, cw);
//if(options.GenerateToString...
// ...
if (m.OptionPreserveUnknown)
{
cw.Summary("Values for unknown fields.");
cw.WriteLine("public List<global::SilentOrbit.ProtocolBuffers.KeyValue> PreservedFields;");
cw.WriteLine();
}
if (m.OptionTriggers)
{
cw.Comment("protected virtual void BeforeSerialize() {}");
cw.Comment("protected virtual void AfterDeserialize() {}");
cw.WriteLine();
}
foreach (ProtoMessage sub in m.Messages.Values)
{
GenerateClass(sub, cw, options);
cw.WriteLine();
}
cw.EndBracket();
return;
}
示例2: GenerateReader
//.........这里部分代码省略.........
cw.WriteLine("limit += stream.Position;");
}
if (method == "DeserializeLength")
{
//Important to read stream position after we have read the length field
cw.WriteLine("long limit = stream.Position + length;");
}
cw.WhileBracket("true");
if (method == "DeserializeLengthDelimited" || method == "DeserializeLength")
{
cw.IfBracket("stream.Position >= limit");
cw.WriteLine("if (stream.Position == limit)");
cw.WriteIndent("break;");
cw.WriteLine("else");
cw.WriteIndent("throw new InvalidOperationException(\"Read past max limit\");");
cw.EndBracket();
}
cw.WriteLine("int keyByte = stream.ReadByte();");
cw.WriteLine("if (keyByte == -1)");
if (method == "Deserialize")
cw.WriteIndent("break;");
else
cw.WriteIndent("throw new System.IO.EndOfStreamException();");
//Determine if we need the lowID optimization
bool hasLowID = false;
foreach (Field f in m.Fields.Values)
{
if (f.ID < 16)
{
hasLowID = true;
break;
}
}
if (hasLowID)
{
cw.Comment("Optimized reading of known fields with field ID < 16");
cw.Switch("keyByte");
foreach (Field f in m.Fields.Values)
{
if (f.ID >= 16)
continue;
cw.Dedent();
cw.Comment("Field " + f.ID + " " + f.WireType);
cw.Indent();
cw.Case(((f.ID << 3) | (int)f.WireType));
if (FieldSerializer.FieldReader(f, cw))
cw.WriteLine("continue;");
}
cw.SwitchEnd();
cw.WriteLine();
}
cw.WriteLine("var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream);");
cw.WriteLine();
cw.Comment("Reading field ID > 16 and unknown field ID/wire type combinations");
cw.Switch("key.Field");
cw.Case(0);
cw.WriteLine("throw new InvalidDataException(\"Invalid field id: 0, something went wrong in the stream\");");
foreach (Field f in m.Fields.Values)
{
if (f.ID < 16)
continue;
cw.Case(f.ID);
//Makes sure we got the right wire type
cw.WriteLine("if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire." + f.WireType + ")");
cw.WriteIndent("break;"); //This can be changed to throw an exception for unknown formats.
if (FieldSerializer.FieldReader(f, cw))
cw.WriteLine("continue;");
}
cw.CaseDefault();
if (m.OptionPreserveUnknown)
{
cw.WriteLine("if (instance.PreservedFields == null)");
cw.WriteIndent("instance.PreservedFields = new List<global::SilentOrbit.ProtocolBuffers.KeyValue>();");
cw.WriteLine("instance.PreservedFields.Add(new global::SilentOrbit.ProtocolBuffers.KeyValue(key, global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadValueBytes(stream, key)));");
}
else
{
cw.WriteLine("global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key);");
}
cw.WriteLine("break;");
cw.SwitchEnd();
cw.EndBracket();
cw.WriteLine();
if (m.OptionTriggers)
cw.WriteLine("instance.AfterDeserialize();");
cw.WriteLine("return instance;");
cw.EndBracket();
cw.WriteLine();
}
return;
}
示例3: BytesWriter
/// <summary>
/// Generates inline writer of a length delimited byte array
/// </summary>
static void BytesWriter(Field f, string stream, CodeWriter cw)
{
cw.Comment("Length delimited byte array");
//Original
//cw.WriteLine("ProtocolParser.WriteBytes(" + stream + ", " + memoryStream + ".ToArray());");
//Much slower than original
/*
cw.WriteLine("ProtocolParser.WriteUInt32(" + stream + ", (uint)" + memoryStream + ".Length);");
cw.WriteLine(memoryStream + ".Seek(0, System.IO.SeekOrigin.Begin);");
cw.WriteLine(memoryStream + ".CopyTo(" + stream + ");");
*/
//Same speed as original
/*
cw.WriteLine("ProtocolParser.WriteUInt32(" + stream + ", (uint)" + memoryStream + ".Length);");
cw.WriteLine(stream + ".Write(" + memoryStream + ".ToArray(), 0, (int)" + memoryStream + ".Length);");
*/
//10% faster than original using GetBuffer rather than ToArray
cw.WriteLine("uint length" + f.ID + " = (uint)msField.Length;");
cw.WriteLine(ProtocolParser.Base + ".WriteUInt32(" + stream + ", length" + f.ID + ");");
cw.WriteLine("msField.WriteTo(" + stream + ");");
}
示例4: KeyWriter
static void KeyWriter(string stream, int id, Wire wire, CodeWriter cw)
{
uint n = ((uint)id << 3) | ((uint)wire);
cw.Comment("Key for field: " + id + ", " + wire);
//cw.WriteLine("ProtocolParser.WriteUInt32(" + stream + ", " + n + ");");
VarintWriter(stream, n, cw);
}
示例5: Save
/// <summary>
/// Generate code for reading and writing protocol buffer messages
/// </summary>
public static void Save(ProtoCollection file, Options options)
{
string csPath = options.OutputPath;
CodeWriter.DefaultIndentPrefix = options.UseTabs ? "\t" : " ";
string ext = Path.GetExtension(csPath);
string prefix = csPath.Substring(0, csPath.Length - ext.Length);
string csDir = Path.GetDirectoryName(csPath);
if (Directory.Exists(csDir) == false)
Directory.CreateDirectory(csDir);
//Basic structures
using (var cw = new CodeWriter(csPath))
{
cw.Comment(@"Classes and structures being serialized");
cw.WriteLine();
cw.Comment(@"Generated by ProtocolBuffer
- a pure c# code generation implementation of protocol buffers
Report bugs to: https://silentorbit.com/protobuf/");
cw.WriteLine();
cw.Comment(@"DO NOT EDIT
This file will be overwritten when CodeGenerator is run.
To make custom modifications, edit the .proto file and add //:external before the message line
then write the code and the changes in a separate file.");
cw.WriteLine("using System;");
cw.WriteLine("using System.Collections.Generic;");
cw.WriteLine();
string ns = null; //avoid writing namespace between classes if they belong to the same
foreach (ProtoMessage m in file.Messages.Values)
{
if (ns != m.CsNamespace)
{
if (ns != null) //First time
cw.EndBracket();
cw.Bracket("namespace " + m.CsNamespace);
ns = m.CsNamespace;
}
MessageCode.GenerateClass(m, cw, options);
cw.WriteLine();
}
foreach (ProtoEnum e in file.Enums.Values)
{
if (ns != e.CsNamespace)
{
if (ns != null) //First time
cw.EndBracket();
cw.Bracket("namespace " + e.CsNamespace);
ns = e.CsNamespace;
}
MessageCode.GenerateEnum(e, cw);
cw.WriteLine();
}
cw.EndBracket();
}
//.Serializer.cs
//Code for Reading/Writing
using (var cw = new CodeWriter(prefix + ".Serializer" + ext))
{
cw.Comment(@"This is the backend code for reading and writing");
cw.WriteLine();
cw.Comment(@"Generated by ProtocolBuffer
- a pure c# code generation implementation of protocol buffers
Report bugs to: https://silentorbit.com/protobuf/");
cw.WriteLine();
cw.Comment(@"DO NOT EDIT
This file will be overwritten when CodeGenerator is run.");
cw.WriteLine("using System;");
cw.WriteLine("using System.IO;");
cw.WriteLine("using System.Text;");
cw.WriteLine("using System.Collections.Generic;");
cw.WriteLine();
string ns = null; //avoid writing namespace between classes if they belong to the same
foreach (ProtoMessage m in file.Messages.Values)
{
if (ns != m.CsNamespace)
{
if (ns != null) //First time
cw.EndBracket();
cw.Bracket("namespace " + m.CsNamespace);
ns = m.CsNamespace;
}
MessageSerializer.GenerateClassSerializer(m, cw);
}
cw.EndBracket();
}
string libPath = Path.Combine(Path.GetDirectoryName(csPath), "ProtocolParser.cs");
using (TextWriter codeWriter = new StreamWriter(libPath, false, Encoding.UTF8))
{
//.........这里部分代码省略.........
示例6: GenerateProperties
/// <summary>
/// Generates the properties.
/// </summary>
/// <param name='template'>
/// if true it will generate only properties that are not included by default, because of the [generate=false] option.
/// </param>
static void GenerateProperties(ProtoMessage m, CodeWriter cw)
{
foreach (Field f in m.Fields.Values)
{
if (f.OptionExternal)
cw.WriteLine("//" + GenerateProperty(f) + " // Implemented by user elsewhere");
else
{
if (f.Comments != null)
cw.Summary(f.Comments);
cw.WriteLine(GenerateProperty(f));
cw.WriteLine();
}
}
//Wire format field ID
#if DEBUG
cw.Comment("ProtocolBuffers wire field id");
foreach (Field f in m.Fields.Values)
{
cw.WriteLine("public const int " + f.CsName + "FieldID = " + f.ID + ";");
}
#endif
}
示例7: FieldReader
/// <summary>
/// Return true for normal code and false if generated thrown exception.
/// In the latter case a break is not needed to be generated afterwards.
/// </summary>
public static bool FieldReader(Field f, CodeWriter cw)
{
if (f.Rule == FieldRule.Repeated)
{
//Make sure we are not reading a list of interfaces
if (f.ProtoType.OptionType == "interface")
{
cw.WriteLine("throw new InvalidOperationException(\"Can't deserialize a list of interfaces\");");
return false;
}
if (f.OptionPacked == true)
{
cw.Comment("repeated packed");
cw.WriteLine("long end" + f.ID + " = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);");
cw.WriteLine("end" + f.ID + " += stream.Position;");
cw.WhileBracket("stream.Position < end" + f.ID);
cw.WriteLine("instance." + f.CsName + ".Add(" + FieldReaderType(f, "stream", "br", null) + ");");
cw.EndBracket();
cw.WriteLine("if (stream.Position != end" + f.ID + ")");
cw.WriteIndent("throw new InvalidDataException(\"Read too many bytes in packed data\");");
}
else
{
cw.Comment("repeated");
cw.WriteLine("instance." + f.CsName + ".Add(" + FieldReaderType(f, "stream", "br", null) + ");");
}
}
else
{
if (f.OptionReadOnly)
{
//The only "readonly" fields we can modify
//We could possibly support bytes primitive too but it would require the incoming length to match the wire length
if (f.ProtoType is ProtoMessage)
{
cw.WriteLine(FieldReaderType(f, "stream", "br", "instance." + f.CsName) + ";");
return true;
}
cw.WriteLine("throw new InvalidOperationException(\"Can't deserialize into a readonly primitive field\");");
return false;
}
if (f.ProtoType is ProtoMessage)
{
if (f.ProtoType.OptionType == "struct")
{
cw.WriteLine(FieldReaderType(f, "stream", "br", "ref instance." + f.CsName) + ";");
return true;
}
cw.WriteLine("if (instance." + f.CsName + " == null)");
if (f.ProtoType.OptionType == "interface")
cw.WriteIndent("throw new InvalidOperationException(\"Can't deserialize into a interfaces null pointer\");");
else
cw.WriteIndent("instance." + f.CsName + " = " + FieldReaderType(f, "stream", "br", null) + ";");
cw.WriteLine("else");
cw.WriteIndent(FieldReaderType(f, "stream", "br", "instance." + f.CsName) + ";");
return true;
}
cw.WriteLine("instance." + f.CsName + " = " + FieldReaderType(f, "stream", "br", "instance." + f.CsName) + ";");
}
return true;
}