csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 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; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 38 | |
| 39 | namespace Google.ProtocolBuffers { |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Non-generic interface for all members whose signatures don't require knowledge of |
| 43 | /// the type being built. The generic interface extends this one. Some methods return |
| 44 | /// either an IBuilder or an IMessage; in these cases the generic interface redeclares |
| 45 | /// the same method with a type-specific signature. Implementations are encouraged to |
| 46 | /// use explicit interface implemenation for the non-generic form. This mirrors |
| 47 | /// how IEnumerable and IEnumerable<T> work. |
| 48 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 49 | public interface IBuilderLite { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 50 | /// <summary> |
| 51 | /// Returns true iff all required fields in the message and all |
| 52 | /// embedded messages are set. |
| 53 | /// </summary> |
| 54 | bool IsInitialized { get; } |
| 55 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 56 | IBuilderLite WeakClear(); |
| 57 | IBuilderLite WeakMergeFrom(IMessageLite message); |
| 58 | IBuilderLite WeakMergeFrom(ByteString data); |
| 59 | IBuilderLite WeakMergeFrom(ByteString data, ExtensionRegistryLite registry); |
| 60 | IBuilderLite WeakMergeFrom(CodedInputStream input); |
| 61 | IBuilderLite WeakMergeFrom(CodedInputStream input, ExtensionRegistryLite registry); |
| 62 | IMessageLite WeakBuild(); |
| 63 | IMessageLite WeakBuildPartial(); |
| 64 | IBuilderLite WeakClone(); |
| 65 | IMessageLite WeakDefaultInstanceForType { get; } |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// <summary> |
| 69 | /// Interface implemented by Protocol Message builders. |
| 70 | /// TODO(jonskeet): Consider "SetXXX" methods returning the builder, as well as the properties. |
| 71 | /// </summary> |
| 72 | /// <typeparam name="TMessage">Type of message</typeparam> |
| 73 | /// <typeparam name="TBuilder">Type of builder</typeparam> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 74 | public interface IBuilderLite<TMessage, TBuilder> : IBuilderLite |
| 75 | where TMessage : IMessageLite<TMessage, TBuilder> |
| 76 | where TBuilder : IBuilderLite<TMessage, TBuilder> { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 77 | |
| 78 | /// <summary> |
| 79 | /// Resets all fields to their default values. |
| 80 | /// </summary> |
| 81 | TBuilder Clear(); |
| 82 | |
| 83 | /// <summary> |
| 84 | /// Merge the specified other message into the message being |
| 85 | /// built. Merging occurs as follows. For each field: |
| 86 | /// For singular primitive fields, if the field is set in <paramref name="other"/>, |
| 87 | /// then <paramref name="other"/>'s value overwrites the value in this message. |
| 88 | /// For singular message fields, if the field is set in <paramref name="other"/>, |
| 89 | /// it is merged into the corresponding sub-message of this message using the same |
| 90 | /// merging rules. |
| 91 | /// For repeated fields, the elements in <paramref name="other"/> are concatenated |
| 92 | /// with the elements in this message. |
| 93 | /// </summary> |
| 94 | /// <param name="other"></param> |
| 95 | /// <returns></returns> |
| 96 | TBuilder MergeFrom(TMessage other); |
| 97 | |
| 98 | /// <summary> |
| 99 | /// Merge the specified other message which may be a different implementation of |
| 100 | /// the same message descriptor. |
| 101 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 102 | TBuilder MergeFrom(IMessageLite other); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 103 | |
| 104 | /// <summary> |
| 105 | /// Constructs the final message. Once this is called, this Builder instance |
| 106 | /// is no longer valid, and calling any other method may throw a |
| 107 | /// NullReferenceException. If you need to continue working with the builder |
| 108 | /// after calling Build, call Clone first. |
| 109 | /// </summary> |
| 110 | /// <exception cref="UninitializedMessageException">the message |
| 111 | /// is missing one or more required fields; use BuildPartial to bypass |
| 112 | /// this check</exception> |
| 113 | TMessage Build(); |
| 114 | |
| 115 | /// <summary> |
| 116 | /// Like Build(), but does not throw an exception if the message is missing |
| 117 | /// required fields. Instead, a partial message is returned. |
| 118 | /// </summary> |
| 119 | TMessage BuildPartial(); |
| 120 | |
| 121 | /// <summary> |
| 122 | /// Clones this builder. |
| 123 | /// TODO(jonskeet): Explain depth of clone. |
| 124 | /// </summary> |
| 125 | TBuilder Clone(); |
| 126 | |
| 127 | /// <summary> |
| 128 | /// Parses a message of this type from the input and merges it with this |
| 129 | /// message, as if using MergeFrom(IMessage<T>). |
| 130 | /// </summary> |
| 131 | /// <remarks> |
| 132 | /// Warning: This does not verify that all required fields are present |
| 133 | /// in the input message. If you call Build() without setting all |
| 134 | /// required fields, it will throw an UninitializedMessageException. |
| 135 | /// There are a few good ways to deal with this: |
| 136 | /// <list> |
| 137 | /// <item>Call IsInitialized to verify to verify that all required fields are |
| 138 | /// set before building.</item> |
| 139 | /// <item>Parse the message separately using one of the static ParseFrom |
| 140 | /// methods, then use MergeFrom(IMessage<T>) to merge it with |
| 141 | /// this one. ParseFrom will throw an InvalidProtocolBufferException |
| 142 | /// (an IOException) if some required fields are missing. |
| 143 | /// Use BuildPartial to build, which ignores missing required fields. |
| 144 | /// </list> |
| 145 | /// </remarks> |
| 146 | TBuilder MergeFrom(CodedInputStream input); |
| 147 | |
| 148 | /// <summary> |
| 149 | /// Like MergeFrom(CodedInputStream), but also parses extensions. |
| 150 | /// The extensions that you want to be able to parse must be registered |
| 151 | /// in <paramref name="extensionRegistry"/>. Extensions not in the registry |
| 152 | /// will be treated as unknown fields. |
| 153 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 154 | TBuilder MergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 155 | |
| 156 | /// <summary> |
| 157 | /// Get's the message's type's default instance. |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 158 | /// <see cref="IMessageLite{TMessage}.DefaultInstanceForType" /> |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 159 | /// </summary> |
| 160 | TMessage DefaultInstanceForType { get; } |
| 161 | |
| 162 | /// <summary> |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 163 | /// Like MergeFrom(Stream), but does not read until the end of the file. |
| 164 | /// Instead, the size of the message (encoded as a varint) is read first, |
| 165 | /// then the message data. Use Message.WriteDelimitedTo(Stream) to |
| 166 | /// write messages in this format. |
| 167 | /// </summary> |
| 168 | /// <param name="input"></param> |
| 169 | TBuilder MergeDelimitedFrom(Stream input); |
| 170 | |
| 171 | /// <summary> |
| 172 | /// Like MergeDelimitedFrom(Stream) but supporting extensions. |
| 173 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 174 | TBuilder MergeDelimitedFrom(Stream input, ExtensionRegistryLite extensionRegistry); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 175 | |
| 176 | #region Convenience methods |
| 177 | /// <summary> |
| 178 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 179 | /// it with the message being built. This is just a small wrapper around |
| 180 | /// MergeFrom(CodedInputStream). |
| 181 | /// </summary> |
| 182 | TBuilder MergeFrom(ByteString data); |
| 183 | |
| 184 | /// <summary> |
| 185 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 186 | /// it with the message being built. This is just a small wrapper around |
| 187 | /// MergeFrom(CodedInputStream, ExtensionRegistry). |
| 188 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 189 | TBuilder MergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 190 | |
| 191 | /// <summary> |
| 192 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 193 | /// it with the message being built. This is just a small wrapper around |
| 194 | /// MergeFrom(CodedInputStream). |
| 195 | /// </summary> |
| 196 | TBuilder MergeFrom(byte[] data); |
| 197 | |
| 198 | /// <summary> |
| 199 | /// Parse <paramref name="data"/> as a message of this type and merge |
| 200 | /// it with the message being built. This is just a small wrapper around |
| 201 | /// MergeFrom(CodedInputStream, ExtensionRegistry). |
| 202 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 203 | TBuilder MergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 204 | |
| 205 | /// <summary> |
| 206 | /// Parse <paramref name="input"/> as a message of this type and merge |
| 207 | /// it with the message being built. This is just a small wrapper around |
| 208 | /// MergeFrom(CodedInputStream). Note that this method always reads |
| 209 | /// the entire input (unless it throws an exception). If you want it to |
| 210 | /// stop earlier, you will need to wrap the input in a wrapper |
| 211 | /// stream which limits reading. Or, use IMessage.WriteDelimitedTo(Stream) |
| 212 | /// to write your message and MmergeDelimitedFrom(Stream) to read it. |
| 213 | /// Despite usually reading the entire stream, this method never closes the stream. |
| 214 | /// </summary> |
| 215 | TBuilder MergeFrom(Stream input); |
| 216 | |
| 217 | /// <summary> |
| 218 | /// Parse <paramref name="input"/> as a message of this type and merge |
| 219 | /// it with the message being built. This is just a small wrapper around |
| 220 | /// MergeFrom(CodedInputStream, ExtensionRegistry). |
| 221 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 222 | TBuilder MergeFrom(Stream input, ExtensionRegistryLite extensionRegistry); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 223 | #endregion |
| 224 | } |
| 225 | } |