parent
e75a10d8ff
commit
aa5104143c
22 changed files with 59 additions and 1154 deletions
@ -1,60 +0,0 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System.Reflection; |
||||
using Google.Protobuf.Descriptors; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Google.Protobuf |
||||
{ |
||||
public class WireFormatTest |
||||
{ |
||||
/// <summary> |
||||
/// Keeps the attributes on FieldType and the switch statement in WireFormat in sync. |
||||
/// </summary> |
||||
[Test] |
||||
public void FieldTypeToWireTypeMapping() |
||||
{ |
||||
foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) |
||||
{ |
||||
FieldType fieldType = (FieldType) field.GetValue(null); |
||||
FieldMappingAttribute mapping = |
||||
(FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0]; |
||||
Assert.AreEqual(mapping.WireType, WireFormat.GetWireType(fieldType)); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,122 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace Google.Protobuf.Collections |
||||
{ |
||||
/// <summary> |
||||
/// Utility class for dictionaries. |
||||
/// </summary> |
||||
public static class Dictionaries |
||||
{ |
||||
/// <summary> |
||||
/// Compares two dictionaries for equality. Each value is compared with equality using Equals |
||||
/// for non-IEnumerable implementations, and using EnumerableEquals otherwise. |
||||
/// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing... |
||||
/// </summary> |
||||
public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right) |
||||
{ |
||||
if (left.Count != right.Count) |
||||
{ |
||||
return false; |
||||
} |
||||
foreach (KeyValuePair<TKey, TValue> leftEntry in left) |
||||
{ |
||||
TValue rightValue; |
||||
if (!right.TryGetValue(leftEntry.Key, out rightValue)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
IEnumerable leftEnumerable = leftEntry.Value as IEnumerable; |
||||
IEnumerable rightEnumerable = rightValue as IEnumerable; |
||||
if (leftEnumerable == null || rightEnumerable == null) |
||||
{ |
||||
if (!Equals(leftEntry.Value, rightValue)) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
if (!Enumerables.Equals(leftEnumerable, rightEnumerable)) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(IDictionary<TKey, TValue> dictionary) |
||||
{ |
||||
return dictionary.IsReadOnly ? dictionary : new ReadOnlyDictionary<TKey, TValue>(dictionary); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates a hashcode for a dictionary by XORing the hashcodes of all the fields |
||||
/// and values. (By XORing, we avoid ordering issues.) |
||||
/// TODO(jonskeet): Currently XORs other stuff too, and assumes non-null values. |
||||
/// </summary> |
||||
public static int GetHashCode<TKey, TValue>(IDictionary<TKey, TValue> dictionary) |
||||
{ |
||||
int ret = 31; |
||||
foreach (KeyValuePair<TKey, TValue> entry in dictionary) |
||||
{ |
||||
int hash = entry.Key.GetHashCode() ^ GetDeepHashCode(entry.Value); |
||||
ret ^= hash; |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Determines the hash of a value by either taking it directly or hashing all the elements |
||||
/// for IEnumerable implementations. |
||||
/// </summary> |
||||
private static int GetDeepHashCode(object value) |
||||
{ |
||||
IEnumerable iterable = value as IEnumerable; |
||||
if (iterable == null) |
||||
{ |
||||
return value.GetHashCode(); |
||||
} |
||||
int hash = 29; |
||||
foreach (object element in iterable) |
||||
{ |
||||
hash = hash*37 + element.GetHashCode(); |
||||
} |
||||
return hash; |
||||
} |
||||
} |
||||
} |
@ -1,74 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
using System; |
||||
using System.Collections; |
||||
|
||||
namespace Google.Protobuf.Collections |
||||
{ |
||||
/// <summary> |
||||
/// Utility class for IEnumerable (and potentially the generic version in the future). |
||||
/// </summary> |
||||
public static class Enumerables |
||||
{ |
||||
public static bool Equals(IEnumerable left, IEnumerable right) |
||||
{ |
||||
IEnumerator leftEnumerator = left.GetEnumerator(); |
||||
try |
||||
{ |
||||
foreach (object rightObject in right) |
||||
{ |
||||
if (!leftEnumerator.MoveNext()) |
||||
{ |
||||
return false; |
||||
} |
||||
if (!Equals(leftEnumerator.Current, rightObject)) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
if (leftEnumerator.MoveNext()) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
finally |
||||
{ |
||||
IDisposable leftEnumeratorDisposable = leftEnumerator as IDisposable; |
||||
if (leftEnumeratorDisposable != null) |
||||
{ |
||||
leftEnumeratorDisposable.Dispose(); |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
} |
||||
} |
@ -1,110 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
using System.Collections.Generic; |
||||
using System.Collections.ObjectModel; |
||||
|
||||
namespace Google.Protobuf.Collections |
||||
{ |
||||
/// <summary> |
||||
/// Utility non-generic class for calling into Lists{T} using type inference. |
||||
/// </summary> |
||||
public static class Lists |
||||
{ |
||||
/// <summary> |
||||
/// Returns a read-only view of the specified list. |
||||
/// </summary> |
||||
public static IList<T> AsReadOnly<T>(IList<T> list) |
||||
{ |
||||
return Lists<T>.AsReadOnly(list); |
||||
} |
||||
|
||||
public static bool Equals<T>(IList<T> left, IList<T> right) |
||||
{ |
||||
if (left == right) |
||||
{ |
||||
return true; |
||||
} |
||||
if (left == null || right == null) |
||||
{ |
||||
return false; |
||||
} |
||||
if (left.Count != right.Count) |
||||
{ |
||||
return false; |
||||
} |
||||
IEqualityComparer<T> comparer = EqualityComparer<T>.Default; |
||||
for (int i = 0; i < left.Count; i++) |
||||
{ |
||||
if (!comparer.Equals(left[i], right[i])) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public static int GetHashCode<T>(IList<T> list) |
||||
{ |
||||
int hash = 31; |
||||
foreach (T element in list) |
||||
{ |
||||
hash = hash*29 + element.GetHashCode(); |
||||
} |
||||
return hash; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Utility class for dealing with lists. |
||||
/// </summary> |
||||
public static class Lists<T> |
||||
{ |
||||
private static readonly ReadOnlyCollection<T> empty = new ReadOnlyCollection<T>(new T[0]); |
||||
|
||||
/// <summary> |
||||
/// Returns an immutable empty list. |
||||
/// </summary> |
||||
public static ReadOnlyCollection<T> Empty |
||||
{ |
||||
get { return empty; } |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Returns either the original reference if it's already read-only, |
||||
/// or a new ReadOnlyCollection wrapping the original list. |
||||
/// </summary> |
||||
public static IList<T> AsReadOnly(IList<T> list) |
||||
{ |
||||
return list.IsReadOnly ? list : new ReadOnlyCollection<T>(list); |
||||
} |
||||
} |
||||
} |
@ -1,85 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Reflection; |
||||
using Google.Protobuf.Collections; |
||||
|
||||
namespace Google.Protobuf.Descriptors |
||||
{ |
||||
/// <summary> |
||||
/// Defined specifically for the <see cref="FieldType" /> enumeration, |
||||
/// this allows each field type to specify the mapped type and wire type. |
||||
/// </summary> |
||||
[AttributeUsage(AttributeTargets.Field)] |
||||
public sealed class FieldMappingAttribute : Attribute |
||||
{ |
||||
public FieldMappingAttribute(MappedType mappedType, WireFormat.WireType wireType) |
||||
{ |
||||
MappedType = mappedType; |
||||
WireType = wireType; |
||||
} |
||||
|
||||
public MappedType MappedType { get; private set; } |
||||
public WireFormat.WireType WireType { get; private set; } |
||||
|
||||
|
||||
/// <summary> |
||||
/// Immutable mapping from field type to mapped type. Built using the attributes on |
||||
/// FieldType values. |
||||
/// </summary> |
||||
private static readonly IDictionary<FieldType, FieldMappingAttribute> FieldTypeToMappedTypeMap = MapFieldTypes(); |
||||
|
||||
private static IDictionary<FieldType, FieldMappingAttribute> MapFieldTypes() |
||||
{ |
||||
var map = new Dictionary<FieldType, FieldMappingAttribute>(); |
||||
foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) |
||||
{ |
||||
FieldType fieldType = (FieldType) field.GetValue(null); |
||||
FieldMappingAttribute mapping = |
||||
(FieldMappingAttribute) field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0]; |
||||
map[fieldType] = mapping; |
||||
} |
||||
return Dictionaries.AsReadOnly(map); |
||||
} |
||||
|
||||
internal static MappedType MappedTypeFromFieldType(FieldType type) |
||||
{ |
||||
return FieldTypeToMappedTypeMap[type].MappedType; |
||||
} |
||||
|
||||
internal static WireFormat.WireType WireTypeFromFieldType(FieldType type, bool packed) |
||||
{ |
||||
return packed ? WireFormat.WireType.LengthDelimited : FieldTypeToMappedTypeMap[type].WireType; |
||||
} |
||||
} |
||||
} |
@ -1,52 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
namespace Google.Protobuf.Descriptors |
||||
{ |
||||
/// <summary> |
||||
/// Type as it's mapped onto a .NET type. |
||||
/// </summary> |
||||
public enum MappedType |
||||
{ |
||||
Int32, |
||||
Int64, |
||||
UInt32, |
||||
UInt64, |
||||
Single, |
||||
Double, |
||||
Boolean, |
||||
String, |
||||
ByteString, |
||||
Message, |
||||
Enum |
||||
} |
||||
} |
@ -1,159 +0,0 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// http://github.com/jskeet/dotnet-protobufs/ |
||||
// Original C++/Java/Python code: |
||||
// http://code.google.com/p/protobuf/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.IO; |
||||
using System.Text; |
||||
|
||||
namespace Google.Protobuf |
||||
{ |
||||
/// <summary> |
||||
/// Helper class to control indentation. Used for TextFormat and by ProtoGen. |
||||
/// </summary> |
||||
public sealed class TextGenerator |
||||
{ |
||||
/// <summary> |
||||
/// The string to use at the end of each line. We assume that "Print" is only called using \n |
||||
/// to indicate a line break; that's what we use to detect when we need to indent etc, and |
||||
/// *just* the \n is replaced with the contents of lineBreak. |
||||
/// </summary> |
||||
private readonly string lineBreak; |
||||
|
||||
/// <summary> |
||||
/// Writer to write formatted text to. |
||||
/// </summary> |
||||
private readonly TextWriter writer; |
||||
|
||||
/// <summary> |
||||
/// Keeps track of whether the next piece of text should be indented |
||||
/// </summary> |
||||
private bool atStartOfLine = true; |
||||
|
||||
/// <summary> |
||||
/// Keeps track of the current level of indentation |
||||
/// </summary> |
||||
private readonly StringBuilder indent = new StringBuilder(); |
||||
|
||||
/// <summary> |
||||
/// Creates a generator writing to the given writer. The writer |
||||
/// is not closed by this class. |
||||
/// </summary> |
||||
public TextGenerator(TextWriter writer, string lineBreak) |
||||
{ |
||||
this.writer = writer; |
||||
this.lineBreak = lineBreak; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Indents text by two spaces. After calling Indent(), two spaces |
||||
/// will be inserted at the beginning of each line of text. Indent() may |
||||
/// be called multiple times to produce deeper indents. |
||||
/// </summary> |
||||
public void Indent() |
||||
{ |
||||
indent.Append(" "); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Reduces the current indent level by two spaces. |
||||
/// </summary> |
||||
public void Outdent() |
||||
{ |
||||
if (indent.Length == 0) |
||||
{ |
||||
throw new InvalidOperationException("Too many calls to Outdent()"); |
||||
} |
||||
indent.Length -= 2; |
||||
} |
||||
|
||||
public void WriteLine(string text) |
||||
{ |
||||
Print(text); |
||||
Print("\n"); |
||||
} |
||||
|
||||
public void WriteLine(string format, params object[] args) |
||||
{ |
||||
WriteLine(string.Format(format, args)); |
||||
} |
||||
|
||||
public void WriteLine() |
||||
{ |
||||
WriteLine(""); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Prints the given text to the output stream, indenting at line boundaries. |
||||
/// </summary> |
||||
/// <param name="text"></param> |
||||
public void Print(string text) |
||||
{ |
||||
int pos = 0; |
||||
|
||||
for (int i = 0; i < text.Length; i++) |
||||
{ |
||||
if (text[i] == '\n') |
||||
{ |
||||
// Strip off the \n from what we write |
||||
Write(text.Substring(pos, i - pos)); |
||||
Write(lineBreak); |
||||
pos = i + 1; |
||||
atStartOfLine = true; |
||||
} |
||||
} |
||||
Write(text.Substring(pos)); |
||||
} |
||||
|
||||
public void Write(string format, params object[] args) |
||||
{ |
||||
Write(string.Format(format, args)); |
||||
} |
||||
|
||||
private void Write(string data) |
||||
{ |
||||
if (data.Length == 0) |
||||
{ |
||||
return; |
||||
} |
||||
if (atStartOfLine) |
||||
{ |
||||
atStartOfLine = false; |
||||
writer.Write(indent); |
||||
} |
||||
writer.Write(data); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue