blob: bad71d761d3ddf2cb233442be5530e470b69a236 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001// 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 Skeet68036862008-10-22 13:30:34 +01005// http://code.google.com/p/protobuf/
6//
Jon Skeet60c059b2008-10-23 21:17:56 +01007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
Jon Skeet68036862008-10-22 13:30:34 +010010//
Jon Skeet60c059b2008-10-23 21:17:56 +010011// * 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 Skeet68036862008-10-22 13:30:34 +010020//
Jon Skeet60c059b2008-10-23 21:17:56 +010021// 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 Skeet68036862008-10-22 13:30:34 +010032using System;
33using System.Collections.Generic;
34using System.IO;
35using Google.ProtocolBuffers.Descriptors;
36
37namespace Google.ProtocolBuffers {
38
39 /// <summary>
40 /// Non-generic interface used for all parts of the API which don't require
41 /// any type knowledge.
42 /// </summary>
43 public interface IMessage {
44 /// <summary>
45 /// Returns the message's type's descriptor. This differs from the
46 /// Descriptor property of each generated message class in that this
47 /// method is an abstract method of IMessage whereas Descriptor is
48 /// a static property of a specific class. They return the same thing.
49 /// </summary>
50 MessageDescriptor DescriptorForType { get; }
51 /// <summary>
52 /// Returns a collection of all the fields in this message which are set
53 /// and their corresponding values. A singular ("required" or "optional")
54 /// field is set iff HasField() returns true for that field. A "repeated"
55 /// field is set iff GetRepeatedFieldSize() is greater than zero. The
56 /// values are exactly what would be returned by calling
57 /// GetField(FieldDescriptor) for each field. The map
58 /// is guaranteed to be a sorted map, so iterating over it will return fields
59 /// in order by field number.
60 /// </summary>
61 IDictionary<FieldDescriptor, object> AllFields { get; }
62
63 /// <summary>
64 /// Returns true if the given field is set. This is exactly equivalent
65 /// to calling the generated "Has" property corresponding to the field.
66 /// </summary>
67 /// <exception cref="ArgumentException">the field is a repeated field,
68 /// or it's not a field of this type</exception>
69 bool HasField(FieldDescriptor field);
70
71 /// <summary>
72 /// Obtains the value of the given field, or the default value if
73 /// it isn't set. For value type fields, the boxed value is returned.
74 /// For enum fields, the EnumValueDescriptor for the enum is returned.
75 /// For embedded message fields, the sub-message
76 /// is returned. For repeated fields, an IList&lt;T&gt; is returned.
77 /// </summary>
78 object this[FieldDescriptor field] { get; }
79
80 /// <summary>
81 /// Returns the number of elements of a repeated field. This is
82 /// exactly equivalent to calling the generated "Count" property
83 /// corresponding to the field.
84 /// </summary>
85 /// <exception cref="ArgumentException">the field is not a repeated field,
86 /// or it's not a field of this type</exception>
87 int GetRepeatedFieldCount(FieldDescriptor field);
88
89 /// <summary>
90 /// Gets an element of a repeated field. For value type fields
91 /// including enums, the boxed value is returned. For embedded
92 /// message fields, the sub-message is returned.
93 /// </summary>
94 /// <exception cref="ArgumentException">the field is not a repeated field,
95 /// or it's not a field of this type</exception>
96 /// <exception cref="ArgumentOutOfRangeException">the index is out of
97 /// range for the repeated field's value</exception>
98 object this[FieldDescriptor field, int index] { get; }
99
100 /// <summary>
101 /// Returns the unknown fields for this message.
102 /// </summary>
103 UnknownFieldSet UnknownFields { get; }
104
105 /// <summary>
106 /// Returns true iff all required fields in the message and all embedded
107 /// messages are set.
108 /// </summary>
109 bool IsInitialized { get; }
110
111 /// <summary>
112 /// Serializes the message and writes it to the given output stream.
113 /// This does not flush or close the stream.
114 /// </summary>
115 /// <param name="output"></param>
116 void WriteTo(CodedOutputStream output);
117
118 /// <summary>
119 /// Returns the number of bytes required to encode this message.
120 /// The result is only computed on the first call and memoized after that.
121 /// </summary>
122 int SerializedSize { get; }
123
124 #region Comparison and hashing
125 /// <summary>
126 /// Compares the specified object with this message for equality.
127 /// Returns true iff the given object is a message of the same type
128 /// (as defined by DescriptorForType) and has identical values
129 /// for all its fields.
130 /// </summary>
131 bool Equals(object other);
132
133 /// <summary>
134 /// Returns the hash code value for this message.
135 /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
136 /// </summary>
137 int GetHashCode();
138 #endregion
139
140 #region Convenience methods
141 /// <summary>
142 /// Converts the message to a string in protocol buffer text format.
143 /// This is just a trivial wrapper around TextFormat.PrintToString.
144 /// </summary>
145 string ToString();
146
147 /// <summary>
148 /// Serializes the message to a ByteString. This is a trivial wrapper
149 /// around WriteTo(CodedOutputStream).
150 /// </summary>
151 ByteString ToByteString();
152
153 /// <summary>
154 /// Serializes the message to a byte array. This is a trivial wrapper
155 /// around WriteTo(CodedOutputStream).
156 /// </summary>
157 byte[] ToByteArray();
158
159 /// <summary>
160 /// Serializes the message and writes it to the given stream.
161 /// This is just a wrapper around WriteTo(CodedOutputStream). This
162 /// does not flush or close the stream.
163 /// </summary>
164 /// <param name="output"></param>
165 void WriteTo(Stream output);
166 #endregion
167
168 /// <summary>
169 /// Creates a builder for the type, but in a weakly typed manner. This
170 /// is typically implemented by strongly typed builders by just returning
171 /// the result of CreateBuilderForType.
172 /// </summary>
173 IBuilder WeakCreateBuilderForType();
174
175 IMessage WeakDefaultInstanceForType { get; }
176 }
177
178 public interface IMessage<TMessage> : IMessage {
179 /// <summary>
180 /// Returns an instance of this message type with all fields set to
181 /// their default values. This may or may not be a singleton. This differs
182 /// from the DefaultInstance property of each generated message class in that this
183 /// method is an abstract method of IMessage whereas DefaultInstance is
184 /// a static property of a specific class. They return the same thing.
185 /// </summary>
186 TMessage DefaultInstanceForType { get; }
187 }
188
189 /// <summary>
190 /// Type-safe interface for all generated messages to implement.
191 /// </summary>
192 public interface IMessage<TMessage, TBuilder> : IMessage<TMessage>
193 where TMessage : IMessage<TMessage, TBuilder>
194 where TBuilder : IBuilder<TMessage, TBuilder> {
195 #region Builders
196 /// <summary>
197 /// Constructs a new builder for a message of the same type as this message.
198 /// </summary>
199 TBuilder CreateBuilderForType();
200 #endregion
201 }
202}