Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/GeneratedExtensionBase.cs

Last change on this file 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: 7.3 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;
38using System.Collections;
39using System.Collections.Generic;
40using System.Reflection;
41using Google.ProtocolBuffers.Descriptors;
42
43namespace Google.ProtocolBuffers
44{
45    /// <summary>
46    /// Base type for all generated extensions.
47    /// </summary>
48    /// <remarks>
49    /// The protocol compiler generates a static singleton instance of this
50    /// class for each extension. For exmaple, imagine a .proto file with:
51    /// <code>
52    /// message Foo {
53    ///   extensions 1000 to max
54    /// }
55    ///
56    /// extend Foo {
57    ///   optional int32 bar;
58    /// }
59    /// </code>
60    /// Then MyProto.Foo.Bar has type GeneratedExtensionBase&lt;MyProto.Foo,int&gt;.
61    /// <para />
62    /// In general, users should ignore the details of this type, and
63    /// simply use the static singletons as parameters to the extension accessors
64    /// in ExtendableMessage and ExtendableBuilder.
65    /// The interface implemented by both GeneratedException and GeneratedRepeatException,
66    /// to make it easier to cope with repeats separately.
67    /// </remarks>
68    public abstract class GeneratedExtensionBase<TExtension>
69    {
70        private readonly FieldDescriptor descriptor;
71        private readonly IMessageLite messageDefaultInstance;
72
73        protected GeneratedExtensionBase(FieldDescriptor descriptor, Type singularExtensionType)
74        {
75            if (!descriptor.IsExtension)
76            {
77                throw new ArgumentException("GeneratedExtension given a regular (non-extension) field.");
78            }
79
80            this.descriptor = descriptor;
81            if (descriptor.MappedType == MappedType.Message)
82            {
83                PropertyInfo defaultInstanceProperty = singularExtensionType
84                    .GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public);
85                if (defaultInstanceProperty == null)
86                {
87                    throw new ArgumentException("No public static DefaultInstance property for type " +
88                                                typeof (TExtension).Name);
89                }
90
91                messageDefaultInstance = (IMessageLite) defaultInstanceProperty.GetValue(null, null);
92            }
93        }
94
95        public FieldDescriptor Descriptor
96        {
97            get { return descriptor; }
98        }
99
100        public int Number
101        {
102            get { return Descriptor.FieldNumber; }
103        }
104
105        /// <summary>
106        /// Returns the default message instance for extensions which are message types.
107        /// </summary>
108        public IMessageLite MessageDefaultInstance
109        {
110            get { return messageDefaultInstance; }
111        }
112
113        public object SingularFromReflectionType(object value)
114        {
115            switch (Descriptor.MappedType)
116            {
117                case MappedType.Message:
118                    if (value is TExtension)
119                    {
120                        return value;
121                    }
122                    else
123                    {
124                        // It seems the copy of the embedded message stored inside the
125                        // extended message is not of the exact type the user was
126                        // expecting.  This can happen if a user defines a
127                        // GeneratedExtension manually and gives it a different type.
128                        // This should not happen in normal use.  But, to be nice, we'll
129                        // copy the message to whatever type the caller was expecting.
130                        return MessageDefaultInstance.WeakCreateBuilderForType()
131                            .WeakMergeFrom((IMessageLite) value).WeakBuild();
132                    }
133                case MappedType.Enum:
134                    // Just return a boxed int - that can be unboxed to the enum
135                    EnumValueDescriptor enumValue = (EnumValueDescriptor) value;
136                    return enumValue.Number;
137                default:
138                    return value;
139            }
140        }
141
142        /// <summary>
143        /// Converts from the type used by the native accessors to the type
144        /// used by reflection accessors. For example, the reflection accessors
145        /// for enums use EnumValueDescriptors but the native accessors use
146        /// the generated enum type.
147        /// </summary>
148        public object ToReflectionType(object value)
149        {
150            if (descriptor.IsRepeated)
151            {
152                if (descriptor.MappedType == MappedType.Enum)
153                {
154                    // Must convert the whole list.
155                    IList<object> result = new List<object>();
156                    foreach (object element in (IEnumerable) value)
157                    {
158                        result.Add(SingularToReflectionType(element));
159                    }
160                    return result;
161                }
162                else
163                {
164                    return value;
165                }
166            }
167            else
168            {
169                return SingularToReflectionType(value);
170            }
171        }
172
173        /// <summary>
174        /// Like ToReflectionType(object) but for a single element.
175        /// </summary>
176        internal Object SingularToReflectionType(object value)
177        {
178            return descriptor.MappedType == MappedType.Enum
179                       ? descriptor.EnumType.FindValueByNumber((int) value)
180                       : value;
181        }
182
183        public abstract object FromReflectionType(object value);
184    }
185}
Note: See TracBrowser for help on using the repository browser.