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