Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/GeneratedExtensionLite.cs @ 9503

Last change on this file since 9503 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 12.8 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System.Collections;
38using System.Collections.Generic;
39using Google.ProtocolBuffers.Descriptors;
40
41namespace Google.ProtocolBuffers
42{
43    public interface IGeneratedExtensionLite
44    {
45        int Number { get; }
46        object ContainingType { get; }
47        IMessageLite MessageDefaultInstance { get; }
48        IFieldDescriptorLite Descriptor { get; }
49    }
50
51    public class ExtensionDescriptorLite : IFieldDescriptorLite
52    {
53        private readonly string fullName;
54        private readonly IEnumLiteMap enumTypeMap;
55        private readonly int number;
56        private readonly FieldType type;
57        private readonly bool isRepeated;
58        private readonly bool isPacked;
59        private readonly MappedType mapType;
60        private readonly object defaultValue;
61
62        public ExtensionDescriptorLite(string fullName, IEnumLiteMap enumTypeMap, int number, FieldType type,
63                                       object defaultValue, bool isRepeated, bool isPacked)
64        {
65            this.fullName = fullName;
66            this.enumTypeMap = enumTypeMap;
67            this.number = number;
68            this.type = type;
69            this.mapType = FieldMappingAttribute.MappedTypeFromFieldType(type);
70            this.isRepeated = isRepeated;
71            this.isPacked = isPacked;
72            this.defaultValue = defaultValue;
73        }
74
75        public string Name
76        {
77            get
78            {
79                string name = fullName;
80                int offset = name.LastIndexOf('.');
81                if (offset >= 0)
82                {
83                    name = name.Substring(offset);
84                }
85                return name;
86            }
87        }
88
89        public string FullName
90        {
91            get { return fullName; }
92        }
93
94        public bool IsRepeated
95        {
96            get { return isRepeated; }
97        }
98
99        public bool IsRequired
100        {
101            get { return false; }
102        }
103
104        public bool IsPacked
105        {
106            get { return isPacked; }
107        }
108
109        public bool IsExtension
110        {
111            get { return true; }
112        }
113
114        /// <summary>
115        /// This is not supported and assertions are made to ensure this does not exist on extensions of Lite types
116        /// </summary>
117        public bool MessageSetWireFormat
118        {
119            get { return false; }
120        }
121
122        public int FieldNumber
123        {
124            get { return number; }
125        }
126
127        public IEnumLiteMap EnumType
128        {
129            get { return enumTypeMap; }
130        }
131
132        public FieldType FieldType
133        {
134            get { return type; }
135        }
136
137        public MappedType MappedType
138        {
139            get { return mapType; }
140        }
141
142        public object DefaultValue
143        {
144            get { return defaultValue; }
145        }
146
147        public int CompareTo(IFieldDescriptorLite other)
148        {
149            return FieldNumber.CompareTo(other.FieldNumber);
150        }
151    }
152
153    public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> :
154        GeneratedExtensionLite<TContainingType, IList<TExtensionType>>
155        where TContainingType : IMessageLite
156    {
157        public GeneratedRepeatExtensionLite(string fullName, TContainingType containingTypeDefaultInstance,
158                                            IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number,
159                                            FieldType type, bool isPacked) :
160                                                base(
161                                                fullName, containingTypeDefaultInstance, new List<TExtensionType>(),
162                                                messageDefaultInstance, enumTypeMap, number, type, isPacked)
163        {
164        }
165
166        public override object ToReflectionType(object value)
167        {
168            IList<object> result = new List<object>();
169            foreach (object element in (IEnumerable) value)
170            {
171                result.Add(SingularToReflectionType(element));
172            }
173            return result;
174        }
175
176        public override object FromReflectionType(object value)
177        {
178            // Must convert the whole list.
179            List<TExtensionType> result = new List<TExtensionType>();
180            foreach (object element in (IEnumerable) value)
181            {
182                result.Add((TExtensionType) SingularFromReflectionType(element));
183            }
184            return result;
185        }
186    }
187
188    public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite
189        where TContainingType : IMessageLite
190    {
191        private readonly TContainingType containingTypeDefaultInstance;
192        private readonly TExtensionType defaultValue;
193        private readonly IMessageLite messageDefaultInstance;
194        private readonly ExtensionDescriptorLite descriptor;
195
196        // We can't always initialize a GeneratedExtension when we first construct
197        // it due to initialization order difficulties (namely, the default
198        // instances may not have been constructed yet).  So, we construct an
199        // uninitialized GeneratedExtension once, then call internalInit() on it
200        // later.  Generated code will always call internalInit() on all extensions
201        // as part of the static initialization code, and internalInit() throws an
202        // exception if called more than once, so this method is useless to users.
203        protected GeneratedExtensionLite(
204            TContainingType containingTypeDefaultInstance,
205            TExtensionType defaultValue,
206            IMessageLite messageDefaultInstance,
207            ExtensionDescriptorLite descriptor)
208        {
209            this.containingTypeDefaultInstance = containingTypeDefaultInstance;
210            this.messageDefaultInstance = messageDefaultInstance;
211            this.defaultValue = defaultValue;
212            this.descriptor = descriptor;
213        }
214
215        /** For use by generated code only. */
216
217        public GeneratedExtensionLite(
218            string fullName,
219            TContainingType containingTypeDefaultInstance,
220            TExtensionType defaultValue,
221            IMessageLite messageDefaultInstance,
222            IEnumLiteMap enumTypeMap,
223            int number,
224            FieldType type)
225            : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
226                   new ExtensionDescriptorLite(fullName, enumTypeMap, number, type, defaultValue,
227                                               false /* isRepeated */, false /* isPacked */))
228        {
229        }
230
231        private static readonly IList<object> Empty = new object[0];
232        /** Repeating fields: For use by generated code only. */
233
234        protected GeneratedExtensionLite(
235            string fullName,
236            TContainingType containingTypeDefaultInstance,
237            TExtensionType defaultValue,
238            IMessageLite messageDefaultInstance,
239            IEnumLiteMap enumTypeMap,
240            int number,
241            FieldType type,
242            bool isPacked)
243            : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
244                   new ExtensionDescriptorLite(fullName, enumTypeMap, number, type, Empty,
245                                               true /* isRepeated */, isPacked))
246        {
247        }
248
249        /// <summary>
250        /// Returns information about this extension
251        /// </summary>
252        public IFieldDescriptorLite Descriptor
253        {
254            get { return descriptor; }
255        }
256
257        /// <summary>
258        /// Returns the default value for this extension
259        /// </summary>
260        public TExtensionType DefaultValue
261        {
262            get { return defaultValue; }
263        }
264
265        /// <summary>
266        /// used for the extension registry
267        /// </summary>
268        object IGeneratedExtensionLite.ContainingType
269        {
270            get { return ContainingTypeDefaultInstance; }
271        }
272
273        /**
274     * Default instance of the type being extended, used to identify that type.
275     */
276
277        public TContainingType ContainingTypeDefaultInstance
278        {
279            get { return containingTypeDefaultInstance; }
280        }
281
282        /** Get the field number. */
283
284        public int Number
285        {
286            get { return descriptor.FieldNumber; }
287        }
288
289        /**
290     * If the extension is an embedded message, this is the default instance of
291     * that type.
292     */
293
294        public IMessageLite MessageDefaultInstance
295        {
296            get { return messageDefaultInstance; }
297        }
298
299        /// <summary>
300        /// Converts from the type used by the native accessors to the type
301        /// used by reflection accessors. For example, the reflection accessors
302        /// for enums use EnumValueDescriptors but the native accessors use
303        /// the generated enum type.
304        /// </summary>
305        public virtual object ToReflectionType(object value)
306        {
307            return SingularToReflectionType(value);
308        }
309
310        /// <summary>
311        /// Like ToReflectionType(object) but for a single element.
312        /// </summary>
313        public object SingularToReflectionType(object value)
314        {
315            return descriptor.MappedType == MappedType.Enum
316                       ? descriptor.EnumType.FindValueByNumber((int) value)
317                       : value;
318        }
319
320        public virtual object FromReflectionType(object value)
321        {
322            return SingularFromReflectionType(value);
323        }
324
325        public object SingularFromReflectionType(object value)
326        {
327            switch (Descriptor.MappedType)
328            {
329                case MappedType.Message:
330                    if (value is TExtensionType)
331                    {
332                        return value;
333                    }
334                    else
335                    {
336                        // It seems the copy of the embedded message stored inside the
337                        // extended message is not of the exact type the user was
338                        // expecting.  This can happen if a user defines a
339                        // GeneratedExtension manually and gives it a different type.
340                        // This should not happen in normal use.  But, to be nice, we'll
341                        // copy the message to whatever type the caller was expecting.
342                        return MessageDefaultInstance.WeakCreateBuilderForType()
343                            .WeakMergeFrom((IMessageLite) value).WeakBuild();
344                    }
345                case MappedType.Enum:
346                    // Just return a boxed int - that can be unboxed to the enum
347                    IEnumLite enumValue = (IEnumLite) value;
348                    return enumValue.Number;
349                default:
350                    return value;
351            }
352        }
353    }
354}
Note: See TracBrowser for help on using the repository browser.