1 | #region Copyright notice and license
|
---|
2 | // Protocol Buffers - Google's data interchange format
|
---|
3 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
4 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
5 | // Original C++/Java/Python code:
|
---|
6 | // http://code.google.com/p/protobuf/
|
---|
7 | //
|
---|
8 | // Redistribution and use in source and binary forms, with or without
|
---|
9 | // modification, are permitted provided that the following conditions are
|
---|
10 | // met:
|
---|
11 | //
|
---|
12 | // * Redistributions of source code must retain the above copyright
|
---|
13 | // notice, this list of conditions and the following disclaimer.
|
---|
14 | // * Redistributions in binary form must reproduce the above
|
---|
15 | // copyright notice, this list of conditions and the following disclaimer
|
---|
16 | // in the documentation and/or other materials provided with the
|
---|
17 | // distribution.
|
---|
18 | // * Neither the name of Google Inc. nor the names of its
|
---|
19 | // contributors may be used to endorse or promote products derived from
|
---|
20 | // this software without specific prior written permission.
|
---|
21 | //
|
---|
22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | using System;
|
---|
36 | using System.Collections.Generic;
|
---|
37 | using System.IO;
|
---|
38 | using Google.ProtocolBuffers.Descriptors;
|
---|
39 |
|
---|
40 | namespace Google.ProtocolBuffers {
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// Non-generic interface for all members whose signatures don't require knowledge of
|
---|
44 | /// the type being built. The generic interface extends this one. Some methods return
|
---|
45 | /// either an IBuilder or an IMessage; in these cases the generic interface redeclares
|
---|
46 | /// the same method with a type-specific signature. Implementations are encouraged to
|
---|
47 | /// use explicit interface implemenation for the non-generic form. This mirrors
|
---|
48 | /// how IEnumerable and IEnumerable<T> work.
|
---|
49 | /// </summary>
|
---|
50 | public interface IBuilder {
|
---|
51 | /// <summary>
|
---|
52 | /// Returns true iff all required fields in the message and all
|
---|
53 | /// embedded messages are set.
|
---|
54 | /// </summary>
|
---|
55 | bool IsInitialized { get; }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// Only present in the nongeneric interface - useful for tests, but
|
---|
59 | /// not as much in real life.
|
---|
60 | /// </summary>
|
---|
61 | IBuilder SetField(FieldDescriptor field, object value);
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Only present in the nongeneric interface - useful for tests, but
|
---|
65 | /// not as much in real life.
|
---|
66 | /// </summary>
|
---|
67 | IBuilder SetRepeatedField(FieldDescriptor field, int index, object value);
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Behaves like the equivalent property in IMessage<T>.
|
---|
71 | /// The returned map may or may not reflect future changes to the builder.
|
---|
72 | /// Either way, the returned map is unmodifiable.
|
---|
73 | /// </summary>
|
---|
74 | IDictionary<FieldDescriptor, object> AllFields { get; }
|
---|
75 |
|
---|
76 | /// <summary>
|
---|
77 | /// Allows getting and setting of a field.
|
---|
78 | /// <see cref="IMessage{TMessage, TBuilder}.Item(FieldDescriptor)"/>
|
---|
79 | /// </summary>
|
---|
80 | /// <param name="field"></param>
|
---|
81 | /// <returns></returns>
|
---|
82 | object this[FieldDescriptor field] { get; set; }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Get the message's type descriptor.
|
---|
86 | /// <see cref="IMessage{TMessage, TBuilder}.DescriptorForType"/>
|
---|
87 | /// </summary>
|
---|
88 | MessageDescriptor DescriptorForType { get; }
|
---|
89 |
|
---|
90 | /// <summary>
|
---|
91 | /// <see cref="IMessage{TMessage, TBuilder}.GetRepeatedFieldCount"/>
|
---|
92 | /// </summary>
|
---|
93 | /// <param name="field"></param>
|
---|
94 | /// <returns></returns>
|
---|
95 | int GetRepeatedFieldCount(FieldDescriptor field);
|
---|
96 |
|
---|
97 | /// <summary>
|
---|
98 | /// Allows getting and setting of a repeated field value.
|
---|
99 | /// <see cref="IMessage{TMessage, TBuilder}.Item(FieldDescriptor, int)"/>
|
---|
100 | /// </summary>
|
---|
101 | object this[FieldDescriptor field, int index] { get; set; }
|
---|
102 |
|
---|
103 | /// <summary>
|
---|
104 | /// <see cref="IMessage{TMessage, TBuilder}.HasField"/>
|
---|
105 | /// </summary>
|
---|
106 | bool HasField(FieldDescriptor field);
|
---|
107 |
|
---|
108 | /// <summary>
|
---|
109 | /// <see cref="IMessage{TMessage, TBuilder}.UnknownFields"/>
|
---|
110 | /// </summary>
|
---|
111 | UnknownFieldSet UnknownFields { get; set; }
|
---|
112 |
|
---|
113 | /// <summary>
|
---|
114 | /// Create a builder for messages of the appropriate type for the given field.
|
---|
115 | /// Messages built with this can then be passed to the various mutation properties
|
---|
116 | /// and methods.
|
---|
117 | /// </summary>
|
---|
118 | IBuilder CreateBuilderForField(FieldDescriptor field);
|
---|
119 |
|
---|
120 | #region Methods which are like those of the generic form, but without any knowledge of the type parameters
|
---|
121 | IBuilder WeakAddRepeatedField(FieldDescriptor field, object value);
|
---|
122 | IBuilder WeakClear();
|
---|
123 | IBuilder WeakClearField(FieldDescriptor field);
|
---|
124 | IBuilder WeakMergeFrom(IMessage message);
|
---|
125 | IBuilder WeakMergeFrom(ByteString data);
|
---|
126 | IBuilder WeakMergeFrom(ByteString data, ExtensionRegistry registry);
|
---|
127 | IBuilder WeakMergeFrom(CodedInputStream input);
|
---|
128 | IBuilder WeakMergeFrom(CodedInputStream input, ExtensionRegistry registry);
|
---|
129 | IMessage WeakBuild();
|
---|
130 | IMessage WeakBuildPartial();
|
---|
131 | IBuilder WeakClone();
|
---|
132 | IMessage WeakDefaultInstanceForType { get; }
|
---|
133 | #endregion
|
---|
134 | }
|
---|
135 |
|
---|
136 | /// <summary>
|
---|
137 | /// Interface implemented by Protocol Message builders.
|
---|
138 | /// TODO(jonskeet): Consider "SetXXX" methods returning the builder, as well as the properties.
|
---|
139 | /// </summary>
|
---|
140 | /// <typeparam name="TMessage">Type of message</typeparam>
|
---|
141 | /// <typeparam name="TBuilder">Type of builder</typeparam>
|
---|
142 | public interface IBuilder<TMessage, TBuilder> : IBuilder
|
---|
143 | where TMessage : IMessage<TMessage, TBuilder>
|
---|
144 | where TBuilder : IBuilder<TMessage, TBuilder> {
|
---|
145 |
|
---|
146 | TBuilder SetUnknownFields(UnknownFieldSet unknownFields);
|
---|
147 |
|
---|
148 | /// <summary>
|
---|
149 | /// Resets all fields to their default values.
|
---|
150 | /// </summary>
|
---|
151 | TBuilder Clear();
|
---|
152 |
|
---|
153 | /// <summary>
|
---|
154 | /// Merge the specified other message into the message being
|
---|
155 | /// built. Merging occurs as follows. For each field:
|
---|
156 | /// For singular primitive fields, if the field is set in <paramref name="other"/>,
|
---|
157 | /// then <paramref name="other"/>'s value overwrites the value in this message.
|
---|
158 | /// For singular message fields, if the field is set in <paramref name="other"/>,
|
---|
159 | /// it is merged into the corresponding sub-message of this message using the same
|
---|
160 | /// merging rules.
|
---|
161 | /// For repeated fields, the elements in <paramref name="other"/> are concatenated
|
---|
162 | /// with the elements in this message.
|
---|
163 | /// </summary>
|
---|
164 | /// <param name="other"></param>
|
---|
165 | /// <returns></returns>
|
---|
166 | TBuilder MergeFrom(TMessage other);
|
---|
167 |
|
---|
168 | /// <summary>
|
---|
169 | /// Merge the specified other message which may be a different implementation of
|
---|
170 | /// the same message descriptor.
|
---|
171 | /// </summary>
|
---|
172 | TBuilder MergeFrom(IMessage other);
|
---|
173 |
|
---|
174 | /// <summary>
|
---|
175 | /// Constructs the final message. Once this is called, this Builder instance
|
---|
176 | /// is no longer valid, and calling any other method may throw a
|
---|
177 | /// NullReferenceException. If you need to continue working with the builder
|
---|
178 | /// after calling Build, call Clone first.
|
---|
179 | /// </summary>
|
---|
180 | /// <exception cref="UninitializedMessageException">the message
|
---|
181 | /// is missing one or more required fields; use BuildPartial to bypass
|
---|
182 | /// this check</exception>
|
---|
183 | TMessage Build();
|
---|
184 |
|
---|
185 | /// <summary>
|
---|
186 | /// Like Build(), but does not throw an exception if the message is missing
|
---|
187 | /// required fields. Instead, a partial message is returned.
|
---|
188 | /// </summary>
|
---|
189 | TMessage BuildPartial();
|
---|
190 |
|
---|
191 | /// <summary>
|
---|
192 | /// Clones this builder.
|
---|
193 | /// TODO(jonskeet): Explain depth of clone.
|
---|
194 | /// </summary>
|
---|
195 | TBuilder Clone();
|
---|
196 |
|
---|
197 | /// <summary>
|
---|
198 | /// Parses a message of this type from the input and merges it with this
|
---|
199 | /// message, as if using MergeFrom(IMessage<T>).
|
---|
200 | /// </summary>
|
---|
201 | /// <remarks>
|
---|
202 | /// Warning: This does not verify that all required fields are present
|
---|
203 | /// in the input message. If you call Build() without setting all
|
---|
204 | /// required fields, it will throw an UninitializedMessageException.
|
---|
205 | /// There are a few good ways to deal with this:
|
---|
206 | /// <list>
|
---|
207 | /// <item>Call IsInitialized to verify to verify that all required fields are
|
---|
208 | /// set before building.</item>
|
---|
209 | /// <item>Parse the message separately using one of the static ParseFrom
|
---|
210 | /// methods, then use MergeFrom(IMessage<T>) to merge it with
|
---|
211 | /// this one. ParseFrom will throw an InvalidProtocolBufferException
|
---|
212 | /// (an IOException) if some required fields are missing.
|
---|
213 | /// Use BuildPartial to build, which ignores missing required fields.
|
---|
214 | /// </list>
|
---|
215 | /// </remarks>
|
---|
216 | TBuilder MergeFrom(CodedInputStream input);
|
---|
217 |
|
---|
218 | /// <summary>
|
---|
219 | /// Like MergeFrom(CodedInputStream), but also parses extensions.
|
---|
220 | /// The extensions that you want to be able to parse must be registered
|
---|
221 | /// in <paramref name="extensionRegistry"/>. Extensions not in the registry
|
---|
222 | /// will be treated as unknown fields.
|
---|
223 | /// </summary>
|
---|
224 | TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry);
|
---|
225 |
|
---|
226 | /// <summary>
|
---|
227 | /// Get's the message's type's default instance.
|
---|
228 | /// <see cref="IMessage{TMessage}.DefaultInstanceForType" />
|
---|
229 | /// </summary>
|
---|
230 | TMessage DefaultInstanceForType { get; }
|
---|
231 |
|
---|
232 | /// <summary>
|
---|
233 | /// Clears the field. This is exactly equivalent to calling the generated
|
---|
234 | /// Clear method corresponding to the field.
|
---|
235 | /// </summary>
|
---|
236 | /// <param name="field"></param>
|
---|
237 | /// <returns></returns>
|
---|
238 | TBuilder ClearField(FieldDescriptor field);
|
---|
239 |
|
---|
240 | /// <summary>
|
---|
241 | /// Appends the given value as a new element for the specified repeated field.
|
---|
242 | /// </summary>
|
---|
243 | /// <exception cref="ArgumentException">the field is not a repeated field,
|
---|
244 | /// the field does not belong to this builder's type, or the value is
|
---|
245 | /// of the incorrect type
|
---|
246 | /// </exception>
|
---|
247 | TBuilder AddRepeatedField(FieldDescriptor field, object value);
|
---|
248 |
|
---|
249 | /// <summary>
|
---|
250 | /// Merge some unknown fields into the set for this message.
|
---|
251 | /// </summary>
|
---|
252 | TBuilder MergeUnknownFields(UnknownFieldSet unknownFields);
|
---|
253 |
|
---|
254 | /// <summary>
|
---|
255 | /// Like MergeFrom(Stream), but does not read until the end of the file.
|
---|
256 | /// Instead, the size of the message (encoded as a varint) is read first,
|
---|
257 | /// then the message data. Use Message.WriteDelimitedTo(Stream) to
|
---|
258 | /// write messages in this format.
|
---|
259 | /// </summary>
|
---|
260 | /// <param name="input"></param>
|
---|
261 | TBuilder MergeDelimitedFrom(Stream input);
|
---|
262 |
|
---|
263 | /// <summary>
|
---|
264 | /// Like MergeDelimitedFrom(Stream) but supporting extensions.
|
---|
265 | /// </summary>
|
---|
266 | TBuilder MergeDelimitedFrom(Stream input, ExtensionRegistry extensionRegistry);
|
---|
267 |
|
---|
268 | #region Convenience methods
|
---|
269 | /// <summary>
|
---|
270 | /// Parse <paramref name="data"/> as a message of this type and merge
|
---|
271 | /// it with the message being built. This is just a small wrapper around
|
---|
272 | /// MergeFrom(CodedInputStream).
|
---|
273 | /// </summary>
|
---|
274 | TBuilder MergeFrom(ByteString data);
|
---|
275 |
|
---|
276 | /// <summary>
|
---|
277 | /// Parse <paramref name="data"/> as a message of this type and merge
|
---|
278 | /// it with the message being built. This is just a small wrapper around
|
---|
279 | /// MergeFrom(CodedInputStream, ExtensionRegistry).
|
---|
280 | /// </summary>
|
---|
281 | TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry);
|
---|
282 |
|
---|
283 | /// <summary>
|
---|
284 | /// Parse <paramref name="data"/> as a message of this type and merge
|
---|
285 | /// it with the message being built. This is just a small wrapper around
|
---|
286 | /// MergeFrom(CodedInputStream).
|
---|
287 | /// </summary>
|
---|
288 | TBuilder MergeFrom(byte[] data);
|
---|
289 |
|
---|
290 | /// <summary>
|
---|
291 | /// Parse <paramref name="data"/> as a message of this type and merge
|
---|
292 | /// it with the message being built. This is just a small wrapper around
|
---|
293 | /// MergeFrom(CodedInputStream, ExtensionRegistry).
|
---|
294 | /// </summary>
|
---|
295 | TBuilder MergeFrom(byte[] data, ExtensionRegistry extensionRegistry);
|
---|
296 |
|
---|
297 | /// <summary>
|
---|
298 | /// Parse <paramref name="input"/> as a message of this type and merge
|
---|
299 | /// it with the message being built. This is just a small wrapper around
|
---|
300 | /// MergeFrom(CodedInputStream). Note that this method always reads
|
---|
301 | /// the entire input (unless it throws an exception). If you want it to
|
---|
302 | /// stop earlier, you will need to wrap the input in a wrapper
|
---|
303 | /// stream which limits reading. Or, use IMessage.WriteDelimitedTo(Stream)
|
---|
304 | /// to write your message and MmergeDelimitedFrom(Stream) to read it.
|
---|
305 | /// Despite usually reading the entire stream, this method never closes the stream.
|
---|
306 | /// </summary>
|
---|
307 | TBuilder MergeFrom(Stream input);
|
---|
308 |
|
---|
309 | /// <summary>
|
---|
310 | /// Parse <paramref name="input"/> as a message of this type and merge
|
---|
311 | /// it with the message being built. This is just a small wrapper around
|
---|
312 | /// MergeFrom(CodedInputStream, ExtensionRegistry).
|
---|
313 | /// </summary>
|
---|
314 | TBuilder MergeFrom(Stream input, ExtensionRegistry extensionRegistry);
|
---|
315 | #endregion
|
---|
316 | }
|
---|
317 | }
|
---|