blob: c2ba5627ce542123318de37ed01a0cdebd5dd1e3 [file] [log] [blame]
csharptest980ba8d2010-11-07 16:30:39 -06001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
csharptest80824a52010-11-07 17:25:47 -060036using System.Collections;
csharptest980ba8d2010-11-07 16:30:39 -060037using System.Collections.Generic;
38using Google.ProtocolBuffers.Collections;
csharptest804b6d82010-11-07 10:49:33 -060039using Google.ProtocolBuffers.Descriptors;
csharptestd9c59e62010-11-04 19:36:28 -050040
41namespace Google.ProtocolBuffers {
42
43 public interface IGeneratedExtensionLite {
44 int Number { get; }
45 object ContainingType { get; }
46 IMessageLite MessageDefaultInstance { get; }
47 }
csharptest804b6d82010-11-07 10:49:33 -060048
csharptest980ba8d2010-11-07 16:30:39 -060049 public class ExtensionDescriptorLite : IFieldDescriptorLite {
50 /// <summary>
51 /// Immutable mapping from field type to mapped type. Built using the attributes on
52 /// FieldType values.
53 /// </summary>
54 public static readonly IDictionary<FieldType, MappedType> FieldTypeToMappedTypeMap = MapFieldTypes();
55
56 private static IDictionary<FieldType, MappedType> MapFieldTypes() {
57 var map = new Dictionary<FieldType, MappedType>();
58 foreach (System.Reflection.FieldInfo field in typeof(FieldType).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)) {
59 FieldType fieldType = (FieldType)field.GetValue(null);
60 FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
61 map[fieldType] = mapping.MappedType;
62 }
63 return Dictionaries.AsReadOnly(map);
64 }
65
66 private readonly IEnumLiteMap enumTypeMap;
csharptest804b6d82010-11-07 10:49:33 -060067 private readonly int number;
68 private readonly FieldType type;
69 private readonly bool isRepeated;
70 private readonly bool isPacked;
csharptest980ba8d2010-11-07 16:30:39 -060071 private readonly MappedType mapType;
72 private readonly object defaultValue;
csharptest804b6d82010-11-07 10:49:33 -060073
csharptest980ba8d2010-11-07 16:30:39 -060074 public ExtensionDescriptorLite(IEnumLiteMap enumTypeMap, int number, FieldType type, object defaultValue, bool isRepeated, bool isPacked) {
csharptest804b6d82010-11-07 10:49:33 -060075 this.enumTypeMap = enumTypeMap;
76 this.number = number;
77 this.type = type;
csharptest980ba8d2010-11-07 16:30:39 -060078 this.mapType = FieldTypeToMappedTypeMap[type];
csharptest804b6d82010-11-07 10:49:33 -060079 this.isRepeated = isRepeated;
80 this.isPacked = isPacked;
csharptest980ba8d2010-11-07 16:30:39 -060081 this.defaultValue = defaultValue;
csharptest804b6d82010-11-07 10:49:33 -060082 }
83
csharptest980ba8d2010-11-07 16:30:39 -060084 public bool IsRepeated {
85 get { return isRepeated; }
86 }
87
88 public bool IsRequired {
89 get { return false; }
90 }
91
92 public bool IsPacked {
93 get { return isPacked; }
94 }
95
96 public bool IsExtension {
97 get { return true; }
98 }
99
100#warning ToDo - Discover the meaning and purpose of this durring serialization and return the correct value
101 public bool MessageSetWireFormat {
102 get { return true; }
103 }
104
105 public int FieldNumber {
csharptest804b6d82010-11-07 10:49:33 -0600106 get { return number; }
107 }
csharptest980ba8d2010-11-07 16:30:39 -0600108
109 public IEnumLiteMap EnumType {
110 get { return enumTypeMap; }
111 }
112
113 public FieldType FieldType {
114 get { return type; }
115 }
116
117 public MappedType MappedType {
118 get { return mapType; }
119 }
120
121 public object DefaultValue {
122 get { return defaultValue; }
123 }
124
125 public int CompareTo(IFieldDescriptorLite other) {
126 return FieldNumber.CompareTo(other.FieldNumber);
127 }
csharptest804b6d82010-11-07 10:49:33 -0600128 }
129
csharptest804b6d82010-11-07 10:49:33 -0600130 public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite
131 where TContainingType : IMessageLite {
132
133 private readonly TContainingType containingTypeDefaultInstance;
134 private readonly TExtensionType defaultValue;
135 private readonly IMessageLite messageDefaultInstance;
136 private readonly ExtensionDescriptorLite descriptor;
137
138 // We can't always initialize a GeneratedExtension when we first construct
139 // it due to initialization order difficulties (namely, the default
140 // instances may not have been constructed yet). So, we construct an
141 // uninitialized GeneratedExtension once, then call internalInit() on it
142 // later. Generated code will always call internalInit() on all extensions
143 // as part of the static initialization code, and internalInit() throws an
144 // exception if called more than once, so this method is useless to users.
145 protected GeneratedExtensionLite(
146 TContainingType containingTypeDefaultInstance,
147 TExtensionType defaultValue,
148 IMessageLite messageDefaultInstance,
149 ExtensionDescriptorLite descriptor) {
150 this.containingTypeDefaultInstance = containingTypeDefaultInstance;
151 this.messageDefaultInstance = messageDefaultInstance;
152 this.defaultValue = defaultValue;
153 this.descriptor = descriptor;
154 }
155
156 /** For use by generated code only. */
157 public GeneratedExtensionLite(
158 TContainingType containingTypeDefaultInstance,
159 TExtensionType defaultValue,
160 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600161 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600162 int number,
163 FieldType type)
164 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600165 new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
csharptest804b6d82010-11-07 10:49:33 -0600166 false /* isRepeated */, false /* isPacked */)) {
167 }
168
169 /** For use by generated code only. */
170 public GeneratedExtensionLite(
171 TContainingType containingTypeDefaultInstance,
172 TExtensionType defaultValue,
173 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600174 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600175 int number,
176 FieldType type,
177 bool isPacked)
178 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600179 new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
csharptest804b6d82010-11-07 10:49:33 -0600180 true /* isRepeated */, isPacked)) {
181 }
182
183 /// <summary>
csharptest980ba8d2010-11-07 16:30:39 -0600184 /// Returns information about this extension
185 /// </summary>
186 public IFieldDescriptorLite Descriptor {
187 get { return descriptor; }
188 }
189
190 /// <summary>
191 /// Returns the default value for this extension
192 /// </summary>
193 public TExtensionType DefaultValue {
194 get { return defaultValue; }
195 }
196
197 /// <summary>
csharptest804b6d82010-11-07 10:49:33 -0600198 /// used for the extension registry
199 /// </summary>
200 object IGeneratedExtensionLite.ContainingType {
201 get { return ContainingTypeDefaultInstance; }
202 }
203 /**
204 * Default instance of the type being extended, used to identify that type.
205 */
206 public TContainingType ContainingTypeDefaultInstance {
207 get {
208 return containingTypeDefaultInstance;
209 }
210 }
211
212 /** Get the field number. */
213 public int Number {
214 get {
csharptest980ba8d2010-11-07 16:30:39 -0600215 return descriptor.FieldNumber;
csharptest804b6d82010-11-07 10:49:33 -0600216 }
217 }
218 /**
219 * If the extension is an embedded message, this is the default instance of
220 * that type.
221 */
222 public IMessageLite MessageDefaultInstance {
223 get {
224 return messageDefaultInstance;
225 }
226 }
csharptest980ba8d2010-11-07 16:30:39 -0600227
csharptest80824a52010-11-07 17:25:47 -0600228 /// <summary>
229 /// Converts from the type used by the native accessors to the type
230 /// used by reflection accessors. For example, the reflection accessors
231 /// for enums use EnumValueDescriptors but the native accessors use
232 /// the generated enum type.
233 /// </summary>
234 public object ToReflectionType(object value) {
235 if (descriptor.IsRepeated) {
236 if (descriptor.MappedType == MappedType.Enum) {
237 // Must convert the whole list.
238 IList<object> result = new List<object>();
239 foreach (object element in (IEnumerable)value) {
240 result.Add(SingularToReflectionType(element));
241 }
242 return result;
243 } else {
244 return value;
245 }
246 } else {
247 return SingularToReflectionType(value);
248 }
249 }
250
251 /// <summary>
252 /// Like ToReflectionType(object) but for a single element.
253 /// </summary>
254 internal Object SingularToReflectionType(object value) {
255 return descriptor.MappedType == MappedType.Enum
256 ? descriptor.EnumType.FindValueByNumber((int)value)
257 : value;
258 }
259
260 public object FromReflectionType(object value) {
261 if (descriptor.IsRepeated) {
262 if (Descriptor.MappedType == MappedType.Message ||
263 Descriptor.MappedType == MappedType.Enum) {
264 // Must convert the whole list.
265 List<TExtensionType> result = new List<TExtensionType>();
266 foreach (object element in (IEnumerable)value) {
267 result.Add((TExtensionType)SingularFromReflectionType(element));
268 }
269 return result;
270 } else {
271 return value;
272 }
273 } else {
274 return SingularFromReflectionType(value);
275 }
276 }
277
csharptest980ba8d2010-11-07 16:30:39 -0600278 public object SingularFromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600279 switch (Descriptor.MappedType) {
280 case MappedType.Message:
281 if (value is TExtensionType) {
282 return value;
283 } else {
284 // It seems the copy of the embedded message stored inside the
285 // extended message is not of the exact type the user was
286 // expecting. This can happen if a user defines a
287 // GeneratedExtension manually and gives it a different type.
288 // This should not happen in normal use. But, to be nice, we'll
289 // copy the message to whatever type the caller was expecting.
290 return MessageDefaultInstance.WeakCreateBuilderForType()
291 .WeakMergeFrom((IMessageLite)value).WeakBuild();
292 }
293 case MappedType.Enum:
294 // Just return a boxed int - that can be unboxed to the enum
295 IEnumLite enumValue = (IEnumLite)value;
296 return enumValue.Number;
297 default:
298 return value;
299 }
csharptest980ba8d2010-11-07 16:30:39 -0600300 }
csharptest804b6d82010-11-07 10:49:33 -0600301 }
csharptestd9c59e62010-11-04 19:36:28 -0500302}