Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 11 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 21 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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. |
Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 33 | #endregion |
| 34 | |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 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 used for all parts of the API which don't require |
| 44 | /// any type knowledge. |
| 45 | /// </summary> |
| 46 | public interface IMessage { |
| 47 | /// <summary> |
| 48 | /// Returns the message's type's descriptor. This differs from the |
| 49 | /// Descriptor property of each generated message class in that this |
| 50 | /// method is an abstract method of IMessage whereas Descriptor is |
| 51 | /// a static property of a specific class. They return the same thing. |
| 52 | /// </summary> |
| 53 | MessageDescriptor DescriptorForType { get; } |
| 54 | /// <summary> |
| 55 | /// Returns a collection of all the fields in this message which are set |
| 56 | /// and their corresponding values. A singular ("required" or "optional") |
| 57 | /// field is set iff HasField() returns true for that field. A "repeated" |
| 58 | /// field is set iff GetRepeatedFieldSize() is greater than zero. The |
| 59 | /// values are exactly what would be returned by calling |
| 60 | /// GetField(FieldDescriptor) for each field. The map |
| 61 | /// is guaranteed to be a sorted map, so iterating over it will return fields |
| 62 | /// in order by field number. |
| 63 | /// </summary> |
| 64 | IDictionary<FieldDescriptor, object> AllFields { get; } |
| 65 | |
| 66 | /// <summary> |
| 67 | /// Returns true if the given field is set. This is exactly equivalent |
| 68 | /// to calling the generated "Has" property corresponding to the field. |
| 69 | /// </summary> |
| 70 | /// <exception cref="ArgumentException">the field is a repeated field, |
| 71 | /// or it's not a field of this type</exception> |
| 72 | bool HasField(FieldDescriptor field); |
| 73 | |
| 74 | /// <summary> |
| 75 | /// Obtains the value of the given field, or the default value if |
| 76 | /// it isn't set. For value type fields, the boxed value is returned. |
| 77 | /// For enum fields, the EnumValueDescriptor for the enum is returned. |
| 78 | /// For embedded message fields, the sub-message |
| 79 | /// is returned. For repeated fields, an IList<T> is returned. |
| 80 | /// </summary> |
| 81 | object this[FieldDescriptor field] { get; } |
| 82 | |
| 83 | /// <summary> |
| 84 | /// Returns the number of elements of a repeated field. This is |
| 85 | /// exactly equivalent to calling the generated "Count" property |
| 86 | /// corresponding to the field. |
| 87 | /// </summary> |
| 88 | /// <exception cref="ArgumentException">the field is not a repeated field, |
| 89 | /// or it's not a field of this type</exception> |
| 90 | int GetRepeatedFieldCount(FieldDescriptor field); |
| 91 | |
| 92 | /// <summary> |
| 93 | /// Gets an element of a repeated field. For value type fields |
Jon Skeet | a376734 | 2009-01-16 18:06:56 +0000 | [diff] [blame] | 94 | /// excluding enums, the boxed value is returned. For embedded |
| 95 | /// message fields, the sub-message is returned. For enums, the |
| 96 | /// relevant EnumValueDescriptor is returned. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 97 | /// </summary> |
| 98 | /// <exception cref="ArgumentException">the field is not a repeated field, |
| 99 | /// or it's not a field of this type</exception> |
| 100 | /// <exception cref="ArgumentOutOfRangeException">the index is out of |
| 101 | /// range for the repeated field's value</exception> |
| 102 | object this[FieldDescriptor field, int index] { get; } |
| 103 | |
| 104 | /// <summary> |
| 105 | /// Returns the unknown fields for this message. |
| 106 | /// </summary> |
| 107 | UnknownFieldSet UnknownFields { get; } |
| 108 | |
| 109 | /// <summary> |
| 110 | /// Returns true iff all required fields in the message and all embedded |
| 111 | /// messages are set. |
| 112 | /// </summary> |
| 113 | bool IsInitialized { get; } |
| 114 | |
| 115 | /// <summary> |
| 116 | /// Serializes the message and writes it to the given output stream. |
| 117 | /// This does not flush or close the stream. |
| 118 | /// </summary> |
Jon Skeet | 2e6dc12 | 2009-05-29 06:34:52 +0100 | [diff] [blame] | 119 | /// <remarks> |
| 120 | /// Protocol Buffers are not self-delimiting. Therefore, if you write |
| 121 | /// any more data to the stream after the message, you must somehow ensure |
| 122 | /// that the parser on the receiving end does not interpret this as being |
| 123 | /// part of the protocol message. One way of doing this is by writing the size |
| 124 | /// of the message before the data, then making sure you limit the input to |
| 125 | /// that size when receiving the data. Alternatively, use WriteDelimitedTo(Stream). |
| 126 | /// </remarks> |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 127 | void WriteTo(CodedOutputStream output); |
| 128 | |
| 129 | /// <summary> |
Jon Skeet | 2e6dc12 | 2009-05-29 06:34:52 +0100 | [diff] [blame] | 130 | /// Like WriteTo(Stream) but writes the size of the message as a varint before |
| 131 | /// writing the data. This allows more data to be written to the stream after the |
| 132 | /// message without the need to delimit the message data yourself. Use |
| 133 | /// IBuilder.MergeDelimitedFrom(Stream) or the static method |
| 134 | /// YourMessageType.ParseDelimitedFrom(Stream) to parse messages written by this method. |
| 135 | /// </summary> |
| 136 | /// <param name="output"></param> |
| 137 | void WriteDelimitedTo(Stream output); |
| 138 | |
| 139 | /// <summary> |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 140 | /// Returns the number of bytes required to encode this message. |
| 141 | /// The result is only computed on the first call and memoized after that. |
| 142 | /// </summary> |
| 143 | int SerializedSize { get; } |
| 144 | |
| 145 | #region Comparison and hashing |
| 146 | /// <summary> |
| 147 | /// Compares the specified object with this message for equality. |
| 148 | /// Returns true iff the given object is a message of the same type |
| 149 | /// (as defined by DescriptorForType) and has identical values |
| 150 | /// for all its fields. |
| 151 | /// </summary> |
| 152 | bool Equals(object other); |
| 153 | |
| 154 | /// <summary> |
| 155 | /// Returns the hash code value for this message. |
| 156 | /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one! |
| 157 | /// </summary> |
| 158 | int GetHashCode(); |
| 159 | #endregion |
| 160 | |
| 161 | #region Convenience methods |
| 162 | /// <summary> |
| 163 | /// Converts the message to a string in protocol buffer text format. |
| 164 | /// This is just a trivial wrapper around TextFormat.PrintToString. |
| 165 | /// </summary> |
| 166 | string ToString(); |
| 167 | |
| 168 | /// <summary> |
| 169 | /// Serializes the message to a ByteString. This is a trivial wrapper |
| 170 | /// around WriteTo(CodedOutputStream). |
| 171 | /// </summary> |
| 172 | ByteString ToByteString(); |
| 173 | |
| 174 | /// <summary> |
| 175 | /// Serializes the message to a byte array. This is a trivial wrapper |
| 176 | /// around WriteTo(CodedOutputStream). |
| 177 | /// </summary> |
| 178 | byte[] ToByteArray(); |
| 179 | |
| 180 | /// <summary> |
| 181 | /// Serializes the message and writes it to the given stream. |
| 182 | /// This is just a wrapper around WriteTo(CodedOutputStream). This |
| 183 | /// does not flush or close the stream. |
| 184 | /// </summary> |
| 185 | /// <param name="output"></param> |
| 186 | void WriteTo(Stream output); |
| 187 | #endregion |
| 188 | |
| 189 | /// <summary> |
| 190 | /// Creates a builder for the type, but in a weakly typed manner. This |
Jon Skeet | e81a9d7 | 2009-02-24 16:50:56 +0000 | [diff] [blame] | 191 | /// is typically implemented by strongly typed messages by just returning |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 192 | /// the result of CreateBuilderForType. |
| 193 | /// </summary> |
| 194 | IBuilder WeakCreateBuilderForType(); |
| 195 | |
Jon Skeet | e81a9d7 | 2009-02-24 16:50:56 +0000 | [diff] [blame] | 196 | /// <summary> |
| 197 | /// Creates a builder with the same contents as this message. This |
| 198 | /// is typically implemented by strongly typed messages by just returning |
| 199 | /// the result of ToBuilder. |
| 200 | /// </summary> |
| 201 | IBuilder WeakToBuilder(); |
| 202 | |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 203 | IMessage WeakDefaultInstanceForType { get; } |
| 204 | } |
| 205 | |
| 206 | public interface IMessage<TMessage> : IMessage { |
| 207 | /// <summary> |
| 208 | /// Returns an instance of this message type with all fields set to |
| 209 | /// their default values. This may or may not be a singleton. This differs |
| 210 | /// from the DefaultInstance property of each generated message class in that this |
| 211 | /// method is an abstract method of IMessage whereas DefaultInstance is |
| 212 | /// a static property of a specific class. They return the same thing. |
| 213 | /// </summary> |
| 214 | TMessage DefaultInstanceForType { get; } |
| 215 | } |
| 216 | |
| 217 | /// <summary> |
| 218 | /// Type-safe interface for all generated messages to implement. |
| 219 | /// </summary> |
| 220 | public interface IMessage<TMessage, TBuilder> : IMessage<TMessage> |
| 221 | where TMessage : IMessage<TMessage, TBuilder> |
| 222 | where TBuilder : IBuilder<TMessage, TBuilder> { |
| 223 | #region Builders |
| 224 | /// <summary> |
| 225 | /// Constructs a new builder for a message of the same type as this message. |
| 226 | /// </summary> |
| 227 | TBuilder CreateBuilderForType(); |
Jon Skeet | e81a9d7 | 2009-02-24 16:50:56 +0000 | [diff] [blame] | 228 | /// <summary> |
| 229 | /// Creates a builder with the same contents as this current instance. |
| 230 | /// This is typically implemented by strongly typed messages by just |
| 231 | /// returning the result of ToBuilder(). |
| 232 | /// </summary> |
| 233 | TBuilder ToBuilder(); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 234 | #endregion |
| 235 | } |
| 236 | } |