Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/UnknownField.cs @ 16605

Last change on this file since 16605 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: 16.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 System.Collections.ObjectModel;
40using Google.ProtocolBuffers.Collections;
41
42namespace Google.ProtocolBuffers
43{
44    /// <summary>
45    /// Represents a single field in an UnknownFieldSet.
46    ///
47    /// An UnknownField consists of five lists of values. The lists correspond
48    /// to the five "wire types" used in the protocol buffer binary format.
49    /// The wire type of each field can be determined from the encoded form alone,
50    /// without knowing the field's declared type. So, we are able to parse
51    /// unknown values at least this far and separate them. Normally, only one
52    /// of the five lists will contain any values, since it is impossible to
53    /// define a valid message type that declares two different types for the
54    /// same field number. However, the code is designed to allow for the case
55    /// where the same unknown field number is encountered using multiple different
56    /// wire types.
57    ///
58    /// UnknownField is an immutable class. To construct one, you must use an
59    /// UnknownField.Builder.
60    /// </summary>
61    public sealed class UnknownField
62    {
63        public const string UnknownFieldName = "unknown_field";
64        private static readonly UnknownField defaultInstance = CreateBuilder().Build();
65        private readonly ReadOnlyCollection<ulong> varintList;
66        private readonly ReadOnlyCollection<uint> fixed32List;
67        private readonly ReadOnlyCollection<ulong> fixed64List;
68        private readonly ReadOnlyCollection<ByteString> lengthDelimitedList;
69        private readonly ReadOnlyCollection<UnknownFieldSet> groupList;
70
71        private UnknownField(ReadOnlyCollection<ulong> varintList,
72                             ReadOnlyCollection<uint> fixed32List,
73                             ReadOnlyCollection<ulong> fixed64List,
74                             ReadOnlyCollection<ByteString> lengthDelimitedList,
75                             ReadOnlyCollection<UnknownFieldSet> groupList)
76        {
77            this.varintList = varintList;
78            this.fixed32List = fixed32List;
79            this.fixed64List = fixed64List;
80            this.lengthDelimitedList = lengthDelimitedList;
81            this.groupList = groupList;
82        }
83
84        public static UnknownField DefaultInstance
85        {
86            get { return defaultInstance; }
87        }
88
89        /// <summary>
90        /// The list of varint values for this field.
91        /// </summary>
92        public IList<ulong> VarintList
93        {
94            get { return varintList; }
95        }
96
97        /// <summary>
98        /// The list of fixed32 values for this field.
99        /// </summary>
100        public IList<uint> Fixed32List
101        {
102            get { return fixed32List; }
103        }
104
105        /// <summary>
106        /// The list of fixed64 values for this field.
107        /// </summary>
108        public IList<ulong> Fixed64List
109        {
110            get { return fixed64List; }
111        }
112
113        /// <summary>
114        /// The list of length-delimited values for this field.
115        /// </summary>
116        public IList<ByteString> LengthDelimitedList
117        {
118            get { return lengthDelimitedList; }
119        }
120
121        /// <summary>
122        /// The list of embedded group values for this field. These
123        /// are represented using UnknownFieldSets rather than Messages
124        /// since the group's type is presumably unknown.
125        /// </summary>
126        public IList<UnknownFieldSet> GroupList
127        {
128            get { return groupList; }
129        }
130
131        public override bool Equals(object other)
132        {
133            if (ReferenceEquals(this, other))
134            {
135                return true;
136            }
137            UnknownField otherField = other as UnknownField;
138            return otherField != null
139                   && Lists.Equals(varintList, otherField.varintList)
140                   && Lists.Equals(fixed32List, otherField.fixed32List)
141                   && Lists.Equals(fixed64List, otherField.fixed64List)
142                   && Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList)
143                   && Lists.Equals(groupList, otherField.groupList);
144        }
145
146        public override int GetHashCode()
147        {
148            int hash = 43;
149            hash = hash*47 + Lists.GetHashCode(varintList);
150            hash = hash*47 + Lists.GetHashCode(fixed32List);
151            hash = hash*47 + Lists.GetHashCode(fixed64List);
152            hash = hash*47 + Lists.GetHashCode(lengthDelimitedList);
153            hash = hash*47 + Lists.GetHashCode(groupList);
154            return hash;
155        }
156
157        /// <summary>
158        /// Constructs a new Builder.
159        /// </summary>
160        public static Builder CreateBuilder()
161        {
162            return new Builder();
163        }
164
165        /// <summary>
166        /// Constructs a new Builder and initializes it to a copy of <paramref name="copyFrom"/>.
167        /// </summary>
168        public static Builder CreateBuilder(UnknownField copyFrom)
169        {
170            return new Builder().MergeFrom(copyFrom);
171        }
172
173        /// <summary>
174        /// Serializes the field, including the field number, and writes it to
175        /// <paramref name="output"/>.
176        /// </summary>
177        public void WriteTo(int fieldNumber, ICodedOutputStream output)
178        {
179            foreach (ulong value in varintList)
180            {
181                output.WriteUnknownField(fieldNumber, WireFormat.WireType.Varint, value);
182            }
183            foreach (uint value in fixed32List)
184            {
185                output.WriteUnknownField(fieldNumber, WireFormat.WireType.Fixed32, value);
186            }
187            foreach (ulong value in fixed64List)
188            {
189                output.WriteUnknownField(fieldNumber, WireFormat.WireType.Fixed64, value);
190            }
191            foreach (ByteString value in lengthDelimitedList)
192            {
193                output.WriteUnknownBytes(fieldNumber, value);
194            }
195            foreach (UnknownFieldSet value in groupList)
196            {
197#pragma warning disable 0612
198                output.WriteUnknownGroup(fieldNumber, value);
199#pragma warning restore 0612
200            }
201        }
202
203        /// <summary>
204        /// Computes the number of bytes required to encode this field, including field
205        /// number.
206        /// </summary>
207        public int GetSerializedSize(int fieldNumber)
208        {
209            int result = 0;
210            foreach (ulong value in varintList)
211            {
212                result += CodedOutputStream.ComputeUInt64Size(fieldNumber, value);
213            }
214            foreach (uint value in fixed32List)
215            {
216                result += CodedOutputStream.ComputeFixed32Size(fieldNumber, value);
217            }
218            foreach (ulong value in fixed64List)
219            {
220                result += CodedOutputStream.ComputeFixed64Size(fieldNumber, value);
221            }
222            foreach (ByteString value in lengthDelimitedList)
223            {
224                result += CodedOutputStream.ComputeBytesSize(fieldNumber, value);
225            }
226            foreach (UnknownFieldSet value in groupList)
227            {
228#pragma warning disable 0612
229                result += CodedOutputStream.ComputeUnknownGroupSize(fieldNumber, value);
230#pragma warning restore 0612
231            }
232            return result;
233        }
234
235        /// <summary>
236        /// Serializes the length-delimited values of the field, including field
237        /// number, and writes them to <paramref name="output"/> using the MessageSet wire format.
238        /// </summary>
239        /// <param name="fieldNumber"></param>
240        /// <param name="output"></param>
241        public void WriteAsMessageSetExtensionTo(int fieldNumber, ICodedOutputStream output)
242        {
243            foreach (ByteString value in lengthDelimitedList)
244            {
245                output.WriteMessageSetExtension(fieldNumber, UnknownFieldName, value);
246            }
247        }
248
249        /// <summary>
250        /// Get the number of bytes required to encode this field, incuding field number,
251        /// using the MessageSet wire format.
252        /// </summary>
253        public int GetSerializedSizeAsMessageSetExtension(int fieldNumber)
254        {
255            int result = 0;
256            foreach (ByteString value in lengthDelimitedList)
257            {
258                result += CodedOutputStream.ComputeRawMessageSetExtensionSize(fieldNumber, value);
259            }
260            return result;
261        }
262
263        /// <summary>
264        /// Used to build instances of UnknownField.
265        /// </summary>
266        public sealed class Builder
267        {
268            private List<ulong> varintList;
269            private List<uint> fixed32List;
270            private List<ulong> fixed64List;
271            private List<ByteString> lengthDelimitedList;
272            private List<UnknownFieldSet> groupList;
273
274            /// <summary>
275            /// Builds the field. After building, the builder is reset to an empty
276            /// state. (This is actually easier than making it unusable.)
277            /// </summary>
278            public UnknownField Build()
279            {
280                return new UnknownField(MakeReadOnly(ref varintList),
281                                        MakeReadOnly(ref fixed32List),
282                                        MakeReadOnly(ref fixed64List),
283                                        MakeReadOnly(ref lengthDelimitedList),
284                                        MakeReadOnly(ref groupList));
285            }
286
287            /// <summary>
288            /// Merge the values in <paramref name="other" /> into this field.  For each list
289            /// of values, <paramref name="other"/>'s values are append to the ones in this
290            /// field.
291            /// </summary>
292            public Builder MergeFrom(UnknownField other)
293            {
294                varintList = AddAll(varintList, other.VarintList);
295                fixed32List = AddAll(fixed32List, other.Fixed32List);
296                fixed64List = AddAll(fixed64List, other.Fixed64List);
297                lengthDelimitedList = AddAll(lengthDelimitedList, other.LengthDelimitedList);
298                groupList = AddAll(groupList, other.GroupList);
299                return this;
300            }
301
302            /// <summary>
303            /// Returns a new list containing all of the given specified values from
304            /// both the <paramref name="current"/> and <paramref name="extras"/> lists.
305            /// If <paramref name="current" /> is null and <paramref name="extras"/> is empty,
306            /// null is returned. Otherwise, either a new list is created (if <paramref name="current" />
307            /// is null) or the elements of <paramref name="extras"/> are added to <paramref name="current" />.
308            /// </summary>
309            private static List<T> AddAll<T>(List<T> current, IList<T> extras)
310            {
311                if (extras.Count == 0)
312                {
313                    return current;
314                }
315                if (current == null)
316                {
317                    current = new List<T>(extras);
318                }
319                else
320                {
321                    current.AddRange(extras);
322                }
323                return current;
324            }
325
326            /// <summary>
327            /// Clears the contents of this builder.
328            /// </summary>
329            public Builder Clear()
330            {
331                varintList = null;
332                fixed32List = null;
333                fixed64List = null;
334                lengthDelimitedList = null;
335                groupList = null;
336                return this;
337            }
338
339            /// <summary>
340            /// Adds a varint value.
341            /// </summary>
342            [CLSCompliant(false)]
343            public Builder AddVarint(ulong value)
344            {
345                varintList = Add(varintList, value);
346                return this;
347            }
348
349            /// <summary>
350            /// Adds a fixed32 value.
351            /// </summary>
352            [CLSCompliant(false)]
353            public Builder AddFixed32(uint value)
354            {
355                fixed32List = Add(fixed32List, value);
356                return this;
357            }
358
359            /// <summary>
360            /// Adds a fixed64 value.
361            /// </summary>
362            [CLSCompliant(false)]
363            public Builder AddFixed64(ulong value)
364            {
365                fixed64List = Add(fixed64List, value);
366                return this;
367            }
368
369            /// <summary>
370            /// Adds a length-delimited value.
371            /// </summary>
372            public Builder AddLengthDelimited(ByteString value)
373            {
374                lengthDelimitedList = Add(lengthDelimitedList, value);
375                return this;
376            }
377
378            /// <summary>
379            /// Adds an embedded group.
380            /// </summary>
381            /// <param name="value"></param>
382            /// <returns></returns>
383            public Builder AddGroup(UnknownFieldSet value)
384            {
385                groupList = Add(groupList, value);
386                return this;
387            }
388
389            /// <summary>
390            /// Adds <paramref name="value"/> to the <paramref name="list"/>, creating
391            /// a new list if <paramref name="list"/> is null. The list is returned - either
392            /// the original reference or the new list.
393            /// </summary>
394            private static List<T> Add<T>(List<T> list, T value)
395            {
396                if (list == null)
397                {
398                    list = new List<T>();
399                }
400                list.Add(value);
401                return list;
402            }
403
404            /// <summary>
405            /// Returns a read-only version of the given IList, and clears
406            /// the field used for <paramref name="list"/>. If the value
407            /// is null, an empty list is produced using Lists.Empty.
408            /// </summary>
409            /// <returns></returns>
410            private static ReadOnlyCollection<T> MakeReadOnly<T>(ref List<T> list)
411            {
412                ReadOnlyCollection<T> ret = list == null ? Lists<T>.Empty : new ReadOnlyCollection<T>(list);
413                list = null;
414                return ret;
415            }
416        }
417    }
418}
Note: See TracBrowser for help on using the repository browser.