blob: 813f69cf97d66885daedaa11654d070a58ba2deb [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeet60c059b2008-10-23 21:17:56 +01002// 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:
Jon Skeet68036862008-10-22 13:30:34 +01006// http://code.google.com/p/protobuf/
7//
Jon Skeet60c059b2008-10-23 21:17:56 +01008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
Jon Skeet68036862008-10-22 13:30:34 +010011//
Jon Skeet60c059b2008-10-23 21:17:56 +010012// * 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.
Jon Skeet68036862008-10-22 13:30:34 +010021//
Jon Skeet60c059b2008-10-23 21:17:56 +010022// 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.
Jon Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet68036862008-10-22 13:30:34 +010035using System;
36using System.Collections;
37using System.Collections.Generic;
38using System.Reflection;
39using Google.ProtocolBuffers.Descriptors;
40
41namespace Google.ProtocolBuffers {
42 /// <summary>
43 /// Base type for all generated extensions.
44 /// </summary>
45 /// <remarks>
46 /// The protocol compiler generates a static singleton instance of this
47 /// class for each extension. For exmaple, imagine a .proto file with:
48 /// <code>
49 /// message Foo {
50 /// extensions 1000 to max
51 /// }
52 ///
53 /// extend Foo {
54 /// optional int32 bar;
55 /// }
56 /// </code>
57 /// Then MyProto.Foo.Bar has type GeneratedExtensionBase&lt;MyProto.Foo,int&gt;.
58 /// <para />
59 /// In general, users should ignore the details of this type, and
60 /// simply use the static singletons as parameters to the extension accessors
61 /// in ExtendableMessage and ExtendableBuilder.
62 /// The interface implemented by both GeneratedException and GeneratedRepeatException,
63 /// to make it easier to cope with repeats separately.
64 /// </remarks>
65 public abstract class GeneratedExtensionBase<TExtension> {
66
67 private readonly FieldDescriptor descriptor;
68 private readonly IMessage messageDefaultInstance;
69
70 protected GeneratedExtensionBase(FieldDescriptor descriptor, Type singularExtensionType) {
71 if (!descriptor.IsExtension) {
72 throw new ArgumentException("GeneratedExtension given a regular (non-extension) field.");
73 }
74
75 this.descriptor = descriptor;
76 if (descriptor.MappedType == MappedType.Message) {
77 PropertyInfo defaultInstanceProperty = singularExtensionType
78 .GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public);
79 if (defaultInstanceProperty == null) {
80 throw new ArgumentException("No public static DefaultInstance property for type " + typeof(TExtension).Name);
81 }
82 messageDefaultInstance = (IMessage)defaultInstanceProperty.GetValue(null, null);
83 }
84 }
85
86 public FieldDescriptor Descriptor {
87 get { return descriptor; }
88 }
89
90 /// <summary>
91 /// Returns the default message instance for extensions which are message types.
92 /// </summary>
93 public IMessage MessageDefaultInstance {
94 get { return messageDefaultInstance; }
95 }
96
97 public object SingularFromReflectionType(object value) {
98 switch (Descriptor.MappedType) {
99 case MappedType.Message:
100 if (value is TExtension) {
101 return value;
102 } else {
103 // It seems the copy of the embedded message stored inside the
104 // extended message is not of the exact type the user was
105 // expecting. This can happen if a user defines a
106 // GeneratedExtension manually and gives it a different type.
107 // This should not happen in normal use. But, to be nice, we'll
108 // copy the message to whatever type the caller was expecting.
109 return MessageDefaultInstance.WeakCreateBuilderForType()
110 .WeakMergeFrom((IMessage)value).WeakBuild();
111 }
112 case MappedType.Enum:
113 // Just return a boxed int - that can be unboxed to the enum
114 EnumValueDescriptor enumValue = (EnumValueDescriptor) value;
115 return enumValue.Number;
116 default:
117 return value;
118 }
119 }
120
121 /// <summary>
122 /// Converts from the type used by the native accessors to the type
123 /// used by reflection accessors. For example, the reflection accessors
124 /// for enums use EnumValueDescriptors but the native accessors use
125 /// the generated enum type.
126 /// </summary>
127 public object ToReflectionType(object value) {
128 if (descriptor.IsRepeated) {
129 if (descriptor.MappedType == MappedType.Enum) {
130 // Must convert the whole list.
131 IList<object> result = new List<object>();
132 foreach (object element in (IEnumerable) value) {
133 result.Add(SingularToReflectionType(element));
134 }
135 return result;
136 } else {
137 return value;
138 }
139 } else {
140 return SingularToReflectionType(value);
141 }
142 }
143
144 /// <summary>
145 /// Like ToReflectionType(object) but for a single element.
146 /// </summary>
147 internal Object SingularToReflectionType(object value) {
148 return descriptor.MappedType == MappedType.Enum
149 ? descriptor.EnumType.FindValueByNumber((int) value)
150 : value;
151 }
152
153 public abstract object FromReflectionType(object value);
154 }
155}