diff options
author | Jon Skeet <skeet@pobox.com> | 2015-06-19 17:30:13 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2015-06-19 17:30:13 +0100 |
commit | cdeda4b87625084f5687115bb1fd7772b7c34ad6 (patch) | |
tree | 373f4c38dbfb318626037ff204c30466574ab876 /csharp/src/ProtocolBuffers | |
parent | d7dda2fed8c37a83e2d4cd7ecc4201b628588c4c (diff) | |
download | protobuf-cdeda4b87625084f5687115bb1fd7772b7c34ad6.tar.gz protobuf-cdeda4b87625084f5687115bb1fd7772b7c34ad6.tar.bz2 protobuf-cdeda4b87625084f5687115bb1fd7772b7c34ad6.zip |
Minor cleanup.
- Make some members internal
- Remove a lot of FrameworkPortability that isn't required
- Start adding documentation comments
- Remove some more group-based members
- Not passing in "the last tag read" into Read*Array, g
Diffstat (limited to 'csharp/src/ProtocolBuffers')
-rw-r--r-- | csharp/src/ProtocolBuffers/ByteArray.cs | 9 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/CodedInputStream.cs | 90 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/CodedOutputStream.cs | 5 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs | 60 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/FrameworkPortability.cs | 68 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/IMessage.cs | 43 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/MessageExtensions.cs (renamed from csharp/src/ProtocolBuffers/Extensions.cs) | 25 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/MessageParser.cs | 37 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/ProtocolBuffers.csproj | 4 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/ThrowHelper.cs | 17 |
10 files changed, 180 insertions, 178 deletions
diff --git a/csharp/src/ProtocolBuffers/ByteArray.cs b/csharp/src/ProtocolBuffers/ByteArray.cs index d367fc39..211a0e11 100644 --- a/csharp/src/ProtocolBuffers/ByteArray.cs +++ b/csharp/src/ProtocolBuffers/ByteArray.cs @@ -51,7 +51,7 @@ namespace Google.Protobuf /// <summary>
/// Determines which copy routine to use based on the number of bytes to be copied.
/// </summary>
- public static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
+ internal static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
{
if (count > CopyThreshold)
{
@@ -66,7 +66,7 @@ namespace Google.Protobuf /// <summary>
/// Copy the bytes provided with a for loop, faster when there are only a few bytes to copy
/// </summary>
- public static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
+ internal static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
{
int stop = srcOffset + count;
for (int i = srcOffset; i < stop; i++)
@@ -78,12 +78,11 @@ namespace Google.Protobuf /// <summary>
/// Reverses the order of bytes in the array
/// </summary>
- public static void Reverse(byte[] bytes)
+ internal static void Reverse(byte[] bytes)
{
- byte temp;
for (int first = 0, last = bytes.Length - 1; first < last; first++, last--)
{
- temp = bytes[first];
+ byte temp = bytes[first];
bytes[first] = bytes[last];
bytes[last] = temp;
}
diff --git a/csharp/src/ProtocolBuffers/CodedInputStream.cs b/csharp/src/ProtocolBuffers/CodedInputStream.cs index 56283318..905cdb9d 100644 --- a/csharp/src/ProtocolBuffers/CodedInputStream.cs +++ b/csharp/src/ProtocolBuffers/CodedInputStream.cs @@ -37,9 +37,7 @@ using System;
using System.Collections.Generic;
using System.IO;
-using System.Text;
using Google.Protobuf.Collections;
-using Google.Protobuf.Descriptors;
namespace Google.Protobuf
{
@@ -183,7 +181,7 @@ namespace Google.Protobuf /// </summary>
/// <exception cref="InvalidProtocolBufferException">The last
/// tag read was not the one specified</exception>
- public void CheckLastTagWas(uint value)
+ internal void CheckLastTagWas(uint value)
{
if (lastTag != value)
{
@@ -251,7 +249,7 @@ namespace Google.Protobuf /// </summary>
public double ReadDouble()
{
- return FrameworkPortability.Int64ToDouble((long) ReadRawLittleEndian64());
+ return BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64());
}
/// <summary>
@@ -348,21 +346,6 @@ namespace Google.Protobuf }
/// <summary>
- /// Reads a group field value from the stream.
- /// </summary>
- public void ReadGroup(int fieldNumber, IMessage message)
- {
- if (recursionDepth >= recursionLimit)
- {
- throw InvalidProtocolBufferException.RecursionLimitExceeded();
- }
- ++recursionDepth;
- message.MergeFrom(this);
- CheckLastTagWas(WireFormat.MakeTag(fieldNumber, WireFormat.WireType.EndGroup));
- --recursionDepth;
- }
-
- /// <summary>
/// Reads an embedded message field value from the stream.
/// </summary>
public void ReadMessage(IMessage builder)
@@ -516,24 +499,34 @@ namespace Google.Protobuf return false;
}
- public void ReadStringArray(uint fieldTag, ICollection<string> list)
+ /// <summary>
+ /// Reads a string array.
+ /// </summary>
+ /// <remarks>The stream is assumed to be positioned after a tag indicating the field
+ /// repeated string value. A string is read, and then if the next tag is the same,
+ /// the process is repeated, until the next tag is a different one.</remarks>
+ /// <param name="list"></param>
+ public void ReadStringArray(ICollection<string> list)
{
+ uint fieldTag = lastTag;
do
{
list.Add(ReadString());
} while (ContinueArray(fieldTag));
}
- public void ReadBytesArray(uint fieldTag, ICollection<ByteString> list)
+ public void ReadBytesArray(ICollection<ByteString> list)
{
+ uint fieldTag = lastTag;
do
{
list.Add(ReadBytes());
} while (ContinueArray(fieldTag));
}
- public void ReadBoolArray(uint fieldTag, ICollection<bool> list)
+ public void ReadBoolArray(ICollection<bool> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -545,8 +538,9 @@ namespace Google.Protobuf }
}
- public void ReadInt32Array(uint fieldTag, ICollection<int> list)
+ public void ReadInt32Array(ICollection<int> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -558,8 +552,9 @@ namespace Google.Protobuf }
}
- public void ReadSInt32Array(uint fieldTag, ICollection<int> list)
+ public void ReadSInt32Array(ICollection<int> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -571,8 +566,9 @@ namespace Google.Protobuf }
}
- public void ReadUInt32Array(uint fieldTag, ICollection<uint> list)
+ public void ReadUInt32Array(ICollection<uint> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -584,8 +580,9 @@ namespace Google.Protobuf }
}
- public void ReadFixed32Array(uint fieldTag, ICollection<uint> list)
+ public void ReadFixed32Array(ICollection<uint> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -597,8 +594,9 @@ namespace Google.Protobuf }
}
- public void ReadSFixed32Array(uint fieldTag, ICollection<int> list)
+ public void ReadSFixed32Array(ICollection<int> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -610,8 +608,9 @@ namespace Google.Protobuf }
}
- public void ReadInt64Array(uint fieldTag, ICollection<long> list)
+ public void ReadInt64Array(ICollection<long> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -623,8 +622,9 @@ namespace Google.Protobuf }
}
- public void ReadSInt64Array(uint fieldTag, ICollection<long> list)
+ public void ReadSInt64Array(ICollection<long> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -636,8 +636,9 @@ namespace Google.Protobuf }
}
- public void ReadUInt64Array(uint fieldTag, ICollection<ulong> list)
+ public void ReadUInt64Array(ICollection<ulong> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -649,8 +650,9 @@ namespace Google.Protobuf }
}
- public void ReadFixed64Array(uint fieldTag, ICollection<ulong> list)
+ public void ReadFixed64Array(ICollection<ulong> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -662,8 +664,9 @@ namespace Google.Protobuf }
}
- public void ReadSFixed64Array(uint fieldTag, ICollection<long> list)
+ public void ReadSFixed64Array(ICollection<long> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -675,8 +678,9 @@ namespace Google.Protobuf }
}
- public void ReadDoubleArray(uint fieldTag, ICollection<double> list)
+ public void ReadDoubleArray(ICollection<double> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -688,8 +692,9 @@ namespace Google.Protobuf }
}
- public void ReadFloatArray(uint fieldTag, ICollection<float> list)
+ public void ReadFloatArray(ICollection<float> list)
{
+ uint fieldTag = lastTag;
bool isPacked;
int holdLimit;
if (BeginArray(fieldTag, out isPacked, out holdLimit))
@@ -701,9 +706,10 @@ namespace Google.Protobuf }
}
- public void ReadEnumArray<T>(uint fieldTag, RepeatedField<T> list)
+ public void ReadEnumArray<T>(RepeatedField<T> list)
where T : struct, IComparable, IFormattable
{
+ uint fieldTag = lastTag;
WireFormat.WireType wformat = WireFormat.GetTagWireType(fieldTag);
// 2.3 allows packed form even if the field is not declared packed.
@@ -727,9 +733,10 @@ namespace Google.Protobuf }
}
- public void ReadMessageArray<T>(uint fieldTag, ICollection<T> list, MessageParser<T> messageParser)
+ public void ReadMessageArray<T>(ICollection<T> list, MessageParser<T> messageParser)
where T : IMessage<T>
{
+ uint fieldTag = lastTag;
do
{
T message = messageParser.CreateTemplate();
@@ -737,17 +744,6 @@ namespace Google.Protobuf list.Add(message);
} while (ContinueArray(fieldTag));
}
-
- public void ReadGroupArray<T>(uint fieldTag, ICollection<T> list, MessageParser<T> messageParser)
- where T : IMessage<T>
- {
- do
- {
- T message = messageParser.CreateTemplate();
- ReadGroup(WireFormat.GetTagFieldNumber(fieldTag), message);
- list.Add(message);
- } while (ContinueArray(fieldTag));
- }
#endregion
#region Underlying reading primitives
diff --git a/csharp/src/ProtocolBuffers/CodedOutputStream.cs b/csharp/src/ProtocolBuffers/CodedOutputStream.cs index 1e6e7e55..e56ce789 100644 --- a/csharp/src/ProtocolBuffers/CodedOutputStream.cs +++ b/csharp/src/ProtocolBuffers/CodedOutputStream.cs @@ -35,12 +35,9 @@ #endregion
using System;
-using System.Collections;
using System.IO;
-using System.Linq;
using System.Text;
using Google.Protobuf.Collections;
-using Google.Protobuf.Descriptors;
namespace Google.Protobuf
{
@@ -151,7 +148,7 @@ namespace Google.Protobuf /// </summary>
public void WriteDouble(double value)
{
- WriteRawLittleEndian64((ulong)FrameworkPortability.DoubleToInt64(value));
+ WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
}
/// <summary>
diff --git a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs index 67938260..7b7abd8e 100644 --- a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs +++ b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs @@ -358,7 +358,7 @@ namespace Google.Protobuf.DescriptorProtos { } break; case 10: { - input.ReadMessageArray(tag, file_, global::Google.Protobuf.DescriptorProtos.FileDescriptorProto.Parser); + input.ReadMessageArray(file_, global::Google.Protobuf.DescriptorProtos.FileDescriptorProto.Parser); break; } } @@ -672,23 +672,23 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 26: { - input.ReadStringArray(tag, dependency_); + input.ReadStringArray(dependency_); break; } case 34: { - input.ReadMessageArray(tag, messageType_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Parser); + input.ReadMessageArray(messageType_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Parser); break; } case 42: { - input.ReadMessageArray(tag, enumType_, global::Google.Protobuf.DescriptorProtos.EnumDescriptorProto.Parser); + input.ReadMessageArray(enumType_, global::Google.Protobuf.DescriptorProtos.EnumDescriptorProto.Parser); break; } case 50: { - input.ReadMessageArray(tag, service_, global::Google.Protobuf.DescriptorProtos.ServiceDescriptorProto.Parser); + input.ReadMessageArray(service_, global::Google.Protobuf.DescriptorProtos.ServiceDescriptorProto.Parser); break; } case 58: { - input.ReadMessageArray(tag, extension_, global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto.Parser); + input.ReadMessageArray(extension_, global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto.Parser); break; } case 66: { @@ -707,12 +707,12 @@ namespace Google.Protobuf.DescriptorProtos { } case 82: case 80: { - input.ReadInt32Array(tag, publicDependency_); + input.ReadInt32Array(publicDependency_); break; } case 90: case 88: { - input.ReadInt32Array(tag, weakDependency_); + input.ReadInt32Array(weakDependency_); break; } case 98: { @@ -979,23 +979,23 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 18: { - input.ReadMessageArray(tag, field_, global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto.Parser); + input.ReadMessageArray(field_, global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto.Parser); break; } case 26: { - input.ReadMessageArray(tag, nestedType_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Parser); + input.ReadMessageArray(nestedType_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Parser); break; } case 34: { - input.ReadMessageArray(tag, enumType_, global::Google.Protobuf.DescriptorProtos.EnumDescriptorProto.Parser); + input.ReadMessageArray(enumType_, global::Google.Protobuf.DescriptorProtos.EnumDescriptorProto.Parser); break; } case 42: { - input.ReadMessageArray(tag, extensionRange_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ExtensionRange.Parser); + input.ReadMessageArray(extensionRange_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ExtensionRange.Parser); break; } case 50: { - input.ReadMessageArray(tag, extension_, global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto.Parser); + input.ReadMessageArray(extension_, global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto.Parser); break; } case 58: { @@ -1006,15 +1006,15 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 66: { - input.ReadMessageArray(tag, oneofDecl_, global::Google.Protobuf.DescriptorProtos.OneofDescriptorProto.Parser); + input.ReadMessageArray(oneofDecl_, global::Google.Protobuf.DescriptorProtos.OneofDescriptorProto.Parser); break; } case 74: { - input.ReadMessageArray(tag, reservedRange_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ReservedRange.Parser); + input.ReadMessageArray(reservedRange_, global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ReservedRange.Parser); break; } case 82: { - input.ReadStringArray(tag, reservedName_); + input.ReadStringArray(reservedName_); break; } } @@ -1800,7 +1800,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 18: { - input.ReadMessageArray(tag, value_, global::Google.Protobuf.DescriptorProtos.EnumValueDescriptorProto.Parser); + input.ReadMessageArray(value_, global::Google.Protobuf.DescriptorProtos.EnumValueDescriptorProto.Parser); break; } case 26: { @@ -2086,7 +2086,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 18: { - input.ReadMessageArray(tag, method_, global::Google.Protobuf.DescriptorProtos.MethodDescriptorProto.Parser); + input.ReadMessageArray(method_, global::Google.Protobuf.DescriptorProtos.MethodDescriptorProto.Parser); break; } case 26: { @@ -2735,7 +2735,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -2933,7 +2933,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -3167,7 +3167,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -3323,7 +3323,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -3437,7 +3437,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -3551,7 +3551,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -3665,7 +3665,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 7994: { - input.ReadMessageArray(tag, uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); + input.ReadMessageArray(uninterpretedOption_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Parser); break; } } @@ -3875,7 +3875,7 @@ namespace Google.Protobuf.DescriptorProtos { } break; case 18: { - input.ReadMessageArray(tag, name_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Types.NamePart.Parser); + input.ReadMessageArray(name_, global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Types.NamePart.Parser); break; } case 26: { @@ -4111,7 +4111,7 @@ namespace Google.Protobuf.DescriptorProtos { } break; case 10: { - input.ReadMessageArray(tag, location_, global::Google.Protobuf.DescriptorProtos.SourceCodeInfo.Types.Location.Parser); + input.ReadMessageArray(location_, global::Google.Protobuf.DescriptorProtos.SourceCodeInfo.Types.Location.Parser); break; } } @@ -4287,12 +4287,12 @@ namespace Google.Protobuf.DescriptorProtos { break; case 10: case 8: { - input.ReadInt32Array(tag, path_); + input.ReadInt32Array(path_); break; } case 18: case 16: { - input.ReadInt32Array(tag, span_); + input.ReadInt32Array(span_); break; } case 26: { @@ -4304,7 +4304,7 @@ namespace Google.Protobuf.DescriptorProtos { break; } case 50: { - input.ReadStringArray(tag, leadingDetachedComments_); + input.ReadStringArray(leadingDetachedComments_); break; } } diff --git a/csharp/src/ProtocolBuffers/FrameworkPortability.cs b/csharp/src/ProtocolBuffers/FrameworkPortability.cs index 5fa7c4e7..06246a9e 100644 --- a/csharp/src/ProtocolBuffers/FrameworkPortability.cs +++ b/csharp/src/ProtocolBuffers/FrameworkPortability.cs @@ -35,8 +35,6 @@ #endregion
using System;
-using System.Globalization;
-using System.Reflection;
using System.Text.RegularExpressions;
namespace Google.Protobuf
@@ -46,66 +44,10 @@ namespace Google.Protobuf /// </summary>
internal static class FrameworkPortability
{
-#if COMPACT_FRAMEWORK
- internal const string NewLine = "\n";
-#else
- internal static readonly string NewLine = System.Environment.NewLine;
-#endif
-
-#if CLIENTPROFILE
- internal const RegexOptions CompiledRegexWhereAvailable = RegexOptions.Compiled;
-#else
- internal const RegexOptions CompiledRegexWhereAvailable = RegexOptions.None;
-#endif
-
- internal static CultureInfo InvariantCulture
- {
- get { return CultureInfo.InvariantCulture; }
- }
-
- internal static double Int64ToDouble(long value)
- {
-#if CLIENTPROFILE
- return BitConverter.Int64BitsToDouble(value);
-#else
- double[] arresult = new double[1];
- Buffer.BlockCopy(new[] { value }, 0, arresult, 0, 8);
- return arresult[0];
-#endif
- }
-
- internal static long DoubleToInt64(double value)
- {
-#if CLIENTPROFILE
- return BitConverter.DoubleToInt64Bits(value);
-#else
- long[] arresult = new long[1];
- Buffer.BlockCopy(new[] { value }, 0, arresult, 0, 8);
- return arresult[0];
-#endif
- }
-
- internal static bool TryParseInt32(string text, out int number)
- {
- return TryParseInt32(text, NumberStyles.Any, InvariantCulture, out number);
- }
-
- internal static bool TryParseInt32(string text, NumberStyles style, IFormatProvider format, out int number)
- {
-#if COMPACT_FRAMEWORK
- try
- {
- number = int.Parse(text, style, format);
- return true;
- }
- catch
- {
- number = 0;
- return false;
- }
-#else
- return int.TryParse(text, style, format, out number);
-#endif
- }
+ // The value of RegexOptions.Compiled is 8. We can test for the presence at
+ // execution time using Enum.IsDefined, so a single build will do the right thing
+ // on each platform.
+ internal static readonly RegexOptions CompiledRegexWhereAvailable =
+ Enum.IsDefined(typeof(RegexOptions), 8) ? (RegexOptions)8 : RegexOptions.None;
}
}
\ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/IMessage.cs b/csharp/src/ProtocolBuffers/IMessage.cs index 55b6fc5d..9c2a2d85 100644 --- a/csharp/src/ProtocolBuffers/IMessage.cs +++ b/csharp/src/ProtocolBuffers/IMessage.cs @@ -34,30 +34,63 @@ #endregion
-using System;
-using System.Collections.Generic;
-using System.IO;
-using Google.Protobuf.Descriptors;
using Google.Protobuf.FieldAccess;
namespace Google.Protobuf
{
- // TODO(jonskeet): Do we want a "weak" version of IReflectedMessage?
+ // TODO(jonskeet): Do we want a "weak" (non-generic) version of IReflectedMessage?
+
+ /// <summary>
+ /// Reflection support for a specific message type. message
+ /// </summary>
+ /// <typeparam name="T">The message type being reflected.</typeparam>
public interface IReflectedMessage<T> where T : IMessage<T>
{
FieldAccessorTable<T> Fields { get; }
+ // TODO(jonskeet): Descriptor? Or a single property which has "all you need for reflection"?
}
+ /// <summary>
+ /// Interface for a Protocol Buffers message, supporting
+ /// basic operations required for serialization.
+ /// </summary>
public interface IMessage
{
+ /// <summary>
+ /// Merges the data from the specified coded input stream with the current message.
+ /// </summary>
+ /// <remarks>See the user guide for precise merge semantics.</remarks>
+ /// <param name="input"></param>
void MergeFrom(CodedInputStream input);
+
+ /// <summary>
+ /// Writes the data to the given coded output stream.
+ /// </summary>
+ /// <param name="output">Coded output stream to write the data to. Must not be null.</param>
void WriteTo(CodedOutputStream output);
+
+ /// <summary>
+ /// Calculates the size of this message in Protocol Buffer wire format, in bytes.
+ /// </summary>
+ /// <returns>The number of bytes required to write this message
+ /// to a coded output stream.</returns>
int CalculateSize();
}
+ /// <summary>
+ /// Generic interface for a Protocol Buffers message,
+ /// where the type parameter is expected to be the same type as
+ /// the implementation class.
+ /// </summary>
+ /// <typeparam name="T">The message type.</typeparam>
public interface IMessage<T> : IMessage where T : IMessage<T>
{
+ /// <summary>
+ /// Merges the given message into this one.
+ /// </summary>
+ /// <remarks>See the user guide for precise merge semantics.</remarks>
+ /// <param name="message">The message to merge with this one. Must not be null.</param>
void MergeFrom(T message);
}
}
\ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/Extensions.cs b/csharp/src/ProtocolBuffers/MessageExtensions.cs index 7f23057e..57cecfd4 100644 --- a/csharp/src/ProtocolBuffers/Extensions.cs +++ b/csharp/src/ProtocolBuffers/MessageExtensions.cs @@ -2,11 +2,15 @@ namespace Google.Protobuf { - // TODO: MessageExtensions? - public static class Extensions + /// <summary> + /// Extension methods on <see cref="IMessage"/> and <see cref="IMessage{T}"/>. + /// </summary> + public static class MessageExtensions { public static void MergeFrom(this IMessage message, byte[] data) { + ThrowHelper.ThrowIfNull(message, "message"); + ThrowHelper.ThrowIfNull(data, "data"); CodedInputStream input = CodedInputStream.CreateInstance(data); message.MergeFrom(input); input.CheckLastTagWas(0); @@ -14,6 +18,8 @@ namespace Google.Protobuf public static void MergeFrom(this IMessage message, ByteString data) { + ThrowHelper.ThrowIfNull(message, "message"); + ThrowHelper.ThrowIfNull(data, "data"); CodedInputStream input = data.CreateCodedInput(); message.MergeFrom(input); input.CheckLastTagWas(0); @@ -21,6 +27,8 @@ namespace Google.Protobuf public static void MergeFrom(this IMessage message, Stream input) { + ThrowHelper.ThrowIfNull(message, "message"); + ThrowHelper.ThrowIfNull(input, "input"); CodedInputStream codedInput = CodedInputStream.CreateInstance(input); message.MergeFrom(codedInput); codedInput.CheckLastTagWas(0); @@ -28,6 +36,8 @@ namespace Google.Protobuf public static void MergeDelimitedFrom(this IMessage message, Stream input) { + ThrowHelper.ThrowIfNull(message, "message"); + ThrowHelper.ThrowIfNull(input, "input"); int size = (int)CodedInputStream.ReadRawVarint32(input); Stream limitedStream = new LimitedInputStream(input, size); message.MergeFrom(limitedStream); @@ -35,6 +45,7 @@ namespace Google.Protobuf public static byte[] ToByteArray(this IMessage message) { + ThrowHelper.ThrowIfNull(message, "message"); byte[] result = new byte[message.CalculateSize()]; CodedOutputStream output = CodedOutputStream.CreateInstance(result); message.WriteTo(output); @@ -44,18 +55,17 @@ namespace Google.Protobuf public static void WriteTo(this IMessage message, Stream output) { + ThrowHelper.ThrowIfNull(message, "message"); + ThrowHelper.ThrowIfNull(output, "output"); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); message.WriteTo(codedOutput); codedOutput.Flush(); } - public static void WriteTo(this IMessage message, CodedOutputStream output) - { - message.WriteTo(output); - } - public static void WriteDelimitedTo(this IMessage message, Stream output) { + ThrowHelper.ThrowIfNull(message, "message"); + ThrowHelper.ThrowIfNull(output, "output"); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); codedOutput.WriteRawVarint32((uint)message.CalculateSize()); message.WriteTo(codedOutput); @@ -64,6 +74,7 @@ namespace Google.Protobuf public static ByteString ToByteString(this IMessage message) { + ThrowHelper.ThrowIfNull(message, "message"); return ByteString.AttachBytes(message.ToByteArray()); } } diff --git a/csharp/src/ProtocolBuffers/MessageParser.cs b/csharp/src/ProtocolBuffers/MessageParser.cs index 722435cc..18cda2dc 100644 --- a/csharp/src/ProtocolBuffers/MessageParser.cs +++ b/csharp/src/ProtocolBuffers/MessageParser.cs @@ -1,26 +1,58 @@ using System; using System.IO; -using Google.Protobuf; namespace Google.Protobuf { + /// <summary> + /// A parser for a specific message type. + /// </summary> + /// <remarks> + /// <p> + /// This delegates most behavior to the + /// <see cref="IMessage.MergeFrom"/> implementation within the original type, but + /// provides convenient overloads to parse from a variety of sources. + /// </p> + /// <p> + /// Most applications will never need to create their own instances of this type; + /// instead, use the static <c>Parser</c> property of a generated message type to obtain a + /// parser for that type. + /// </p> + /// </remarks> + /// <typeparam name="T">The type of message to be parsed.</typeparam> public sealed class MessageParser<T> where T : IMessage<T> { private readonly Func<T> factory; + /// <summary> + /// Creates a new parser. + /// </summary> + /// <remarks> + /// The factory method is effectively an optimization over using a generic constraint + /// to require a parameterless constructor: delegates are significantly faster to execute. + /// </remarks> + /// <param name="factory">Function to invoke when a new, empty message is required.</param> public MessageParser(Func<T> factory) { this.factory = factory; } - // Creates a template instance ready for population. + /// <summary> + /// Creates a template instance ready for population. + /// </summary> + /// <returns>An empty message.</returns> internal T CreateTemplate() { return factory(); } + /// <summary> + /// Parses a message from a byte array. + /// </summary> + /// <param name="data">The byte array containing the message. Must not be null.</param> + /// <returns>The newly parsed message.</returns> public T ParseFrom(byte[] data) { + ThrowHelper.ThrowIfNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; @@ -28,6 +60,7 @@ namespace Google.Protobuf public T ParseFrom(ByteString data) { + ThrowHelper.ThrowIfNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj index 3a07e87f..33a32ff2 100644 --- a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj @@ -84,7 +84,8 @@ <Compile Include="Descriptors\MethodDescriptor.cs" />
<Compile Include="Descriptors\PackageDescriptor.cs" />
<Compile Include="Descriptors\ServiceDescriptor.cs" />
- <Compile Include="Extensions.cs" />
+ <Compile Include="FrameworkPortability.cs" />
+ <Compile Include="MessageExtensions.cs" />
<Compile Include="FieldAccess\FieldAccessorBase.cs" />
<Compile Include="FieldAccess\ReflectionUtil.cs" />
<Compile Include="FieldAccess\RepeatedFieldAccessor.cs" />
@@ -92,7 +93,6 @@ <Compile Include="FieldAccess\IFieldAccessor.cs" />
<Compile Include="FieldAccess\FieldAccessorTable.cs" />
<Compile Include="FieldAccess\OneofAccessor.cs" />
- <Compile Include="FrameworkPortability.cs" />
<Compile Include="IMessage.cs" />
<Compile Include="InvalidProtocolBufferException.cs" />
<Compile Include="LimitedInputStream.cs" />
diff --git a/csharp/src/ProtocolBuffers/ThrowHelper.cs b/csharp/src/ProtocolBuffers/ThrowHelper.cs index 097b5032..c12a48a3 100644 --- a/csharp/src/ProtocolBuffers/ThrowHelper.cs +++ b/csharp/src/ProtocolBuffers/ThrowHelper.cs @@ -42,12 +42,12 @@ namespace Google.Protobuf /// <summary>
/// Helper methods for throwing exceptions
/// </summary>
- public static class ThrowHelper
+ internal static class ThrowHelper
{
/// <summary>
/// Throws an ArgumentNullException if the given value is null.
/// </summary>
- public static void ThrowIfNull(object value, string name)
+ internal static void ThrowIfNull(object value, string name)
{
if (value == null)
{
@@ -58,7 +58,7 @@ namespace Google.Protobuf /// <summary>
/// Throws an ArgumentNullException if the given value is null.
/// </summary>
- public static void ThrowIfNull(object value)
+ internal static void ThrowIfNull(object value)
{
if (value == null)
{
@@ -69,7 +69,7 @@ namespace Google.Protobuf /// <summary>
/// Throws an ArgumentNullException if the given value or any element within it is null.
/// </summary>
- public static void ThrowIfAnyNull<T>(IEnumerable<T> sequence)
+ internal static void ThrowIfAnyNull<T>(IEnumerable<T> sequence)
{
foreach (T t in sequence)
{
@@ -79,14 +79,5 @@ namespace Google.Protobuf }
}
}
-
- public static Exception CreateMissingMethod(Type type, string methodName)
- {
-#if CLIENTPROFILE
- return new System.MissingMethodException(type.FullName, methodName);
-#else
- return new System.ArgumentException(String.Format("The method '{0}' was not found on type {1}.", methodName, type));
-#endif
- }
}
}
\ No newline at end of file |