aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/CodedInputStream.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2015-02-07 15:33:24 -0600
committerrogerk <devnull@localhost>2015-02-07 15:33:24 -0600
commit1e29e701a73d3fa1466d6999b765aaddf12f1b59 (patch)
treef1350c507286e87b02550f5c3ddd6987142ffbac /src/ProtocolBuffers/CodedInputStream.cs
parente234691b67fb62057dfa0d71196e508f230bfb5f (diff)
downloadprotobuf-1e29e701a73d3fa1466d6999b765aaddf12f1b59.tar.gz
protobuf-1e29e701a73d3fa1466d6999b765aaddf12f1b59.tar.bz2
protobuf-1e29e701a73d3fa1466d6999b765aaddf12f1b59.zip
Fix build error for missing method Enum.GetValues() on some platforms
Diffstat (limited to 'src/ProtocolBuffers/CodedInputStream.cs')
-rw-r--r--src/ProtocolBuffers/CodedInputStream.cs81
1 files changed, 1 insertions, 80 deletions
diff --git a/src/ProtocolBuffers/CodedInputStream.cs b/src/ProtocolBuffers/CodedInputStream.cs
index 4f3dfb2d..773e8c18 100644
--- a/src/ProtocolBuffers/CodedInputStream.cs
+++ b/src/ProtocolBuffers/CodedInputStream.cs
@@ -482,7 +482,7 @@ namespace Google.ProtocolBuffers
where T : struct, IComparable, IFormattable
{
int number = (int) ReadRawVarint32();
- if (EnumHelper<T>.TryConvert(number, ref value))
+ if (EnumParser<T>.TryConvert(number, ref value))
{
unknown = null;
return true;
@@ -1860,84 +1860,5 @@ namespace Google.ProtocolBuffers
}
#endregion
-
- /// <summary>
- /// Helper class to make parsing enums faster.
- /// </summary>
- private static class EnumHelper<T> where T : struct
- {
- /// <summary>
- /// We use the array form if all values are in the range [0, LimitForArray),
- /// otherwise we build a dictionary.
- /// </summary>
- private const int LimitForArray = 32;
- // Only one of these will be populated.
- private static readonly Dictionary<int, T> dictionary;
- private static readonly T?[] values;
-
- static EnumHelper()
- {
- // It will actually be a T[], but the CLR will let us convert.
- int[] array = (int[]) Enum.GetValues(typeof (T));
- if (array.Length == 0)
- {
- // Empty enum; model with an empty values array.
- values = new T?[0];
- return;
- }
- int min = int.MaxValue;
- int max = int.MinValue;
- foreach (int number in array)
- {
- min = Math.Min(number, min);
- max = Math.Max(number, max);
- }
- if (min >= 0 && max < LimitForArray)
- {
- values = new T?[max + 1];
- foreach (int number in array)
- {
- values[number] = (T)(object)number;
- }
- }
- else
- {
- dictionary = new Dictionary<int, T>();
- foreach (int number in array)
- {
- dictionary[number] = (T)(object)number;
- }
- }
- }
-
- /// <summary>
- /// Tries to convert an integer to its enum representation. This would take an out parameter,
- /// but the caller uses ref, so this approach is simpler.
- /// </summary>
- internal static bool TryConvert(int number, ref T value)
- {
- if (values != null)
- {
- if (number < 0 || number >= values.Length)
- {
- return false;
- }
- T? maybeValue = values[number];
- if (maybeValue != null)
- {
- value = maybeValue.Value;
- return true;
- }
- return false;
- }
- T converted;
- if (dictionary.TryGetValue(number, out converted))
- {
- value = converted;
- return true;
- }
- return false;
- }
- }
}
} \ No newline at end of file