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; |
| 33 | using System.Collections.Generic; |
| 34 | using System.IO; |
| 35 | using Google.ProtocolBuffers.Descriptors; |
| 36 | |
| 37 | namespace Google.ProtocolBuffers { |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Non-generic interface for all members whose signatures don't require knowledge of |
| 41 | /// the type being built. The generic interface extends this one. Some methods return |
| 42 | /// either an IBuilder or an IMessage; in these cases the generic interface redeclares |
| 43 | /// the same method with a type-specific signature. Implementations are encouraged to |
| 44 | /// use explicit interface implemenation for the non-generic form. This mirrors |
| 45 | /// how IEnumerable and IEnumerable<T> work. |
| 46 | /// </summary> |
| 47 | public interface IBuilder { |
| 48 | /// <summary> |
| 49 | /// Returns true iff all required fields in the message and all |
| 50 | /// embedded messages are set. |
| 51 | /// </summary> |
| 52 | bool IsInitialized { get; } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Only present in the nongeneric interface - useful for tests, but |
| 56 | /// not as much in real life. |
| 57 | /// </summary> |
| 58 | IBuilder SetField(FieldDescriptor field, object value); |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Only present in the nongeneric interface - useful for tests, but |
| 62 | /// not as much in real life. |
| 63 | /// </summary> |
| 64 | IBuilder SetRepeatedField(FieldDescriptor field, int index, object value); |
| 65 | |
| 66 | /// <summary> |
| 67 | /// Behaves like the equivalent property in IMessage<T>. |
| 68 | /// The returned map may or may not reflect future changes to the builder. |
| 69 | /// Either way, the returned map is unmodifiable. |
| 70 | /// </summary> |
| 71 | IDictionary<FieldDescriptor, object> AllFields { get; } |
| 72 | |
| 73 | /// <summary> |
| 74 | /// Allows getting and setting of a field. |
| 75 | /// <see cref="IMessage{TMessage, TBuilder}.Item(FieldDescriptor)"/> |
| 76 | /// </summary> |
| 77 | /// <param name="field"></param> |
| 78 | /// <returns></returns> |
| 79 | object this[FieldDescriptor field] { get; set; } |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Get the message's type descriptor. |
| 83 | /// <see cref="IMessage{TMessage, TBuilder}.DescriptorForType"/> |
| 84 | /// </summary> |
| 85 | MessageDescriptor DescriptorForType { get; } |
| 86 | |
| 87 | /// <summary> |
| 88 | /// <see cref="IMessage{TMessage, TBuilder}.GetRepeatedFieldCount"/> |
| 89 | /// </summary> |
| 90 | /// <param name="field"></param> |
| 91 | /// <returns></returns> |
| 92 | int GetRepeatedFieldCount(FieldDescriptor field); |
| 93 | |
| 94 | /// <summary> |
| 95 | /// Allows getting and setting of a repeated field value. |
| 96 | /// <see cref="IMessage{TMessage, TBuilder}.Item(FieldDescriptor, int)"/> |
| 97 | /// </summary> |
| 98 | object this[FieldDescriptor field, int index] { get; set; } |
| 99 | |
| 100 | /// <summary> |
| 101 | /// <see cref="IMessage{TMessage, TBuilder}.HasField"/> |
| 102 | /// </summary> |
| 103 | bool HasField(FieldDescriptor field); |
| 104 | |
| 105 | /// <summary> |
| 106 | /// <see cref="IMessage{TMessage, TBuilder}.UnknownFields"/> |
| 107 | /// </summary> |
| 108 | UnknownFieldSet UnknownFields { get; set; } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Create a builder for messages of the appropriate type for the given field. |
| 112 | /// Messages built with this can then be passed to the various mutation properties |
| 113 | /// and methods. |
| 114 | /// </summary> |
| 115 | IBuilder CreateBuilderForField(FieldDescriptor field); |
| 116 | |
| 117 | #region Methods which are like those of the generic form, but without any knowledge of the type parameters |
| 118 | IBuilder WeakAddRepeatedField(FieldDescriptor field, object value); |
| 119 | IBuilder WeakClear(); |
| 120 | IBuilder WeakClearField(FieldDescriptor field); |
| 121 | IBuilder WeakMergeFrom(IMessage message); |
| 122 | IBuilder WeakMergeFrom(ByteString data); |
| 123 | IBuilder WeakMergeFrom(ByteString data, ExtensionRegistry registry); |
| 124 | IBuilder WeakMergeFrom(CodedInputStream input); |
| 125 | IBuilder WeakMergeFrom(CodedInputStream input, ExtensionRegistry registry); |
| 126 | IMessage WeakBuild(); |
| 127 | IMessage WeakBuildPartial(); |
| 128 | IBuilder WeakClone(); |
| 129 | IMessage WeakDefaultInstanceForType { get; } |
| 130 | #endregion |
| 131 | } |
| 132 | |
| 133 | /// <summary> |
| 134 | /// Interface implemented by Protocol Message builders. |
| 135 | /// TODO(jonskeet): Consider "SetXXX" methods returning the builder, as well as the properties. |
| 136 | /// </summary> |
| 137 | /// <typeparam name="TMessage">Type of message</typeparam> |
| 138 | /// <typeparam name="TBuilder">Type of builder</typeparam> |
| 139 | public interface IBuilder<TMessage, TBuilder> : IBuilder |
| 140 | where TMessage : IMessage<TMessage, TBuilder> |
| 141 | where TBuilder : IBuilder<TMessage, TBuilder> { |
| 142 | |
| 143 | TBuilder SetUnknownFields(UnknownFieldSet unknownFields); |
| 144 | |
| 145 | /// <summary> |
| 146 | /// Resets all fields to their default values. |
| 147 | /// </summary> |
| 148 | TBuilder Clear(); |
| 149 | |
| 150 | /// <summary> |
| 151 | /// Merge the specified other message into the message being |
| 152 | /// built. Merging occurs as follows. For each field: |
| 153 | /// For singular primitive fields, if the field is set in <paramref name="other"/>, |
| 154 | /// then <paramref name="other"/>'s value overwrites the value in this message. |
| 155 | /// For singular message fields, if the field is set in <paramref name="other"/>, |
| 156 | /// it is merged into the corresponding sub-message of this message using the same |
| 157 | /// merging rules. |
| 158 | /// For repeated fields, the elements in <paramref name="other"/> are concatenated |
| 159 | /// with the elements in this message. |
| 160 | /// </summary> |
| 161 | /// <param name="other"></param> |
| 162 | /// <returns></returns> |
| 163 | TBuilder MergeFrom(TMessage other); |
| 164 | |
| 165 | /// <summary> |
| 166 | /// Merge the specified other message which may be a different implementation of |
| 167 | /// the same message descriptor. |
| 168 | /// </summary> |
| 169 | TBuilder MergeFrom(IMessage other); |
| 170 | |
| 171 | /// <summary> |
| 172 | /// Constructs the final message. Once this is called, this Builder instance |
| 173 | /// is no longer valid, and calling any other method may throw a |
| 174 | /// NullReferenceException. If you need to continue working with the builder |
| 175 | /// after calling Build, call Clone first. |
| 176 | /// </summary> |
| 177 | /// <exception cref="UninitializedMessageException">the message |
| 178 | /// is missing one or more required fields; use BuildPartial to bypass |
| 179 | /// this check</exception> |
| 180 | TMessage Build(); |
| 181 | |
| 182 | /// <summary> |
| 183 | /// Like Build(), but does not throw an exception if the message is missing |
| 184 | /// required fields. Instead, a partial message is returned. |
| 185 | /// </summary> |
| 186 | TMessage BuildPartial(); |
| 187 | |
| 188 | /// <summary> |
| 189 | /// Clones this builder. |
| 190 | /// TODO(jonskeet): Explain depth of clone. |
| 191 | /// </summary> |
| 192 | TBuilder Clone(); |
| 193 | |
| 194 | /// <summary> |
| 195 | /// Parses a message of this type from the input and merges it with this |
| 196 | /// message, as if using MergeFrom(IMessage<T>). |
| 197 | /// </summary> |
| 198 | /// <remarks> |
| 199 | /// Warning: This does not verify that all required fields are present |
| 200 | /// in the input message. If you call Build() without setting all |
| 201 | /// required fields, it will throw an UninitializedMessageException. |
| 202 | /// There are a few good ways to deal with this: |
| 203 | /// <list> |
| 204 | /// <item>Call IsInitialized to verify to verify that all required fields are |
| 205 | /// set before building.</item> |
| 206 | /// <item>Parse the message separately using one of the static ParseFrom |
| 207 | /// methods, then use MergeFrom(IMessage<T>) to merge it with |
| 208 | /// this one. ParseFrom will throw an InvalidProtocolBufferException |
| 209 | /// (an IOException) if some required fields are missing. |
| 210 | /// Use BuildPartial to build, which ignores missing required fields. |
| 211 | /// </list> |
| 212 | /// </remarks> |
| 213 | TBuilder MergeFrom(CodedInputStream input); |
| 214 | |
| 215 | /// <summary> |
| 216 | /// Like MergeFrom(CodedInputStream), but also parses extensions. |
| 217 | /// The extensions that you want to be able to parse must be registered |
| 218 | /// in <paramref name="extensionRegistry"/>. Extensions not in the registry |
| 219 | /// will be treated as unknown fields. |
| 220 | /// </summary> |
| 221 | TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry); |
| 222 | |
| 223 | /// <summary> |
| 224 | /// Get's the message's type's default instance. |
| 225 | /// <see cref="IMessage{TMessage}.DefaultInstanceForType" /> |
| 226 | /// </summary> |
| 227 | TMessage DefaultInstanceForType { get; } |
| 228 | |
| 229 | /// <summary> |
| 230 | /// Clears the field. This is exactly equivalent to calling the generated |
| 231 | /// Clear method corresponding to the field. |
| 232 | /// </summary> |
| 233 | /// <param name="field"></param> |
| 234 | /// <returns></returns> |
| 235 | TBuilder ClearField(FieldDescriptor field); |
| 236 | |
| 237 | /// <summary> |
| 238 | /// Appends the given value as a new element for the specified repeated field. |
| 239 | /// </summary> |
| 240 | /// <exception cref="ArgumentException">the field is not a repeated field, |
| 241 | /// the field does not belong to this builder's type, or the value is |
| 242 | /// of the incorrect type |
| 243 | /// </exception> |
| 244 | TBuilder AddRepeatedField(FieldDescriptor field, object value); |
| 245 | |
| 246 | /// <summary> |
| 247 | /// Merge some unknown fields into the set for this message. |
| 248 | /// </summary> |
| 249 | TBuilder MergeUnknownFields(UnknownFieldSet unknownFields); |
Jon Skeet | 2e6dc12 | 2009-05-29 06:34:52 +0100 | [diff] [blame^] | 250 | |
| 251 | /// <summary> |
| 252 | /// Like MergeFrom(Stream), but does not read until the end of the file. |
| 253 | /// Instead, the size of the message (encoded as a varint) is read first, |
| 254 | /// then the message data. Use Message.WriteDelimitedTo(Stream) to |
| 255 | /// write messages in this format. |
| 256 | /// </summary> |
| 257 | /// <param name="input"></param> |
| 258 | TBuilder MergeDelimitedFrom(Stream input); |
| 259 | |
| 260 | /// <summary> |
| 261 | /// Like MergeDelimitedFrom(Stream) but supporting extensions. |
| 262 | /// </summary> |
| 263 | TBuilder MergeDelimitedFrom(Stream input, ExtensionRegistry extensionRegistry); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 264 | |
| 265 | #region Convenience methods |
| 266 | /// <summary> |
| 267 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 268 | /// it with the message being built. This is just a small wrapper around |
| 269 | /// MergeFrom(CodedInputStream). |
| 270 | /// </summary> |
| 271 | TBuilder MergeFrom(ByteString data); |
| 272 | |
| 273 | /// <summary> |
| 274 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 275 | /// it with the message being built. This is just a small wrapper around |
| 276 | /// MergeFrom(CodedInputStream, ExtensionRegistry). |
| 277 | /// </summary> |
| 278 | TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry); |
| 279 | |
| 280 | /// <summary> |
| 281 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 282 | /// it with the message being built. This is just a small wrapper around |
| 283 | /// MergeFrom(CodedInputStream). |
| 284 | /// </summary> |
| 285 | TBuilder MergeFrom(byte[] data); |
| 286 | |
| 287 | /// <summary> |
| 288 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 289 | /// it with the message being built. This is just a small wrapper around |
| 290 | /// MergeFrom(CodedInputStream, ExtensionRegistry). |
| 291 | /// </summary> |
| 292 | TBuilder MergeFrom(byte[] data, ExtensionRegistry extensionRegistry); |
| 293 | |
| 294 | /// <summary> |
| 295 | /// Parse <paramref name="input"/> as a message of this type and merge |
| 296 | /// it with the message being built. This is just a small wrapper around |
| 297 | /// MergeFrom(CodedInputStream). Note that this method always reads |
| 298 | /// the entire input (unless it throws an exception). If you want it to |
| 299 | /// stop earlier, you will need to wrap the input in a wrapper |
Jon Skeet | 2e6dc12 | 2009-05-29 06:34:52 +0100 | [diff] [blame^] | 300 | /// stream which limits reading. Or, use IMessage.WriteDelimitedTo(Stream) |
| 301 | /// to write your message and MmergeDelimitedFrom(Stream) to read it. |
| 302 | /// Despite usually reading the entire stream, this method never closes the stream. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 303 | /// </summary> |
| 304 | TBuilder MergeFrom(Stream input); |
| 305 | |
| 306 | /// <summary> |
| 307 | /// Parse <paramref name="input"/> as a message of this type and merge |
| 308 | /// it with the message being built. This is just a small wrapper around |
| 309 | /// MergeFrom(CodedInputStream, ExtensionRegistry). |
| 310 | /// </summary> |
| 311 | TBuilder MergeFrom(Stream input, ExtensionRegistry extensionRegistry); |
| 312 | #endregion |
| 313 | } |
| 314 | } |