Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/ExtendableBuilderLite.cs @ 11211

Last change on this file since 11211 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: 14.1 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.Generic;
39using Google.ProtocolBuffers.Descriptors;
40
41namespace Google.ProtocolBuffers
42{
43    public abstract partial class ExtendableBuilderLite<TMessage, TBuilder> : GeneratedBuilderLite<TMessage, TBuilder>
44        where TMessage : ExtendableMessageLite<TMessage, TBuilder>
45        where TBuilder : GeneratedBuilderLite<TMessage, TBuilder>
46    {
47        protected ExtendableBuilderLite()
48        {
49        }
50
51        /// <summary>
52        /// Checks if a singular extension is present
53        /// </summary>
54        public bool HasExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
55        {
56            return MessageBeingBuilt.HasExtension(extension);
57        }
58
59        /// <summary>
60        /// Returns the number of elements in a repeated extension.
61        /// </summary>
62        public int GetExtensionCount<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension)
63        {
64            return MessageBeingBuilt.GetExtensionCount(extension);
65        }
66
67        /// <summary>
68        /// Returns the value of an extension.
69        /// </summary>
70        public TExtension GetExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
71        {
72            return MessageBeingBuilt.GetExtension(extension);
73        }
74
75        /// <summary>
76        /// Returns one element of a repeated extension.
77        /// </summary>
78        public TExtension GetExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
79                                                   int index)
80        {
81            return MessageBeingBuilt.GetExtension(extension, index);
82        }
83
84        /// <summary>
85        /// Sets the value of an extension.
86        /// </summary>
87        public TBuilder SetExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension,
88                                                 TExtension value)
89        {
90            ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
91            message.VerifyExtensionContainingType(extension);
92            message.Extensions[extension.Descriptor] = extension.ToReflectionType(value);
93            return ThisBuilder;
94        }
95
96        /// <summary>
97        /// Sets the value of one element of a repeated extension.
98        /// </summary>
99        public TBuilder SetExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
100                                                 int index, TExtension value)
101        {
102            ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
103            message.VerifyExtensionContainingType(extension);
104            message.Extensions[extension.Descriptor, index] = extension.SingularToReflectionType(value);
105            return ThisBuilder;
106        }
107
108        /// <summary>
109        /// Appends a value to a repeated extension.
110        /// </summary>
111        public TBuilder AddExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
112                                                 TExtension value)
113        {
114            ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
115            message.VerifyExtensionContainingType(extension);
116            message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
117            return ThisBuilder;
118        }
119
120        /// <summary>
121        /// Clears an extension.
122        /// </summary>
123        public TBuilder ClearExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
124        {
125            ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
126            message.VerifyExtensionContainingType(extension);
127            message.Extensions.ClearField(extension.Descriptor);
128            return ThisBuilder;
129        }
130
131        /// <summary>
132        /// Called by subclasses to parse an unknown field or an extension.
133        /// </summary>
134        /// <returns>true unless the tag is an end-group tag</returns>
135        [CLSCompliant(false)]
136        protected override bool ParseUnknownField(ICodedInputStream input,
137                                                  ExtensionRegistry extensionRegistry, uint tag, string fieldName)
138        {
139            FieldSet extensions = MessageBeingBuilt.Extensions;
140
141            WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
142            int fieldNumber = WireFormat.GetTagFieldNumber(tag);
143            IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];
144
145            if (extension == null) //unknown field
146            {
147                return input.SkipField();
148            }
149
150            IFieldDescriptorLite field = extension.Descriptor;
151
152
153            // Unknown field or wrong wire type. Skip.
154            if (field == null)
155            {
156                return input.SkipField();
157            }
158            WireFormat.WireType expectedType = field.IsPacked
159                                                   ? WireFormat.WireType.LengthDelimited
160                                                   : WireFormat.GetWireType(field.FieldType);
161            if (wireType != expectedType)
162            {
163                expectedType = WireFormat.GetWireType(field.FieldType);
164                if (wireType == expectedType)
165                {
166                    //Allowed as of 2.3, this is unpacked data for a packed array
167                }
168                else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited &&
169                         (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 ||
170                          expectedType == WireFormat.WireType.Fixed64))
171                {
172                    //Allowed as of 2.3, this is packed data for an unpacked array
173                }
174                else
175                {
176                    return input.SkipField();
177                }
178            }
179            if (!field.IsRepeated && wireType != WireFormat.GetWireType(field.FieldType)) //invalid wire type
180            {
181                return input.SkipField();
182            }
183
184            switch (field.FieldType)
185            {
186                case FieldType.Group:
187                case FieldType.Message:
188                    {
189                        if (!field.IsRepeated)
190                        {
191                            IMessageLite message = extensions[extension.Descriptor] as IMessageLite;
192                            IBuilderLite subBuilder = (message ?? extension.MessageDefaultInstance).WeakToBuilder();
193
194                            if (field.FieldType == FieldType.Group)
195                            {
196                                input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
197                            }
198                            else
199                            {
200                                input.ReadMessage(subBuilder, extensionRegistry);
201                            }
202
203                            extensions[field] = subBuilder.WeakBuild();
204                        }
205                        else
206                        {
207                            List<IMessageLite> list = new List<IMessageLite>();
208                            if (field.FieldType == FieldType.Group)
209                            {
210                                input.ReadGroupArray(tag, fieldName, list, extension.MessageDefaultInstance,
211                                                     extensionRegistry);
212                            }
213                            else
214                            {
215                                input.ReadMessageArray(tag, fieldName, list, extension.MessageDefaultInstance,
216                                                       extensionRegistry);
217                            }
218
219                            foreach (IMessageLite m in list)
220                            {
221                                extensions.AddRepeatedField(field, m);
222                            }
223                            return true;
224                        }
225                        break;
226                    }
227                case FieldType.Enum:
228                    {
229                        if (!field.IsRepeated)
230                        {
231                            object unknown;
232                            IEnumLite value = null;
233                            if (input.ReadEnum(ref value, out unknown, field.EnumType))
234                            {
235                                extensions[field] = value;
236                            }
237                        }
238                        else
239                        {
240                            ICollection<object> unknown;
241                            List<IEnumLite> list = new List<IEnumLite>();
242                            input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType);
243
244                            foreach (IEnumLite en in list)
245                            {
246                                extensions.AddRepeatedField(field, en);
247                            }
248                        }
249                        break;
250                    }
251                default:
252                    {
253                        if (!field.IsRepeated)
254                        {
255                            object value = null;
256                            if (input.ReadPrimitiveField(field.FieldType, ref value))
257                            {
258                                extensions[field] = value;
259                            }
260                        }
261                        else
262                        {
263                            List<object> list = new List<object>();
264                            input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list);
265                            foreach (object oval in list)
266                            {
267                                extensions.AddRepeatedField(field, oval);
268                            }
269                        }
270                        break;
271                    }
272            }
273
274            return true;
275        }
276
277        #region Reflection
278
279        public object this[IFieldDescriptorLite field, int index]
280        {
281            set
282            {
283                if (field.IsExtension)
284                {
285                    ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
286                    message.Extensions[field, index] = value;
287                }
288                else
289                {
290                    throw new NotSupportedException("Not supported in the lite runtime.");
291                }
292            }
293        }
294
295        public object this[IFieldDescriptorLite field]
296        {
297            set
298            {
299                if (field.IsExtension)
300                {
301                    ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
302                    message.Extensions[field] = value;
303                }
304                else
305                {
306                    throw new NotSupportedException("Not supported in the lite runtime.");
307                }
308            }
309        }
310
311        public TBuilder ClearField(IFieldDescriptorLite field)
312        {
313            if (field.IsExtension)
314            {
315                ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
316                message.Extensions.ClearField(field);
317                return ThisBuilder;
318            }
319            else
320            {
321                throw new NotSupportedException("Not supported in the lite runtime.");
322            }
323        }
324
325        public TBuilder AddRepeatedField(IFieldDescriptorLite field, object value)
326        {
327            if (field.IsExtension)
328            {
329                ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
330                message.Extensions.AddRepeatedField(field, value);
331                return ThisBuilder;
332            }
333            else
334            {
335                throw new NotSupportedException("Not supported in the lite runtime.");
336            }
337        }
338
339        protected void MergeExtensionFields(ExtendableMessageLite<TMessage, TBuilder> other)
340        {
341            MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
342        }
343
344        #endregion
345    }
346}
Note: See TracBrowser for help on using the repository browser.