Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // http://github.com/jskeet/dotnet-protobufs/ |
| 4 | // Original C++/Java/Python code: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 5 | // http://code.google.com/p/protobuf/ |
| 6 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame^] | 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions are |
| 9 | // met: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 10 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame^] | 11 | // * Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // * Redistributions in binary form must reproduce the above |
| 14 | // copyright notice, this list of conditions and the following disclaimer |
| 15 | // in the documentation and/or other materials provided with the |
| 16 | // distribution. |
| 17 | // * Neither the name of Google Inc. nor the names of its |
| 18 | // contributors may be used to endorse or promote products derived from |
| 19 | // this software without specific prior written permission. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 20 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame^] | 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 32 | using System.Collections.Generic; |
| 33 | using System.Collections.ObjectModel; |
| 34 | using Google.ProtocolBuffers.Collections; |
| 35 | |
| 36 | namespace Google.ProtocolBuffers { |
| 37 | /// <summary> |
| 38 | /// Represents a single field in an UnknownFieldSet. |
| 39 | /// |
| 40 | /// An UnknownField consists of five lists of values. The lists correspond |
| 41 | /// to the five "wire types" used in the protocol buffer binary format. |
| 42 | /// The wire type of each field can be determined from the encoded form alone, |
| 43 | /// without knowing the field's declared type. So, we are able to parse |
| 44 | /// unknown values at least this far and separate them. Normally, only one |
| 45 | /// of the five lists will contain any values, since it is impossible to |
| 46 | /// define a valid message type that declares two different types for the |
| 47 | /// same field number. However, the code is designed to allow for the case |
| 48 | /// where the same unknown field number is encountered using multiple different |
| 49 | /// wire types. |
| 50 | /// |
| 51 | /// UnknownField is an immutable class. To construct one, you must use an |
| 52 | /// UnknownField.Builder. |
| 53 | /// </summary> |
| 54 | public sealed class UnknownField { |
| 55 | |
| 56 | private static readonly UnknownField defaultInstance = CreateBuilder().Build(); |
| 57 | private readonly ReadOnlyCollection<ulong> varintList; |
| 58 | private readonly ReadOnlyCollection<uint> fixed32List; |
| 59 | private readonly ReadOnlyCollection<ulong> fixed64List; |
| 60 | private readonly ReadOnlyCollection<ByteString> lengthDelimitedList; |
| 61 | private readonly ReadOnlyCollection<UnknownFieldSet> groupList; |
| 62 | |
| 63 | private UnknownField(ReadOnlyCollection<ulong> varintList, |
| 64 | ReadOnlyCollection<uint> fixed32List, |
| 65 | ReadOnlyCollection<ulong> fixed64List, |
| 66 | ReadOnlyCollection<ByteString> lengthDelimitedList, |
| 67 | ReadOnlyCollection<UnknownFieldSet> groupList) { |
| 68 | this.varintList = varintList; |
| 69 | this.fixed32List = fixed32List; |
| 70 | this.fixed64List = fixed64List; |
| 71 | this.lengthDelimitedList = lengthDelimitedList; |
| 72 | this.groupList = groupList; |
| 73 | } |
| 74 | |
| 75 | public static UnknownField DefaultInstance { |
| 76 | get { return defaultInstance; } |
| 77 | } |
| 78 | |
| 79 | /// <summary> |
| 80 | /// The list of varint values for this field. |
| 81 | /// </summary> |
| 82 | public IList<ulong> VarintList { |
| 83 | get { return varintList; } |
| 84 | } |
| 85 | |
| 86 | /// <summary> |
| 87 | /// The list of fixed32 values for this field. |
| 88 | /// </summary> |
| 89 | public IList<uint> Fixed32List { |
| 90 | get { return fixed32List; } |
| 91 | } |
| 92 | |
| 93 | /// <summary> |
| 94 | /// The list of fixed64 values for this field. |
| 95 | /// </summary> |
| 96 | public IList<ulong> Fixed64List { |
| 97 | get { return fixed64List; } |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
| 101 | /// The list of length-delimited values for this field. |
| 102 | /// </summary> |
| 103 | public IList<ByteString> LengthDelimitedList { |
| 104 | get { return lengthDelimitedList; } |
| 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// The list of embedded group values for this field. These |
| 109 | /// are represented using UnknownFieldSets rather than Messages |
| 110 | /// since the group's type is presumably unknown. |
| 111 | /// </summary> |
| 112 | public IList<UnknownFieldSet> GroupList { |
| 113 | get { return groupList; } |
| 114 | } |
| 115 | |
| 116 | /// <summary> |
| 117 | /// Constructs a new Builder. |
| 118 | /// </summary> |
| 119 | public static Builder CreateBuilder() { |
| 120 | return new Builder(); |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Constructs a new Builder and initializes it to a copy of <paramref name="copyFrom"/>. |
| 125 | /// </summary> |
| 126 | public static Builder CreateBuilder(UnknownField copyFrom) { |
| 127 | return new Builder().MergeFrom(copyFrom); |
| 128 | } |
| 129 | |
| 130 | /// <summary> |
| 131 | /// Serializes the field, including the field number, and writes it to |
| 132 | /// <paramref name="output"/>. |
| 133 | /// </summary> |
| 134 | public void WriteTo(int fieldNumber, CodedOutputStream output) { |
| 135 | foreach (ulong value in varintList) { |
| 136 | output.WriteUInt64(fieldNumber, value); |
| 137 | } |
| 138 | foreach (uint value in fixed32List) { |
| 139 | output.WriteFixed32(fieldNumber, value); |
| 140 | } |
| 141 | foreach (ulong value in fixed64List) { |
| 142 | output.WriteFixed64(fieldNumber, value); |
| 143 | } |
| 144 | foreach (ByteString value in lengthDelimitedList) { |
| 145 | output.WriteBytes(fieldNumber, value); |
| 146 | } |
| 147 | foreach (UnknownFieldSet value in groupList) { |
| 148 | output.WriteUnknownGroup(fieldNumber, value); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// <summary> |
| 153 | /// Computes the number of bytes required to encode this field, including field |
| 154 | /// number. |
| 155 | /// </summary> |
| 156 | public int GetSerializedSize(int fieldNumber) { |
| 157 | int result = 0; |
| 158 | foreach (ulong value in varintList) { |
| 159 | result += CodedOutputStream.ComputeUInt64Size(fieldNumber, value); |
| 160 | } |
| 161 | foreach (uint value in fixed32List) { |
| 162 | result += CodedOutputStream.ComputeFixed32Size(fieldNumber, value); |
| 163 | } |
| 164 | foreach (ulong value in fixed64List) { |
| 165 | result += CodedOutputStream.ComputeFixed64Size(fieldNumber, value); |
| 166 | } |
| 167 | foreach (ByteString value in lengthDelimitedList) { |
| 168 | result += CodedOutputStream.ComputeBytesSize(fieldNumber, value); |
| 169 | } |
| 170 | foreach (UnknownFieldSet value in groupList) { |
| 171 | result += CodedOutputStream.ComputeUnknownGroupSize(fieldNumber, value); |
| 172 | } |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | /// <summary> |
| 177 | /// Serializes the length-delimited values of the field, including field |
| 178 | /// number, and writes them to <paramref name="output"/> using the MessageSet wire format. |
| 179 | /// </summary> |
| 180 | /// <param name="fieldNumber"></param> |
| 181 | /// <param name="output"></param> |
| 182 | public void WriteAsMessageSetExtensionTo(int fieldNumber, CodedOutputStream output) { |
| 183 | foreach (ByteString value in lengthDelimitedList) { |
| 184 | output.WriteRawMessageSetExtension(fieldNumber, value); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /// <summary> |
| 189 | /// Get the number of bytes required to encode this field, incuding field number, |
| 190 | /// using the MessageSet wire format. |
| 191 | /// </summary> |
| 192 | public int GetSerializedSizeAsMessageSetExtension(int fieldNumber) { |
| 193 | int result = 0; |
| 194 | foreach (ByteString value in lengthDelimitedList) { |
| 195 | result += CodedOutputStream.ComputeRawMessageSetExtensionSize(fieldNumber, value); |
| 196 | } |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | /// <summary> |
| 201 | /// Used to build instances of UnknownField. |
| 202 | /// </summary> |
| 203 | public sealed class Builder { |
| 204 | |
| 205 | private List<ulong> varintList; |
| 206 | private List<uint> fixed32List; |
| 207 | private List<ulong> fixed64List; |
| 208 | private List<ByteString> lengthDelimitedList; |
| 209 | private List<UnknownFieldSet> groupList; |
| 210 | |
| 211 | /// <summary> |
| 212 | /// Builds the field. After building, the builder is reset to an empty |
| 213 | /// state. (This is actually easier than making it unusable.) |
| 214 | /// </summary> |
| 215 | public UnknownField Build() { |
| 216 | return new UnknownField(MakeReadOnly(ref varintList), |
| 217 | MakeReadOnly(ref fixed32List), |
| 218 | MakeReadOnly(ref fixed64List), |
| 219 | MakeReadOnly(ref lengthDelimitedList), |
| 220 | MakeReadOnly(ref groupList)); |
| 221 | } |
| 222 | |
| 223 | /// <summary> |
| 224 | /// Merge the values in <paramref name="other" /> into this field. For each list |
| 225 | /// of values, <paramref name="other"/>'s values are append to the ones in this |
| 226 | /// field. |
| 227 | /// </summary> |
| 228 | public Builder MergeFrom(UnknownField other) { |
| 229 | varintList = AddAll(varintList, other.VarintList); |
| 230 | fixed32List = AddAll(fixed32List, other.Fixed32List); |
| 231 | fixed64List = AddAll(fixed64List, other.Fixed64List); |
| 232 | lengthDelimitedList = AddAll(lengthDelimitedList, other.LengthDelimitedList); |
| 233 | groupList = AddAll(groupList, other.GroupList); |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | /// <summary> |
| 238 | /// Returns a new list containing all of the given specified values from |
| 239 | /// both the <paramref name="current"/> and <paramref name="extras"/> lists. |
| 240 | /// If <paramref name="current" /> is null and <paramref name="extras"/> is empty, |
| 241 | /// null is returned. Otherwise, either a new list is created (if <paramref name="current" /> |
| 242 | /// is null) or the elements of <paramref name="extras"/> are added to <paramref name="current" />. |
| 243 | /// </summary> |
| 244 | private static List<T> AddAll<T>(List<T> current, IList<T> extras) |
| 245 | { |
| 246 | if (extras.Count == 0) { |
| 247 | return current; |
| 248 | } |
| 249 | if (current == null) { |
| 250 | current = new List<T>(extras); |
| 251 | } else { |
| 252 | current.AddRange(extras); |
| 253 | } |
| 254 | return current; |
| 255 | } |
| 256 | |
| 257 | /// <summary> |
| 258 | /// Clears the contents of this builder. |
| 259 | /// </summary> |
| 260 | public Builder Clear() { |
| 261 | varintList = null; |
| 262 | fixed32List = null; |
| 263 | fixed64List = null; |
| 264 | lengthDelimitedList = null; |
| 265 | groupList = null; |
| 266 | return this; |
| 267 | } |
| 268 | |
| 269 | /// <summary> |
| 270 | /// Adds a varint value. |
| 271 | /// </summary> |
| 272 | public Builder AddVarint(ulong value) { |
| 273 | varintList = Add(varintList, value); |
| 274 | return this; |
| 275 | } |
| 276 | |
| 277 | /// <summary> |
| 278 | /// Adds a fixed32 value. |
| 279 | /// </summary> |
| 280 | public Builder AddFixed32(uint value) { |
| 281 | fixed32List = Add(fixed32List, value); |
| 282 | return this; |
| 283 | } |
| 284 | |
| 285 | /// <summary> |
| 286 | /// Adds a fixed64 value. |
| 287 | /// </summary> |
| 288 | public Builder AddFixed64(ulong value) { |
| 289 | fixed64List = Add(fixed64List, value); |
| 290 | return this; |
| 291 | } |
| 292 | |
| 293 | /// <summary> |
| 294 | /// Adds a length-delimited value. |
| 295 | /// </summary> |
| 296 | public Builder AddLengthDelimited(ByteString value) { |
| 297 | lengthDelimitedList = Add(lengthDelimitedList, value); |
| 298 | return this; |
| 299 | } |
| 300 | |
| 301 | /// <summary> |
| 302 | /// Adds an embedded group. |
| 303 | /// </summary> |
| 304 | /// <param name="value"></param> |
| 305 | /// <returns></returns> |
| 306 | public Builder AddGroup(UnknownFieldSet value) { |
| 307 | groupList = Add(groupList, value); |
| 308 | return this; |
| 309 | } |
| 310 | |
| 311 | /// <summary> |
| 312 | /// Adds <paramref name="value"/> to the <paramref name="list"/>, creating |
| 313 | /// a new list if <paramref name="list"/> is null. The list is returned - either |
| 314 | /// the original reference or the new list. |
| 315 | /// </summary> |
| 316 | private static List<T> Add<T>(List<T> list, T value) { |
| 317 | if (list == null) { |
| 318 | list = new List<T>(); |
| 319 | } |
| 320 | list.Add(value); |
| 321 | return list; |
| 322 | } |
| 323 | |
| 324 | /// <summary> |
| 325 | /// Returns a read-only version of the given IList, and clears |
| 326 | /// the field used for <paramref name="list"/>. If the value |
| 327 | /// is null, an empty list is produced using Lists.Empty. |
| 328 | /// </summary> |
| 329 | /// <returns></returns> |
| 330 | private static ReadOnlyCollection<T> MakeReadOnly<T>(ref List<T> list) { |
| 331 | ReadOnlyCollection<T> ret = list == null ? Lists<T>.Empty : new ReadOnlyCollection<T>(list); |
| 332 | list = null; |
| 333 | return ret; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |