blob: a26088f815a218ef1e93d9a8d88c75acfd1af144 [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; }
csharptest7d396f92010-11-08 20:06:46 -060047 IFieldDescriptorLite Descriptor { get; }
csharptestd9c59e62010-11-04 19:36:28 -050048 }
csharptest804b6d82010-11-07 10:49:33 -060049
csharptest980ba8d2010-11-07 16:30:39 -060050 public class ExtensionDescriptorLite : IFieldDescriptorLite {
csharptest980ba8d2010-11-07 16:30:39 -060051 private readonly IEnumLiteMap enumTypeMap;
csharptest804b6d82010-11-07 10:49:33 -060052 private readonly int number;
53 private readonly FieldType type;
54 private readonly bool isRepeated;
55 private readonly bool isPacked;
csharptest980ba8d2010-11-07 16:30:39 -060056 private readonly MappedType mapType;
57 private readonly object defaultValue;
csharptest804b6d82010-11-07 10:49:33 -060058
csharptest980ba8d2010-11-07 16:30:39 -060059 public ExtensionDescriptorLite(IEnumLiteMap enumTypeMap, int number, FieldType type, object defaultValue, bool isRepeated, bool isPacked) {
csharptest804b6d82010-11-07 10:49:33 -060060 this.enumTypeMap = enumTypeMap;
61 this.number = number;
62 this.type = type;
csharptest7d396f92010-11-08 20:06:46 -060063 this.mapType = FieldMappingAttribute.MappedTypeFromFieldType(type);
csharptest804b6d82010-11-07 10:49:33 -060064 this.isRepeated = isRepeated;
65 this.isPacked = isPacked;
csharptest980ba8d2010-11-07 16:30:39 -060066 this.defaultValue = defaultValue;
csharptest804b6d82010-11-07 10:49:33 -060067 }
68
csharptest980ba8d2010-11-07 16:30:39 -060069 public bool IsRepeated {
70 get { return isRepeated; }
71 }
72
73 public bool IsRequired {
74 get { return false; }
75 }
76
77 public bool IsPacked {
78 get { return isPacked; }
79 }
80
81 public bool IsExtension {
82 get { return true; }
83 }
84
85#warning ToDo - Discover the meaning and purpose of this durring serialization and return the correct value
86 public bool MessageSetWireFormat {
csharptest7d396f92010-11-08 20:06:46 -060087 get { return false; }
csharptest980ba8d2010-11-07 16:30:39 -060088 }
89
90 public int FieldNumber {
csharptest804b6d82010-11-07 10:49:33 -060091 get { return number; }
92 }
csharptest980ba8d2010-11-07 16:30:39 -060093
94 public IEnumLiteMap EnumType {
95 get { return enumTypeMap; }
96 }
97
98 public FieldType FieldType {
99 get { return type; }
100 }
101
102 public MappedType MappedType {
103 get { return mapType; }
104 }
105
106 public object DefaultValue {
107 get { return defaultValue; }
108 }
109
110 public int CompareTo(IFieldDescriptorLite other) {
111 return FieldNumber.CompareTo(other.FieldNumber);
112 }
csharptest804b6d82010-11-07 10:49:33 -0600113 }
csharptest487da482010-11-07 18:49:42 -0600114
115 public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> : GeneratedExtensionLite<TContainingType, IList<TExtensionType>>
116 where TContainingType : IMessageLite {
117 public GeneratedRepeatExtensionLite(TContainingType containingTypeDefaultInstance,
118 IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number, FieldType type, bool isPacked) :
119 base(containingTypeDefaultInstance, new List<TExtensionType>(), messageDefaultInstance, enumTypeMap, number, type, isPacked) {
120 }
121
122 public override object ToReflectionType(object value) {
csharptest7d396f92010-11-08 20:06:46 -0600123 IList<object> result = new List<object>();
124 foreach (object element in (IEnumerable) value) {
125 result.Add(SingularToReflectionType(element));
126 }
127 return result;
csharptest487da482010-11-07 18:49:42 -0600128 }
129
130 public override object FromReflectionType(object value) {
131 // Must convert the whole list.
132 List<TExtensionType> result = new List<TExtensionType>();
133 foreach (object element in (IEnumerable)value) {
134 result.Add((TExtensionType)SingularFromReflectionType(element));
135 }
136 return result;
137 }
138 }
139
csharptest804b6d82010-11-07 10:49:33 -0600140 public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite
141 where TContainingType : IMessageLite {
142
143 private readonly TContainingType containingTypeDefaultInstance;
144 private readonly TExtensionType defaultValue;
145 private readonly IMessageLite messageDefaultInstance;
146 private readonly ExtensionDescriptorLite descriptor;
147
148 // We can't always initialize a GeneratedExtension when we first construct
149 // it due to initialization order difficulties (namely, the default
150 // instances may not have been constructed yet). So, we construct an
151 // uninitialized GeneratedExtension once, then call internalInit() on it
152 // later. Generated code will always call internalInit() on all extensions
153 // as part of the static initialization code, and internalInit() throws an
154 // exception if called more than once, so this method is useless to users.
155 protected GeneratedExtensionLite(
156 TContainingType containingTypeDefaultInstance,
157 TExtensionType defaultValue,
158 IMessageLite messageDefaultInstance,
159 ExtensionDescriptorLite descriptor) {
160 this.containingTypeDefaultInstance = containingTypeDefaultInstance;
161 this.messageDefaultInstance = messageDefaultInstance;
162 this.defaultValue = defaultValue;
163 this.descriptor = descriptor;
164 }
165
166 /** For use by generated code only. */
167 public GeneratedExtensionLite(
168 TContainingType containingTypeDefaultInstance,
169 TExtensionType defaultValue,
170 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600171 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600172 int number,
173 FieldType type)
174 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600175 new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
csharptest804b6d82010-11-07 10:49:33 -0600176 false /* isRepeated */, false /* isPacked */)) {
177 }
178
csharptest7d396f92010-11-08 20:06:46 -0600179 private static readonly IList<object> Empty = new object[0];
csharptest487da482010-11-07 18:49:42 -0600180 /** Repeating fields: For use by generated code only. */
181 protected GeneratedExtensionLite(
csharptest804b6d82010-11-07 10:49:33 -0600182 TContainingType containingTypeDefaultInstance,
183 TExtensionType defaultValue,
184 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600185 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600186 int number,
187 FieldType type,
188 bool isPacked)
189 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest7d396f92010-11-08 20:06:46 -0600190 new ExtensionDescriptorLite(enumTypeMap, number, type, Empty,
csharptest804b6d82010-11-07 10:49:33 -0600191 true /* isRepeated */, isPacked)) {
192 }
193
194 /// <summary>
csharptest980ba8d2010-11-07 16:30:39 -0600195 /// Returns information about this extension
196 /// </summary>
197 public IFieldDescriptorLite Descriptor {
198 get { return descriptor; }
199 }
200
201 /// <summary>
202 /// Returns the default value for this extension
203 /// </summary>
204 public TExtensionType DefaultValue {
205 get { return defaultValue; }
206 }
207
208 /// <summary>
csharptest804b6d82010-11-07 10:49:33 -0600209 /// used for the extension registry
210 /// </summary>
211 object IGeneratedExtensionLite.ContainingType {
212 get { return ContainingTypeDefaultInstance; }
213 }
214 /**
215 * Default instance of the type being extended, used to identify that type.
216 */
217 public TContainingType ContainingTypeDefaultInstance {
218 get {
219 return containingTypeDefaultInstance;
220 }
221 }
222
223 /** Get the field number. */
224 public int Number {
225 get {
csharptest980ba8d2010-11-07 16:30:39 -0600226 return descriptor.FieldNumber;
csharptest804b6d82010-11-07 10:49:33 -0600227 }
228 }
229 /**
230 * If the extension is an embedded message, this is the default instance of
231 * that type.
232 */
233 public IMessageLite MessageDefaultInstance {
234 get {
235 return messageDefaultInstance;
236 }
237 }
csharptest980ba8d2010-11-07 16:30:39 -0600238
csharptest80824a52010-11-07 17:25:47 -0600239 /// <summary>
240 /// Converts from the type used by the native accessors to the type
241 /// used by reflection accessors. For example, the reflection accessors
242 /// for enums use EnumValueDescriptors but the native accessors use
243 /// the generated enum type.
244 /// </summary>
csharptest487da482010-11-07 18:49:42 -0600245 public virtual object ToReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600246 return SingularToReflectionType(value);
csharptest80824a52010-11-07 17:25:47 -0600247 }
248
249 /// <summary>
250 /// Like ToReflectionType(object) but for a single element.
251 /// </summary>
csharptest487da482010-11-07 18:49:42 -0600252 public object SingularToReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600253 return descriptor.MappedType == MappedType.Enum
254 ? descriptor.EnumType.FindValueByNumber((int)value)
255 : value;
256 }
257
csharptest487da482010-11-07 18:49:42 -0600258 public virtual object FromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600259 return SingularFromReflectionType(value);
csharptest80824a52010-11-07 17:25:47 -0600260 }
261
csharptest980ba8d2010-11-07 16:30:39 -0600262 public object SingularFromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600263 switch (Descriptor.MappedType) {
264 case MappedType.Message:
265 if (value is TExtensionType) {
266 return value;
267 } else {
268 // It seems the copy of the embedded message stored inside the
269 // extended message is not of the exact type the user was
270 // expecting. This can happen if a user defines a
271 // GeneratedExtension manually and gives it a different type.
272 // This should not happen in normal use. But, to be nice, we'll
273 // copy the message to whatever type the caller was expecting.
274 return MessageDefaultInstance.WeakCreateBuilderForType()
275 .WeakMergeFrom((IMessageLite)value).WeakBuild();
276 }
277 case MappedType.Enum:
278 // Just return a boxed int - that can be unboxed to the enum
279 IEnumLite enumValue = (IEnumLite)value;
280 return enumValue.Number;
281 default:
282 return value;
283 }
csharptest980ba8d2010-11-07 16:30:39 -0600284 }
csharptest804b6d82010-11-07 10:49:33 -0600285 }
csharptestd9c59e62010-11-04 19:36:28 -0500286}