1 | #region Copyright notice and license
|
---|
2 |
|
---|
3 | // Protocol Buffers - Google's data interchange format
|
---|
4 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
5 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
6 | // Original C++/Java/Python code:
|
---|
7 | // http://code.google.com/p/protobuf/
|
---|
8 | //
|
---|
9 | // Redistribution and use in source and binary forms, with or without
|
---|
10 | // modification, are permitted provided that the following conditions are
|
---|
11 | // met:
|
---|
12 | //
|
---|
13 | // * Redistributions of source code must retain the above copyright
|
---|
14 | // notice, this list of conditions and the following disclaimer.
|
---|
15 | // * Redistributions in binary form must reproduce the above
|
---|
16 | // copyright notice, this list of conditions and the following disclaimer
|
---|
17 | // in the documentation and/or other materials provided with the
|
---|
18 | // distribution.
|
---|
19 | // * Neither the name of Google Inc. nor the names of its
|
---|
20 | // contributors may be used to endorse or promote products derived from
|
---|
21 | // this software without specific prior written permission.
|
---|
22 | //
|
---|
23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
34 |
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | using System;
|
---|
38 | using System.Collections.Generic;
|
---|
39 |
|
---|
40 | namespace Google.ProtocolBuffers
|
---|
41 | {
|
---|
42 | ///<summary>
|
---|
43 | ///Interface for an enum value or value descriptor, to be used in FieldSet.
|
---|
44 | ///The lite library stores enum values directly in FieldSets but the full
|
---|
45 | ///library stores EnumValueDescriptors in order to better support reflection.
|
---|
46 | ///</summary>
|
---|
47 | public interface IEnumLite
|
---|
48 | {
|
---|
49 | int Number { get; }
|
---|
50 | string Name { get; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | ///<summary>
|
---|
54 | ///Interface for an object which maps integers to {@link EnumLite}s.
|
---|
55 | ///{@link Descriptors.EnumDescriptor} implements this interface by mapping
|
---|
56 | ///numbers to {@link Descriptors.EnumValueDescriptor}s. Additionally,
|
---|
57 | ///every generated enum type has a static method internalGetValueMap() which
|
---|
58 | ///returns an implementation of this type that maps numbers to enum values.
|
---|
59 | ///</summary>
|
---|
60 | public interface IEnumLiteMap<T> : IEnumLiteMap
|
---|
61 | where T : IEnumLite
|
---|
62 | {
|
---|
63 | new T FindValueByNumber(int number);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public interface IEnumLiteMap
|
---|
67 | {
|
---|
68 | bool IsValidValue(IEnumLite value);
|
---|
69 | IEnumLite FindValueByNumber(int number);
|
---|
70 | IEnumLite FindValueByName(string name);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public class EnumLiteMap<TEnum> : IEnumLiteMap<IEnumLite>
|
---|
74 | where TEnum : struct, IComparable, IFormattable
|
---|
75 | {
|
---|
76 | private struct EnumValue : IEnumLite
|
---|
77 | {
|
---|
78 | private readonly TEnum value;
|
---|
79 |
|
---|
80 | public EnumValue(TEnum value)
|
---|
81 | {
|
---|
82 | this.value = value;
|
---|
83 | }
|
---|
84 |
|
---|
85 | int IEnumLite.Number
|
---|
86 | {
|
---|
87 | get { return Convert.ToInt32(value); }
|
---|
88 | }
|
---|
89 |
|
---|
90 | string IEnumLite.Name
|
---|
91 | {
|
---|
92 | get { return value.ToString(); }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private readonly SortedList<int, IEnumLite> items;
|
---|
97 |
|
---|
98 | public EnumLiteMap()
|
---|
99 | {
|
---|
100 | items = new SortedList<int, IEnumLite>();
|
---|
101 | #if SILVERLIGHT
|
---|
102 | // Silverlight doesn't support Enum.GetValues
|
---|
103 | // TODO(jonskeet): Validate that this reflection is permitted, e.g. in Windows Phone 7
|
---|
104 | foreach (System.Reflection.FieldInfo fi in typeof(TEnum).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public))
|
---|
105 | {
|
---|
106 | TEnum evalue = (TEnum) fi.GetValue(null);
|
---|
107 | items.Add(Convert.ToInt32(evalue), new EnumValue(evalue));
|
---|
108 | }
|
---|
109 | #else
|
---|
110 | foreach (TEnum evalue in Enum.GetValues(typeof (TEnum)))
|
---|
111 | {
|
---|
112 | items.Add(Convert.ToInt32(evalue), new EnumValue(evalue));
|
---|
113 | }
|
---|
114 | #endif
|
---|
115 | }
|
---|
116 |
|
---|
117 | IEnumLite IEnumLiteMap.FindValueByNumber(int number)
|
---|
118 | {
|
---|
119 | return FindValueByNumber(number);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public IEnumLite FindValueByNumber(int number)
|
---|
123 | {
|
---|
124 | IEnumLite val;
|
---|
125 | return items.TryGetValue(number, out val) ? val : null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public IEnumLite FindValueByName(string name)
|
---|
129 | {
|
---|
130 | IEnumLite val;
|
---|
131 | if (Enum.IsDefined(typeof (TEnum), name))
|
---|
132 | {
|
---|
133 | return items.TryGetValue((int) Enum.Parse(typeof (TEnum), name, false), out val) ? val : null;
|
---|
134 | }
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public bool IsValidValue(IEnumLite value)
|
---|
139 | {
|
---|
140 | return items.ContainsKey(value.Number);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | } |
---|