本文整理汇总了C#中CodeWriter.IfBracket方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.IfBracket方法的具体用法?C# CodeWriter.IfBracket怎么用?C# CodeWriter.IfBracket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.IfBracket方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateReader
//.........这里部分代码省略.........
cw.WriteLine("instance." + f.CsName + " = " + f.OptionDefault + ";");
}
else if (f.Rule == FieldRule.Optional)
{
if (f.ProtoType is ProtoEnum)
{
ProtoEnum pe = f.ProtoType as ProtoEnum;
//the default value is the first value listed in the enum's type definition
foreach (var kvp in pe.Enums)
{
cw.WriteLine("instance." + f.CsName + " = " + pe.FullCsType + "." + kvp.Name + ";");
break;
}
}
}
}
if (method == "DeserializeLengthDelimited")
{
//Important to read stream position after we have read the length field
cw.WriteLine("long limit = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);");
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)
示例2: FieldWriter
/// <summary>
/// Generates code for writing one field
/// </summary>
public void FieldWriter(ProtoMessage m, Field f, CodeWriter cw, Options options)
{
if (f.Rule == FieldRule.Repeated)
{
if (f.OptionPacked == true)
{
//Repeated packed
cw.IfBracket("instance." + f.CsName + " != null");
KeyWriter("stream", f.ID, Wire.LengthDelimited, cw);
if (f.ProtoType.WireSize < 0)
{
//Un-optimized, unknown size
cw.WriteLine("msField.SetLength(0);");
if (f.IsUsingBinaryWriter)
cw.WriteLine("BinaryWriter bw" + f.ID + " = new BinaryWriter(ms" + f.ID + ");");
cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
FieldWriterType(f, "msField", "bw" + f.ID, "i" + f.ID, cw);
cw.EndBracket();
BytesWriter(f, "stream", cw);
}
else
{
//Optimized with known size
//No memorystream buffering, write size first at once
//For constant size messages we can skip serializing to the MemoryStream
cw.WriteLine(ProtocolParser.Base + ".WriteUInt32(stream, " + f.ProtoType.WireSize + "u * (uint)instance." + f.CsName + ".Count);");
cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
FieldWriterType(f, "stream", "bw", "i" + f.ID, cw);
cw.EndBracket();
}
cw.EndBracket();
}
else
{
//Repeated not packet
cw.IfBracket("instance." + f.CsName + " != null");
cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
FieldWriterType(f, "stream", "bw", "i" + f.ID, cw);
cw.EndBracket();
cw.EndBracket();
}
return;
}
else if (f.Rule == FieldRule.Optional)
{
bool skip = options.SkipSerializeDefault && f.OptionDefault != null;
if (options.Nullable ||
f.ProtoType is ProtoMessage ||
f.ProtoType.ProtoName == ProtoBuiltin.String ||
f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
{
if (f.ProtoType.Nullable || options.Nullable) //Struct always exist, not optional
cw.IfBracket("instance." + f.CsName + " != null");
KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
var needValue = !f.ProtoType.Nullable && options.Nullable;
FieldWriterType(f, "stream", "bw", "instance." + f.CsName + (needValue ? ".Value" : ""), cw);
if (f.ProtoType.Nullable || options.Nullable) //Struct always exist, not optional
cw.EndBracket();
return;
}
if (f.ProtoType is ProtoEnum)
{
if (skip)
cw.IfBracket("instance." + f.CsName + " != " + f.FormatDefaultForTypeAssignment());
KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
if (skip)
cw.EndBracket();
return;
}
if (skip) //Skip writing value if default
cw.IfBracket("instance." + f.CsName + " != " + f.FormatDefaultForTypeAssignment());
KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
if (skip)
cw.EndBracket();
return;
}
else if (f.Rule == FieldRule.Required)
{
if (f.ProtoType is ProtoMessage && f.ProtoType.OptionType != "struct" ||
f.ProtoType.ProtoName == ProtoBuiltin.String ||
f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
{
cw.WriteLine("if (instance." + f.CsName + " == null)");
cw.WriteIndent("throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException(\"" + f.CsName + " is required by the proto specification.\");");
}
KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
return;
//.........这里部分代码省略.........
示例3: GenerateWriter
/// <summary>
/// Generates code for writing a class/message
/// </summary>
static void GenerateWriter(ProtoMessage m, CodeWriter cw)
{
cw.Summary("Serialize the instance into the stream");
cw.Bracket(m.OptionAccess + " static void Serialize(Stream stream, " + m.CsType + " instance)");
if (m.OptionTriggers)
{
cw.WriteLine("instance.BeforeSerialize();");
cw.WriteLine();
}
if (m.IsUsingBinaryWriter)
cw.WriteLine("BinaryWriter bw = new BinaryWriter(stream);");
//Shared memorystream for all fields
cw.Using("var msField = new MemoryStream(" + m.MaxFieldBufferSize() + ")");
foreach (Field f in m.Fields.Values)
FieldSerializer.FieldWriter(m, f, cw);
cw.EndBracket();
if (m.OptionPreserveUnknown)
{
cw.IfBracket("instance.PreservedFields != null");
cw.ForeachBracket("var kv in instance.PreservedFields");
cw.WriteLine("global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteKey(stream, kv.Key);");
cw.WriteLine("stream.Write(kv.Value, 0, kv.Value.Length);");
cw.EndBracket();
cw.EndBracket();
}
cw.EndBracket();
cw.WriteLine();
cw.Summary("Helper: Serialize into a MemoryStream and return its byte array");
cw.Bracket(m.OptionAccess + " static byte[] SerializeToBytes(" + m.CsType + " instance)");
cw.Using("var ms = new MemoryStream()");
cw.WriteLine("Serialize(ms, instance);");
cw.WriteLine("return ms.ToArray();");
cw.EndBracket();
cw.EndBracket();
cw.Summary("Helper: Serialize with a varint length prefix");
cw.Bracket(m.OptionAccess + " static void SerializeLengthDelimited(Stream stream, " + m.CsType + " instance)");
cw.WriteLine("var data = SerializeToBytes(instance);");
cw.WriteLine("global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length);");
cw.WriteLine("stream.Write(data, 0, data.Length);");
cw.EndBracket();
}
示例4: FieldSizeWriter
public void FieldSizeWriter(ProtoMessage m, Field f, CodeWriter cw, Options options)
{
if (f.Rule == FieldRule.Repeated)
{
if (f.OptionPacked == true)
{
Console.WriteLine("not support");
}
else
{
//Repeated not packet
cw.IfBracket("this." + f.CsName + " != null");
cw.ForeachBracket("var i" + f.ID + " in this." + f.CsName);
//KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
FieldSizeWriterType(f, "i" + f.ID, cw);
cw.EndBracket();
cw.EndBracket();
}
return;
}
else if (f.Rule == FieldRule.Optional)
{
if (options.Nullable ||
f.ProtoType is ProtoMessage ||
f.ProtoType.ProtoName == ProtoBuiltin.String ||
f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
{
if (f.ProtoType.Nullable || options.Nullable) //Struct always exist, not optional
cw.IfBracket("this." + f.CsName + " != null");
FieldSizeWriterType(f, "this." + f.CsName, cw);
if (f.ProtoType.Nullable || options.Nullable) //Struct always exist, not optional
cw.EndBracket();
return;
}
if (f.ProtoType is ProtoEnum)
{
if (f.OptionDefault != null)
cw.IfBracket("this." + f.CsName + " != " + f.ProtoType.CsType + "." + f.OptionDefault);
FieldSizeWriterType(f, "this." + f.CsName, cw);
if (f.OptionDefault != null)
cw.EndBracket();
return;
}
KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
return;
}
else if (f.Rule == FieldRule.Required)
{
if (f.ProtoType is ProtoMessage && f.ProtoType.OptionType != "struct" ||
f.ProtoType.ProtoName == ProtoBuiltin.String ||
f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
{
cw.WriteLine("if (this." + f.CsName + " == null)");
cw.WriteIndent("throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException(\"" + f.CsName + " is required by the proto specification.\");");
}
cw.WriteLine();
FieldSizeWriterType(f, "this." + f.CsName, cw);
return;
}
throw new NotImplementedException("Unknown rule: " + f.Rule);
}