Commit 68380f0f authored by Jon Skeet's avatar Jon Skeet

Rename ThrowHelper to Preconditions and make it public - we'll want to use it…

Rename ThrowHelper to Preconditions and make it public - we'll want to use it from the generated code soon.

Additionally, change it to return the value passed, and make it generic with a class constraint.
A separate method doesn't have the class constraint, for more unusual scenarios.
parent 7909b2ed
...@@ -107,6 +107,7 @@ csharp_EXTRA_DIST= \ ...@@ -107,6 +107,7 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf/LimitedInputStream.cs \ csharp/src/Google.Protobuf/LimitedInputStream.cs \
csharp/src/Google.Protobuf/MessageExtensions.cs \ csharp/src/Google.Protobuf/MessageExtensions.cs \
csharp/src/Google.Protobuf/MessageParser.cs \ csharp/src/Google.Protobuf/MessageParser.cs \
csharp/src/Google.Protobuf/Preconditions.cs \
csharp/src/Google.Protobuf/Properties/AssemblyInfo.cs \ csharp/src/Google.Protobuf/Properties/AssemblyInfo.cs \
csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs \
csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs \
...@@ -133,7 +134,6 @@ csharp_EXTRA_DIST= \ ...@@ -133,7 +134,6 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs \ csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs \
csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs \ csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs \
csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs \ csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs \
csharp/src/Google.Protobuf/ThrowHelper.cs \
csharp/src/Google.Protobuf/WellKnownTypes/Any.cs \ csharp/src/Google.Protobuf/WellKnownTypes/Any.cs \
csharp/src/Google.Protobuf/WellKnownTypes/Api.cs \ csharp/src/Google.Protobuf/WellKnownTypes/Api.cs \
csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs \ csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs \
......
...@@ -112,13 +112,13 @@ namespace Google.Protobuf.Collections ...@@ -112,13 +112,13 @@ namespace Google.Protobuf.Collections
public bool ContainsKey(TKey key) public bool ContainsKey(TKey key)
{ {
ThrowHelper.ThrowIfNull(key, "key"); Preconditions.CheckNotNullUnconstrained(key, "key");
return map.ContainsKey(key); return map.ContainsKey(key);
} }
public bool Remove(TKey key) public bool Remove(TKey key)
{ {
ThrowHelper.ThrowIfNull(key, "key"); Preconditions.CheckNotNullUnconstrained(key, "key");
LinkedListNode<KeyValuePair<TKey, TValue>> node; LinkedListNode<KeyValuePair<TKey, TValue>> node;
if (map.TryGetValue(key, out node)) if (map.TryGetValue(key, out node))
{ {
...@@ -151,7 +151,7 @@ namespace Google.Protobuf.Collections ...@@ -151,7 +151,7 @@ namespace Google.Protobuf.Collections
{ {
get get
{ {
ThrowHelper.ThrowIfNull(key, "key"); Preconditions.CheckNotNullUnconstrained(key, "key");
TValue value; TValue value;
if (TryGetValue(key, out value)) if (TryGetValue(key, out value))
{ {
...@@ -161,11 +161,11 @@ namespace Google.Protobuf.Collections ...@@ -161,11 +161,11 @@ namespace Google.Protobuf.Collections
} }
set set
{ {
ThrowHelper.ThrowIfNull(key, "key"); Preconditions.CheckNotNullUnconstrained(key, "key");
// value == null check here is redundant, but avoids boxing. // value == null check here is redundant, but avoids boxing.
if (value == null && !allowNullValues) if (value == null && !allowNullValues)
{ {
ThrowHelper.ThrowIfNull(value, "value"); Preconditions.CheckNotNullUnconstrained(value, "value");
} }
LinkedListNode<KeyValuePair<TKey, TValue>> node; LinkedListNode<KeyValuePair<TKey, TValue>> node;
var pair = new KeyValuePair<TKey, TValue>(key, value); var pair = new KeyValuePair<TKey, TValue>(key, value);
...@@ -187,7 +187,7 @@ namespace Google.Protobuf.Collections ...@@ -187,7 +187,7 @@ namespace Google.Protobuf.Collections
public void Add(IDictionary<TKey, TValue> entries) public void Add(IDictionary<TKey, TValue> entries)
{ {
ThrowHelper.ThrowIfNull(entries, "entries"); Preconditions.CheckNotNull(entries, "entries");
foreach (var pair in entries) foreach (var pair in entries)
{ {
Add(pair.Key, pair.Value); Add(pair.Key, pair.Value);
...@@ -374,7 +374,7 @@ namespace Google.Protobuf.Collections ...@@ -374,7 +374,7 @@ namespace Google.Protobuf.Collections
void IDictionary.Remove(object key) void IDictionary.Remove(object key)
{ {
ThrowHelper.ThrowIfNull(key, "key"); Preconditions.CheckNotNull(key, "key");
if (!(key is TKey)) if (!(key is TKey))
{ {
return; return;
...@@ -403,7 +403,7 @@ namespace Google.Protobuf.Collections ...@@ -403,7 +403,7 @@ namespace Google.Protobuf.Collections
{ {
get get
{ {
ThrowHelper.ThrowIfNull(key, "key"); Preconditions.CheckNotNull(key, "key");
if (!(key is TKey)) if (!(key is TKey))
{ {
return null; return null;
......
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
<Compile Include="Reflection\RepeatedFieldAccessor.cs" /> <Compile Include="Reflection\RepeatedFieldAccessor.cs" />
<Compile Include="Reflection\ServiceDescriptor.cs" /> <Compile Include="Reflection\ServiceDescriptor.cs" />
<Compile Include="Reflection\SingleFieldAccessor.cs" /> <Compile Include="Reflection\SingleFieldAccessor.cs" />
<Compile Include="ThrowHelper.cs" /> <Compile Include="Preconditions.cs" />
<Compile Include="WellKnownTypes\Any.cs" /> <Compile Include="WellKnownTypes\Any.cs" />
<Compile Include="WellKnownTypes\Api.cs" /> <Compile Include="WellKnownTypes\Api.cs" />
<Compile Include="WellKnownTypes\Duration.cs" /> <Compile Include="WellKnownTypes\Duration.cs" />
......
...@@ -120,7 +120,7 @@ namespace Google.Protobuf ...@@ -120,7 +120,7 @@ namespace Google.Protobuf
public string Format(IMessage message) public string Format(IMessage message)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
// TODO(jonskeet): Handle well-known types here. // TODO(jonskeet): Handle well-known types here.
// Our reflection support needs improving so that we can get at the descriptor // Our reflection support needs improving so that we can get at the descriptor
......
...@@ -41,8 +41,8 @@ namespace Google.Protobuf ...@@ -41,8 +41,8 @@ namespace Google.Protobuf
{ {
public static void MergeFrom(this IMessage message, byte[] data) public static void MergeFrom(this IMessage message, byte[] data)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
ThrowHelper.ThrowIfNull(data, "data"); Preconditions.CheckNotNull(data, "data");
CodedInputStream input = CodedInputStream.CreateInstance(data); CodedInputStream input = CodedInputStream.CreateInstance(data);
message.MergeFrom(input); message.MergeFrom(input);
input.CheckLastTagWas(0); input.CheckLastTagWas(0);
...@@ -50,8 +50,8 @@ namespace Google.Protobuf ...@@ -50,8 +50,8 @@ namespace Google.Protobuf
public static void MergeFrom(this IMessage message, ByteString data) public static void MergeFrom(this IMessage message, ByteString data)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
ThrowHelper.ThrowIfNull(data, "data"); Preconditions.CheckNotNull(data, "data");
CodedInputStream input = data.CreateCodedInput(); CodedInputStream input = data.CreateCodedInput();
message.MergeFrom(input); message.MergeFrom(input);
input.CheckLastTagWas(0); input.CheckLastTagWas(0);
...@@ -59,8 +59,8 @@ namespace Google.Protobuf ...@@ -59,8 +59,8 @@ namespace Google.Protobuf
public static void MergeFrom(this IMessage message, Stream input) public static void MergeFrom(this IMessage message, Stream input)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
ThrowHelper.ThrowIfNull(input, "input"); Preconditions.CheckNotNull(input, "input");
CodedInputStream codedInput = CodedInputStream.CreateInstance(input); CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
message.MergeFrom(codedInput); message.MergeFrom(codedInput);
codedInput.CheckLastTagWas(0); codedInput.CheckLastTagWas(0);
...@@ -68,8 +68,8 @@ namespace Google.Protobuf ...@@ -68,8 +68,8 @@ namespace Google.Protobuf
public static void MergeDelimitedFrom(this IMessage message, Stream input) public static void MergeDelimitedFrom(this IMessage message, Stream input)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
ThrowHelper.ThrowIfNull(input, "input"); Preconditions.CheckNotNull(input, "input");
int size = (int) CodedInputStream.ReadRawVarint32(input); int size = (int) CodedInputStream.ReadRawVarint32(input);
Stream limitedStream = new LimitedInputStream(input, size); Stream limitedStream = new LimitedInputStream(input, size);
message.MergeFrom(limitedStream); message.MergeFrom(limitedStream);
...@@ -77,7 +77,7 @@ namespace Google.Protobuf ...@@ -77,7 +77,7 @@ namespace Google.Protobuf
public static byte[] ToByteArray(this IMessage message) public static byte[] ToByteArray(this IMessage message)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
byte[] result = new byte[message.CalculateSize()]; byte[] result = new byte[message.CalculateSize()];
CodedOutputStream output = CodedOutputStream.CreateInstance(result); CodedOutputStream output = CodedOutputStream.CreateInstance(result);
message.WriteTo(output); message.WriteTo(output);
...@@ -87,8 +87,8 @@ namespace Google.Protobuf ...@@ -87,8 +87,8 @@ namespace Google.Protobuf
public static void WriteTo(this IMessage message, Stream output) public static void WriteTo(this IMessage message, Stream output)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
ThrowHelper.ThrowIfNull(output, "output"); Preconditions.CheckNotNull(output, "output");
CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
message.WriteTo(codedOutput); message.WriteTo(codedOutput);
codedOutput.Flush(); codedOutput.Flush();
...@@ -96,8 +96,8 @@ namespace Google.Protobuf ...@@ -96,8 +96,8 @@ namespace Google.Protobuf
public static void WriteDelimitedTo(this IMessage message, Stream output) public static void WriteDelimitedTo(this IMessage message, Stream output)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
ThrowHelper.ThrowIfNull(output, "output"); Preconditions.CheckNotNull(output, "output");
CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
codedOutput.WriteRawVarint32((uint)message.CalculateSize()); codedOutput.WriteRawVarint32((uint)message.CalculateSize());
message.WriteTo(codedOutput); message.WriteTo(codedOutput);
...@@ -106,7 +106,7 @@ namespace Google.Protobuf ...@@ -106,7 +106,7 @@ namespace Google.Protobuf
public static ByteString ToByteString(this IMessage message) public static ByteString ToByteString(this IMessage message)
{ {
ThrowHelper.ThrowIfNull(message, "message"); Preconditions.CheckNotNull(message, "message");
return ByteString.AttachBytes(message.ToByteArray()); return ByteString.AttachBytes(message.ToByteArray());
} }
} }
......
...@@ -84,7 +84,7 @@ namespace Google.Protobuf ...@@ -84,7 +84,7 @@ namespace Google.Protobuf
/// <returns>The newly parsed message.</returns> /// <returns>The newly parsed message.</returns>
public T ParseFrom(byte[] data) public T ParseFrom(byte[] data)
{ {
ThrowHelper.ThrowIfNull(data, "data"); Preconditions.CheckNotNull(data, "data");
T message = factory(); T message = factory();
message.MergeFrom(data); message.MergeFrom(data);
return message; return message;
...@@ -92,7 +92,7 @@ namespace Google.Protobuf ...@@ -92,7 +92,7 @@ namespace Google.Protobuf
public T ParseFrom(ByteString data) public T ParseFrom(ByteString data)
{ {
ThrowHelper.ThrowIfNull(data, "data"); Preconditions.CheckNotNull(data, "data");
T message = factory(); T message = factory();
message.MergeFrom(data); message.MergeFrom(data);
return message; return message;
......
...@@ -37,17 +37,38 @@ namespace Google.Protobuf ...@@ -37,17 +37,38 @@ namespace Google.Protobuf
/// <summary> /// <summary>
/// Helper methods for throwing exceptions /// Helper methods for throwing exceptions
/// </summary> /// </summary>
internal static class ThrowHelper public static class Preconditions
{ {
/// <summary> /// <summary>
/// Throws an ArgumentNullException if the given value is null. /// Throws an ArgumentNullException if the given value is null, otherwise
/// return the value to the caller.
/// </summary> /// </summary>
internal static void ThrowIfNull(object value, string name) public static T CheckNotNull<T>(T value, string name) where T : class
{ {
if (value == null) if (value == null)
{ {
throw new ArgumentNullException(name); throw new ArgumentNullException(name);
} }
return value;
}
/// <summary>
/// Throws an ArgumentNullException if the given value is null, otherwise
/// return the value to the caller.
/// </summary>
/// <remarks>
/// This is equivalent to <see cref="CheckNotNull"/> but without the type parameter
/// constraint. In most cases, the constraint is useful to prevent you from calling CheckNotNull
/// with a value type - but it gets in the way if either you want to use it with a nullable
/// value type, or you want to use it with an unconstrained type parameter.
/// </remarks>
internal static T CheckNotNullUnconstrained<T>(T value, string name)
{
if (value == null)
{
throw new ArgumentNullException(name);
}
return value;
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment