blob: 87009cf8c45e7f1e2e31dd92c0849a1cfc8e25a8 [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 }
csharptest487da482010-11-07 18:49:42 -0600129
130 public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> : GeneratedExtensionLite<TContainingType, IList<TExtensionType>>
131 where TContainingType : IMessageLite {
132 public GeneratedRepeatExtensionLite(TContainingType containingTypeDefaultInstance,
133 IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number, FieldType type, bool isPacked) :
134 base(containingTypeDefaultInstance, new List<TExtensionType>(), messageDefaultInstance, enumTypeMap, number, type, isPacked) {
135 }
136
137 public override object ToReflectionType(object value) {
138 IList<TExtensionType> result = new List<TExtensionType>();
139 foreach (object element in (IEnumerable)value) {
140 result.Add((TExtensionType)SingularToReflectionType(element));
141 }
142 return result;
143 }
144
145 public override object FromReflectionType(object value) {
146 // Must convert the whole list.
147 List<TExtensionType> result = new List<TExtensionType>();
148 foreach (object element in (IEnumerable)value) {
149 result.Add((TExtensionType)SingularFromReflectionType(element));
150 }
151 return result;
152 }
153 }
154
csharptest804b6d82010-11-07 10:49:33 -0600155 public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite
156 where TContainingType : IMessageLite {
157
158 private readonly TContainingType containingTypeDefaultInstance;
159 private readonly TExtensionType defaultValue;
160 private readonly IMessageLite messageDefaultInstance;
161 private readonly ExtensionDescriptorLite descriptor;
162
163 // We can't always initialize a GeneratedExtension when we first construct
164 // it due to initialization order difficulties (namely, the default
165 // instances may not have been constructed yet). So, we construct an
166 // uninitialized GeneratedExtension once, then call internalInit() on it
167 // later. Generated code will always call internalInit() on all extensions
168 // as part of the static initialization code, and internalInit() throws an
169 // exception if called more than once, so this method is useless to users.
170 protected GeneratedExtensionLite(
171 TContainingType containingTypeDefaultInstance,
172 TExtensionType defaultValue,
173 IMessageLite messageDefaultInstance,
174 ExtensionDescriptorLite descriptor) {
175 this.containingTypeDefaultInstance = containingTypeDefaultInstance;
176 this.messageDefaultInstance = messageDefaultInstance;
177 this.defaultValue = defaultValue;
178 this.descriptor = descriptor;
179 }
180
181 /** For use by generated code only. */
182 public GeneratedExtensionLite(
183 TContainingType containingTypeDefaultInstance,
184 TExtensionType defaultValue,
185 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600186 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600187 int number,
188 FieldType type)
189 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600190 new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
csharptest804b6d82010-11-07 10:49:33 -0600191 false /* isRepeated */, false /* isPacked */)) {
192 }
193
csharptest487da482010-11-07 18:49:42 -0600194 /** Repeating fields: For use by generated code only. */
195 protected GeneratedExtensionLite(
csharptest804b6d82010-11-07 10:49:33 -0600196 TContainingType containingTypeDefaultInstance,
197 TExtensionType defaultValue,
198 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600199 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600200 int number,
201 FieldType type,
202 bool isPacked)
203 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600204 new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
csharptest804b6d82010-11-07 10:49:33 -0600205 true /* isRepeated */, isPacked)) {
206 }
207
208 /// <summary>
csharptest980ba8d2010-11-07 16:30:39 -0600209 /// Returns information about this extension
210 /// </summary>
211 public IFieldDescriptorLite Descriptor {
212 get { return descriptor; }
213 }
214
215 /// <summary>
216 /// Returns the default value for this extension
217 /// </summary>
218 public TExtensionType DefaultValue {
219 get { return defaultValue; }
220 }
221
222 /// <summary>
csharptest804b6d82010-11-07 10:49:33 -0600223 /// used for the extension registry
224 /// </summary>
225 object IGeneratedExtensionLite.ContainingType {
226 get { return ContainingTypeDefaultInstance; }
227 }
228 /**
229 * Default instance of the type being extended, used to identify that type.
230 */
231 public TContainingType ContainingTypeDefaultInstance {
232 get {
233 return containingTypeDefaultInstance;
234 }
235 }
236
237 /** Get the field number. */
238 public int Number {
239 get {
csharptest980ba8d2010-11-07 16:30:39 -0600240 return descriptor.FieldNumber;
csharptest804b6d82010-11-07 10:49:33 -0600241 }
242 }
243 /**
244 * If the extension is an embedded message, this is the default instance of
245 * that type.
246 */
247 public IMessageLite MessageDefaultInstance {
248 get {
249 return messageDefaultInstance;
250 }
251 }
csharptest980ba8d2010-11-07 16:30:39 -0600252
csharptest80824a52010-11-07 17:25:47 -0600253 /// <summary>
254 /// Converts from the type used by the native accessors to the type
255 /// used by reflection accessors. For example, the reflection accessors
256 /// for enums use EnumValueDescriptors but the native accessors use
257 /// the generated enum type.
258 /// </summary>
csharptest487da482010-11-07 18:49:42 -0600259 public virtual object ToReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600260 return SingularToReflectionType(value);
csharptest80824a52010-11-07 17:25:47 -0600261 }
262
263 /// <summary>
264 /// Like ToReflectionType(object) but for a single element.
265 /// </summary>
csharptest487da482010-11-07 18:49:42 -0600266 public object SingularToReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600267 return descriptor.MappedType == MappedType.Enum
268 ? descriptor.EnumType.FindValueByNumber((int)value)
269 : value;
270 }
271
csharptest487da482010-11-07 18:49:42 -0600272 public virtual object FromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600273 return SingularFromReflectionType(value);
csharptest80824a52010-11-07 17:25:47 -0600274 }
275
csharptest980ba8d2010-11-07 16:30:39 -0600276 public object SingularFromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600277 switch (Descriptor.MappedType) {
278 case MappedType.Message:
279 if (value is TExtensionType) {
280 return value;
281 } else {
282 // It seems the copy of the embedded message stored inside the
283 // extended message is not of the exact type the user was
284 // expecting. This can happen if a user defines a
285 // GeneratedExtension manually and gives it a different type.
286 // This should not happen in normal use. But, to be nice, we'll
287 // copy the message to whatever type the caller was expecting.
288 return MessageDefaultInstance.WeakCreateBuilderForType()
289 .WeakMergeFrom((IMessageLite)value).WeakBuild();
290 }
291 case MappedType.Enum:
292 // Just return a boxed int - that can be unboxed to the enum
293 IEnumLite enumValue = (IEnumLite)value;
294 return enumValue.Number;
295 default:
296 return value;
297 }
csharptest980ba8d2010-11-07 16:30:39 -0600298 }
csharptest804b6d82010-11-07 10:49:33 -0600299 }
csharptestd9c59e62010-11-04 19:36:28 -0500300}