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 |
|
---|
37 | using System;
|
---|
38 | using System.Collections.Generic;
|
---|
39 | using Google.ProtocolBuffers.Descriptors;
|
---|
40 |
|
---|
41 | namespace Google.ProtocolBuffers
|
---|
42 | {
|
---|
43 | public abstract partial class ExtendableBuilder<TMessage, TBuilder> : GeneratedBuilder<TMessage, TBuilder>
|
---|
44 | where TMessage : ExtendableMessage<TMessage, TBuilder>
|
---|
45 | where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
|
---|
46 | {
|
---|
47 | protected ExtendableBuilder()
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Checks if a singular extension is present
|
---|
53 | /// </summary>
|
---|
54 | public bool HasExtension<TExtension>(GeneratedExtensionBase<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>(GeneratedExtensionBase<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>(GeneratedExtensionBase<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>(GeneratedExtensionBase<IList<TExtension>> extension, int index)
|
---|
79 | {
|
---|
80 | return MessageBeingBuilt.GetExtension(extension, index);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
84 | /// Sets the value of an extension.
|
---|
85 | /// </summary>
|
---|
86 | public TBuilder SetExtension<TExtension>(GeneratedExtensionBase<TExtension> extension, TExtension value)
|
---|
87 | {
|
---|
88 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
89 | message.VerifyExtensionContainingType(extension);
|
---|
90 | message.Extensions[extension.Descriptor] = extension.ToReflectionType(value);
|
---|
91 | return ThisBuilder;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// <summary>
|
---|
95 | /// Sets the value of one element of a repeated extension.
|
---|
96 | /// </summary>
|
---|
97 | public TBuilder SetExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, int index,
|
---|
98 | TExtension value)
|
---|
99 | {
|
---|
100 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
101 | message.VerifyExtensionContainingType(extension);
|
---|
102 | message.Extensions[extension.Descriptor, index] = extension.SingularToReflectionType(value);
|
---|
103 | return ThisBuilder;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /// <summary>
|
---|
107 | /// Appends a value to a repeated extension.
|
---|
108 | /// </summary>
|
---|
109 | public TBuilder AddExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, TExtension value)
|
---|
110 | {
|
---|
111 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
112 | message.VerifyExtensionContainingType(extension);
|
---|
113 | message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
|
---|
114 | return ThisBuilder;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | /// Clears an extension.
|
---|
119 | /// </summary>
|
---|
120 | public TBuilder ClearExtension<TExtension>(GeneratedExtensionBase<TExtension> extension)
|
---|
121 | {
|
---|
122 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
123 | message.VerifyExtensionContainingType(extension);
|
---|
124 | message.Extensions.ClearField(extension.Descriptor);
|
---|
125 | return ThisBuilder;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /// <summary>
|
---|
129 | /// Called by subclasses to parse an unknown field or an extension.
|
---|
130 | /// </summary>
|
---|
131 | /// <returns>true unless the tag is an end-group tag</returns>
|
---|
132 | [CLSCompliant(false)]
|
---|
133 | protected override bool ParseUnknownField(ICodedInputStream input, UnknownFieldSet.Builder unknownFields,
|
---|
134 | ExtensionRegistry extensionRegistry, uint tag, string fieldName)
|
---|
135 | {
|
---|
136 | return unknownFields.MergeFieldFrom(input, extensionRegistry, this, tag, fieldName);
|
---|
137 | }
|
---|
138 |
|
---|
139 | // ---------------------------------------------------------------
|
---|
140 | // Reflection
|
---|
141 |
|
---|
142 |
|
---|
143 | public override object this[FieldDescriptor field, int index]
|
---|
144 | {
|
---|
145 | set
|
---|
146 | {
|
---|
147 | if (field.IsExtension)
|
---|
148 | {
|
---|
149 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
150 | message.VerifyContainingType(field);
|
---|
151 | message.Extensions[field, index] = value;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | base[field, index] = value;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | public override object this[FieldDescriptor field]
|
---|
162 | {
|
---|
163 | set
|
---|
164 | {
|
---|
165 | if (field.IsExtension)
|
---|
166 | {
|
---|
167 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
168 | message.VerifyContainingType(field);
|
---|
169 | message.Extensions[field] = value;
|
---|
170 | }
|
---|
171 | else
|
---|
172 | {
|
---|
173 | base[field] = value;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | public override TBuilder ClearField(FieldDescriptor field)
|
---|
179 | {
|
---|
180 | if (field.IsExtension)
|
---|
181 | {
|
---|
182 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
183 | message.VerifyContainingType(field);
|
---|
184 | message.Extensions.ClearField(field);
|
---|
185 | return ThisBuilder;
|
---|
186 | }
|
---|
187 | else
|
---|
188 | {
|
---|
189 | return base.ClearField(field);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public override TBuilder AddRepeatedField(FieldDescriptor field, object value)
|
---|
194 | {
|
---|
195 | if (field.IsExtension)
|
---|
196 | {
|
---|
197 | ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
|
---|
198 | message.VerifyContainingType(field);
|
---|
199 | message.Extensions.AddRepeatedField(field, value);
|
---|
200 | return ThisBuilder;
|
---|
201 | }
|
---|
202 | else
|
---|
203 | {
|
---|
204 | return base.AddRepeatedField(field, value);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | protected void MergeExtensionFields(ExtendableMessage<TMessage, TBuilder> other)
|
---|
209 | {
|
---|
210 | MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | } |
---|