OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2015 Google Inc. All rights reserved. | 3 // Copyright 2015 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 15 matching lines...) Expand all Loading... |
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 #endregion | 31 #endregion |
32 | 32 |
33 using System; | 33 using System; |
34 using System.Collections; | 34 using System.Collections; |
35 using System.Collections.Generic; | 35 using System.Collections.Generic; |
| 36 using System.IO; |
36 using System.Text; | 37 using System.Text; |
37 using Google.Protobuf.Compatibility; | |
38 | 38 |
39 namespace Google.Protobuf.Collections | 39 namespace Google.Protobuf.Collections |
40 { | 40 { |
41 /// <summary> | 41 /// <summary> |
42 /// The contents of a repeated field: essentially, a collection with some ex
tra | 42 /// The contents of a repeated field: essentially, a collection with some ex
tra |
43 /// restrictions (no null values) and capabilities (deep cloning). | 43 /// restrictions (no null values) and capabilities (deep cloning). |
44 /// </summary> | 44 /// </summary> |
45 /// <remarks> | 45 /// <remarks> |
46 /// This implementation does not generally prohibit the use of types which a
re not | 46 /// This implementation does not generally prohibit the use of types which a
re not |
47 /// supported by Protocol Buffers but nor does it guarantee that all operati
ons will work in such cases. | 47 /// supported by Protocol Buffers but nor does it guarantee that all operati
ons will work in such cases. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 /// Adds the entries from the given input stream, decoding them with the
specified codec. | 89 /// Adds the entries from the given input stream, decoding them with the
specified codec. |
90 /// </summary> | 90 /// </summary> |
91 /// <param name="input">The input stream to read from.</param> | 91 /// <param name="input">The input stream to read from.</param> |
92 /// <param name="codec">The codec to use in order to read each entry.</p
aram> | 92 /// <param name="codec">The codec to use in order to read each entry.</p
aram> |
93 public void AddEntriesFrom(CodedInputStream input, FieldCodec<T> codec) | 93 public void AddEntriesFrom(CodedInputStream input, FieldCodec<T> codec) |
94 { | 94 { |
95 // TODO: Inline some of the Add code, so we can avoid checking the s
ize on every | 95 // TODO: Inline some of the Add code, so we can avoid checking the s
ize on every |
96 // iteration. | 96 // iteration. |
97 uint tag = input.LastTag; | 97 uint tag = input.LastTag; |
98 var reader = codec.ValueReader; | 98 var reader = codec.ValueReader; |
99 // Value types can be packed or not. | 99 // Non-nullable value types can be packed or not. |
100 if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == Wir
eFormat.WireType.LengthDelimited) | 100 if (FieldCodec<T>.IsPackedRepeatedField(tag)) |
101 { | 101 { |
102 int length = input.ReadLength(); | 102 int length = input.ReadLength(); |
103 if (length > 0) | 103 if (length > 0) |
104 { | 104 { |
105 int oldLimit = input.PushLimit(length); | 105 int oldLimit = input.PushLimit(length); |
106 while (!input.ReachedLimit) | 106 while (!input.ReachedLimit) |
107 { | 107 { |
108 Add(reader(input)); | 108 Add(reader(input)); |
109 } | 109 } |
110 input.PopLimit(oldLimit); | 110 input.PopLimit(oldLimit); |
(...skipping 16 matching lines...) Expand all Loading... |
127 /// <param name="codec">The codec to use when encoding each field.</para
m> | 127 /// <param name="codec">The codec to use when encoding each field.</para
m> |
128 /// <returns>The number of bytes that would be written to a <see cref="C
odedOutputStream"/> by <see cref="WriteTo"/>, | 128 /// <returns>The number of bytes that would be written to a <see cref="C
odedOutputStream"/> by <see cref="WriteTo"/>, |
129 /// using the same codec.</returns> | 129 /// using the same codec.</returns> |
130 public int CalculateSize(FieldCodec<T> codec) | 130 public int CalculateSize(FieldCodec<T> codec) |
131 { | 131 { |
132 if (count == 0) | 132 if (count == 0) |
133 { | 133 { |
134 return 0; | 134 return 0; |
135 } | 135 } |
136 uint tag = codec.Tag; | 136 uint tag = codec.Tag; |
137 if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == Wir
eFormat.WireType.LengthDelimited) | 137 if (codec.PackedRepeatedField) |
138 { | 138 { |
139 int dataSize = CalculatePackedDataSize(codec); | 139 int dataSize = CalculatePackedDataSize(codec); |
140 return CodedOutputStream.ComputeRawVarint32Size(tag) + | 140 return CodedOutputStream.ComputeRawVarint32Size(tag) + |
141 CodedOutputStream.ComputeLengthSize(dataSize) + | 141 CodedOutputStream.ComputeLengthSize(dataSize) + |
142 dataSize; | 142 dataSize; |
143 } | 143 } |
144 else | 144 else |
145 { | 145 { |
146 var sizeCalculator = codec.ValueSizeCalculator; | 146 var sizeCalculator = codec.ValueSizeCalculator; |
147 int size = count * CodedOutputStream.ComputeRawVarint32Size(tag)
; | 147 int size = count * CodedOutputStream.ComputeRawVarint32Size(tag)
; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 /// <param name="output">The output stream to write to.</param> | 179 /// <param name="output">The output stream to write to.</param> |
180 /// <param name="codec">The codec to use when encoding each value.</para
m> | 180 /// <param name="codec">The codec to use when encoding each value.</para
m> |
181 public void WriteTo(CodedOutputStream output, FieldCodec<T> codec) | 181 public void WriteTo(CodedOutputStream output, FieldCodec<T> codec) |
182 { | 182 { |
183 if (count == 0) | 183 if (count == 0) |
184 { | 184 { |
185 return; | 185 return; |
186 } | 186 } |
187 var writer = codec.ValueWriter; | 187 var writer = codec.ValueWriter; |
188 var tag = codec.Tag; | 188 var tag = codec.Tag; |
189 if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == Wir
eFormat.WireType.LengthDelimited) | 189 if (codec.PackedRepeatedField) |
190 { | 190 { |
191 // Packed primitive type | 191 // Packed primitive type |
192 uint size = (uint)CalculatePackedDataSize(codec); | 192 uint size = (uint)CalculatePackedDataSize(codec); |
193 output.WriteTag(tag); | 193 output.WriteTag(tag); |
194 output.WriteRawVarint32(size); | 194 output.WriteRawVarint32(size); |
195 for (int i = 0; i < count; i++) | 195 for (int i = 0; i < count; i++) |
196 { | 196 { |
197 writer(output, array[i]); | 197 writer(output, array[i]); |
198 } | 198 } |
199 } | 199 } |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 count--; | 468 count--; |
469 array[count] = default(T); | 469 array[count] = default(T); |
470 } | 470 } |
471 | 471 |
472 /// <summary> | 472 /// <summary> |
473 /// Returns a string representation of this repeated field, in the same | 473 /// Returns a string representation of this repeated field, in the same |
474 /// way as it would be represented by the default JSON formatter. | 474 /// way as it would be represented by the default JSON formatter. |
475 /// </summary> | 475 /// </summary> |
476 public override string ToString() | 476 public override string ToString() |
477 { | 477 { |
478 var builder = new StringBuilder(); | 478 var writer = new StringWriter(); |
479 JsonFormatter.Default.WriteList(builder, this); | 479 JsonFormatter.Default.WriteList(writer, this); |
480 return builder.ToString(); | 480 return writer.ToString(); |
481 } | 481 } |
482 | 482 |
483 /// <summary> | 483 /// <summary> |
484 /// Gets or sets the item at the specified index. | 484 /// Gets or sets the item at the specified index. |
485 /// </summary> | 485 /// </summary> |
486 /// <value> | 486 /// <value> |
487 /// The element at the specified index. | 487 /// The element at the specified index. |
488 /// </value> | 488 /// </value> |
489 /// <param name="index">The zero-based index of the element to get or se
t.</param> | 489 /// <param name="index">The zero-based index of the element to get or se
t.</param> |
490 /// <returns>The item at the specified index.</returns> | 490 /// <returns>The item at the specified index.</returns> |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 { | 559 { |
560 if (!(value is T)) | 560 if (!(value is T)) |
561 { | 561 { |
562 return; | 562 return; |
563 } | 563 } |
564 Remove((T)value); | 564 Remove((T)value); |
565 } | 565 } |
566 #endregion | 566 #endregion |
567 } | 567 } |
568 } | 568 } |
OLD | NEW |