blob: 81d8d41bd608d11e1ef6ed37e87691e9e8b5b0eb [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
csharpteste4954772010-11-09 14:47:27 -060085 /// <summary>
86 /// This is not supported and assertions are made to ensure this does not exist on extensions of Lite types
87 /// </summary>
csharptest980ba8d2010-11-07 16:30:39 -060088 public bool MessageSetWireFormat {
csharptest7d396f92010-11-08 20:06:46 -060089 get { return false; }
csharptest980ba8d2010-11-07 16:30:39 -060090 }
91
92 public int FieldNumber {
csharptest804b6d82010-11-07 10:49:33 -060093 get { return number; }
94 }
csharptest980ba8d2010-11-07 16:30:39 -060095
96 public IEnumLiteMap EnumType {
97 get { return enumTypeMap; }
98 }
99
100 public FieldType FieldType {
101 get { return type; }
102 }
103
104 public MappedType MappedType {
105 get { return mapType; }
106 }
107
108 public object DefaultValue {
109 get { return defaultValue; }
110 }
111
112 public int CompareTo(IFieldDescriptorLite other) {
113 return FieldNumber.CompareTo(other.FieldNumber);
114 }
csharptest804b6d82010-11-07 10:49:33 -0600115 }
csharptest487da482010-11-07 18:49:42 -0600116
117 public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> : GeneratedExtensionLite<TContainingType, IList<TExtensionType>>
118 where TContainingType : IMessageLite {
119 public GeneratedRepeatExtensionLite(TContainingType containingTypeDefaultInstance,
120 IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number, FieldType type, bool isPacked) :
121 base(containingTypeDefaultInstance, new List<TExtensionType>(), messageDefaultInstance, enumTypeMap, number, type, isPacked) {
122 }
123
124 public override object ToReflectionType(object value) {
csharptest7d396f92010-11-08 20:06:46 -0600125 IList<object> result = new List<object>();
126 foreach (object element in (IEnumerable) value) {
127 result.Add(SingularToReflectionType(element));
128 }
129 return result;
csharptest487da482010-11-07 18:49:42 -0600130 }
131
132 public override object FromReflectionType(object value) {
133 // Must convert the whole list.
134 List<TExtensionType> result = new List<TExtensionType>();
135 foreach (object element in (IEnumerable)value) {
136 result.Add((TExtensionType)SingularFromReflectionType(element));
137 }
138 return result;
139 }
140 }
141
csharptest804b6d82010-11-07 10:49:33 -0600142 public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite
143 where TContainingType : IMessageLite {
144
145 private readonly TContainingType containingTypeDefaultInstance;
146 private readonly TExtensionType defaultValue;
147 private readonly IMessageLite messageDefaultInstance;
148 private readonly ExtensionDescriptorLite descriptor;
149
150 // We can't always initialize a GeneratedExtension when we first construct
151 // it due to initialization order difficulties (namely, the default
152 // instances may not have been constructed yet). So, we construct an
153 // uninitialized GeneratedExtension once, then call internalInit() on it
154 // later. Generated code will always call internalInit() on all extensions
155 // as part of the static initialization code, and internalInit() throws an
156 // exception if called more than once, so this method is useless to users.
157 protected GeneratedExtensionLite(
158 TContainingType containingTypeDefaultInstance,
159 TExtensionType defaultValue,
160 IMessageLite messageDefaultInstance,
161 ExtensionDescriptorLite descriptor) {
162 this.containingTypeDefaultInstance = containingTypeDefaultInstance;
163 this.messageDefaultInstance = messageDefaultInstance;
164 this.defaultValue = defaultValue;
165 this.descriptor = descriptor;
166 }
167
168 /** For use by generated code only. */
169 public GeneratedExtensionLite(
170 TContainingType containingTypeDefaultInstance,
171 TExtensionType defaultValue,
172 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600173 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600174 int number,
175 FieldType type)
176 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600177 new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
csharptest804b6d82010-11-07 10:49:33 -0600178 false /* isRepeated */, false /* isPacked */)) {
179 }
180
csharptest7d396f92010-11-08 20:06:46 -0600181 private static readonly IList<object> Empty = new object[0];
csharptest487da482010-11-07 18:49:42 -0600182 /** Repeating fields: For use by generated code only. */
183 protected GeneratedExtensionLite(
csharptest804b6d82010-11-07 10:49:33 -0600184 TContainingType containingTypeDefaultInstance,
185 TExtensionType defaultValue,
186 IMessageLite messageDefaultInstance,
csharptest980ba8d2010-11-07 16:30:39 -0600187 IEnumLiteMap enumTypeMap,
csharptest804b6d82010-11-07 10:49:33 -0600188 int number,
189 FieldType type,
190 bool isPacked)
191 : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
csharptest7d396f92010-11-08 20:06:46 -0600192 new ExtensionDescriptorLite(enumTypeMap, number, type, Empty,
csharptest804b6d82010-11-07 10:49:33 -0600193 true /* isRepeated */, isPacked)) {
194 }
195
196 /// <summary>
csharptest980ba8d2010-11-07 16:30:39 -0600197 /// Returns information about this extension
198 /// </summary>
199 public IFieldDescriptorLite Descriptor {
200 get { return descriptor; }
201 }
202
203 /// <summary>
204 /// Returns the default value for this extension
205 /// </summary>
206 public TExtensionType DefaultValue {
207 get { return defaultValue; }
208 }
209
210 /// <summary>
csharptest804b6d82010-11-07 10:49:33 -0600211 /// used for the extension registry
212 /// </summary>
213 object IGeneratedExtensionLite.ContainingType {
214 get { return ContainingTypeDefaultInstance; }
215 }
216 /**
217 * Default instance of the type being extended, used to identify that type.
218 */
219 public TContainingType ContainingTypeDefaultInstance {
220 get {
221 return containingTypeDefaultInstance;
222 }
223 }
224
225 /** Get the field number. */
226 public int Number {
227 get {
csharptest980ba8d2010-11-07 16:30:39 -0600228 return descriptor.FieldNumber;
csharptest804b6d82010-11-07 10:49:33 -0600229 }
230 }
231 /**
232 * If the extension is an embedded message, this is the default instance of
233 * that type.
234 */
235 public IMessageLite MessageDefaultInstance {
236 get {
237 return messageDefaultInstance;
238 }
239 }
csharptest980ba8d2010-11-07 16:30:39 -0600240
csharptest80824a52010-11-07 17:25:47 -0600241 /// <summary>
242 /// Converts from the type used by the native accessors to the type
243 /// used by reflection accessors. For example, the reflection accessors
244 /// for enums use EnumValueDescriptors but the native accessors use
245 /// the generated enum type.
246 /// </summary>
csharptest487da482010-11-07 18:49:42 -0600247 public virtual object ToReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600248 return SingularToReflectionType(value);
csharptest80824a52010-11-07 17:25:47 -0600249 }
250
251 /// <summary>
252 /// Like ToReflectionType(object) but for a single element.
253 /// </summary>
csharptest487da482010-11-07 18:49:42 -0600254 public object SingularToReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600255 return descriptor.MappedType == MappedType.Enum
256 ? descriptor.EnumType.FindValueByNumber((int)value)
257 : value;
258 }
259
csharptest487da482010-11-07 18:49:42 -0600260 public virtual object FromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600261 return SingularFromReflectionType(value);
csharptest80824a52010-11-07 17:25:47 -0600262 }
263
csharptest980ba8d2010-11-07 16:30:39 -0600264 public object SingularFromReflectionType(object value) {
csharptest80824a52010-11-07 17:25:47 -0600265 switch (Descriptor.MappedType) {
266 case MappedType.Message:
267 if (value is TExtensionType) {
268 return value;
269 } else {
270 // It seems the copy of the embedded message stored inside the
271 // extended message is not of the exact type the user was
272 // expecting. This can happen if a user defines a
273 // GeneratedExtension manually and gives it a different type.
274 // This should not happen in normal use. But, to be nice, we'll
275 // copy the message to whatever type the caller was expecting.
276 return MessageDefaultInstance.WeakCreateBuilderForType()
277 .WeakMergeFrom((IMessageLite)value).WeakBuild();
278 }
279 case MappedType.Enum:
280 // Just return a boxed int - that can be unboxed to the enum
281 IEnumLite enumValue = (IEnumLite)value;
282 return enumValue.Number;
283 default:
284 return value;
285 }
csharptest980ba8d2010-11-07 16:30:39 -0600286 }
csharptest804b6d82010-11-07 10:49:33 -0600287 }
csharptestd9c59e62010-11-04 19:36:28 -0500288}