blob: 1ce18a5c9e23a07508795a3a9ac8f729b501e0e2 [file] [log] [blame]
Jon Skeet68036862008-10-22 13:30:34 +01001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.
3// http://code.google.com/p/protobuf/
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16using System;
17using System.Collections.Generic;
18using System.IO;
19using Google.ProtocolBuffers.Descriptors;
20
21namespace Google.ProtocolBuffers {
22
23 /// <summary>
24 /// Non-generic interface used for all parts of the API which don't require
25 /// any type knowledge.
26 /// </summary>
27 public interface IMessage {
28 /// <summary>
29 /// Returns the message's type's descriptor. This differs from the
30 /// Descriptor property of each generated message class in that this
31 /// method is an abstract method of IMessage whereas Descriptor is
32 /// a static property of a specific class. They return the same thing.
33 /// </summary>
34 MessageDescriptor DescriptorForType { get; }
35 /// <summary>
36 /// Returns a collection of all the fields in this message which are set
37 /// and their corresponding values. A singular ("required" or "optional")
38 /// field is set iff HasField() returns true for that field. A "repeated"
39 /// field is set iff GetRepeatedFieldSize() is greater than zero. The
40 /// values are exactly what would be returned by calling
41 /// GetField(FieldDescriptor) for each field. The map
42 /// is guaranteed to be a sorted map, so iterating over it will return fields
43 /// in order by field number.
44 /// </summary>
45 IDictionary<FieldDescriptor, object> AllFields { get; }
46
47 /// <summary>
48 /// Returns true if the given field is set. This is exactly equivalent
49 /// to calling the generated "Has" property corresponding to the field.
50 /// </summary>
51 /// <exception cref="ArgumentException">the field is a repeated field,
52 /// or it's not a field of this type</exception>
53 bool HasField(FieldDescriptor field);
54
55 /// <summary>
56 /// Obtains the value of the given field, or the default value if
57 /// it isn't set. For value type fields, the boxed value is returned.
58 /// For enum fields, the EnumValueDescriptor for the enum is returned.
59 /// For embedded message fields, the sub-message
60 /// is returned. For repeated fields, an IList&lt;T&gt; is returned.
61 /// </summary>
62 object this[FieldDescriptor field] { get; }
63
64 /// <summary>
65 /// Returns the number of elements of a repeated field. This is
66 /// exactly equivalent to calling the generated "Count" property
67 /// corresponding to the field.
68 /// </summary>
69 /// <exception cref="ArgumentException">the field is not a repeated field,
70 /// or it's not a field of this type</exception>
71 int GetRepeatedFieldCount(FieldDescriptor field);
72
73 /// <summary>
74 /// Gets an element of a repeated field. For value type fields
75 /// including enums, the boxed value is returned. For embedded
76 /// message fields, the sub-message is returned.
77 /// </summary>
78 /// <exception cref="ArgumentException">the field is not a repeated field,
79 /// or it's not a field of this type</exception>
80 /// <exception cref="ArgumentOutOfRangeException">the index is out of
81 /// range for the repeated field's value</exception>
82 object this[FieldDescriptor field, int index] { get; }
83
84 /// <summary>
85 /// Returns the unknown fields for this message.
86 /// </summary>
87 UnknownFieldSet UnknownFields { get; }
88
89 /// <summary>
90 /// Returns true iff all required fields in the message and all embedded
91 /// messages are set.
92 /// </summary>
93 bool IsInitialized { get; }
94
95 /// <summary>
96 /// Serializes the message and writes it to the given output stream.
97 /// This does not flush or close the stream.
98 /// </summary>
99 /// <param name="output"></param>
100 void WriteTo(CodedOutputStream output);
101
102 /// <summary>
103 /// Returns the number of bytes required to encode this message.
104 /// The result is only computed on the first call and memoized after that.
105 /// </summary>
106 int SerializedSize { get; }
107
108 #region Comparison and hashing
109 /// <summary>
110 /// Compares the specified object with this message for equality.
111 /// Returns true iff the given object is a message of the same type
112 /// (as defined by DescriptorForType) and has identical values
113 /// for all its fields.
114 /// </summary>
115 bool Equals(object other);
116
117 /// <summary>
118 /// Returns the hash code value for this message.
119 /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
120 /// </summary>
121 int GetHashCode();
122 #endregion
123
124 #region Convenience methods
125 /// <summary>
126 /// Converts the message to a string in protocol buffer text format.
127 /// This is just a trivial wrapper around TextFormat.PrintToString.
128 /// </summary>
129 string ToString();
130
131 /// <summary>
132 /// Serializes the message to a ByteString. This is a trivial wrapper
133 /// around WriteTo(CodedOutputStream).
134 /// </summary>
135 ByteString ToByteString();
136
137 /// <summary>
138 /// Serializes the message to a byte array. This is a trivial wrapper
139 /// around WriteTo(CodedOutputStream).
140 /// </summary>
141 byte[] ToByteArray();
142
143 /// <summary>
144 /// Serializes the message and writes it to the given stream.
145 /// This is just a wrapper around WriteTo(CodedOutputStream). This
146 /// does not flush or close the stream.
147 /// </summary>
148 /// <param name="output"></param>
149 void WriteTo(Stream output);
150 #endregion
151
152 /// <summary>
153 /// Creates a builder for the type, but in a weakly typed manner. This
154 /// is typically implemented by strongly typed builders by just returning
155 /// the result of CreateBuilderForType.
156 /// </summary>
157 IBuilder WeakCreateBuilderForType();
158
159 IMessage WeakDefaultInstanceForType { get; }
160 }
161
162 public interface IMessage<TMessage> : IMessage {
163 /// <summary>
164 /// Returns an instance of this message type with all fields set to
165 /// their default values. This may or may not be a singleton. This differs
166 /// from the DefaultInstance property of each generated message class in that this
167 /// method is an abstract method of IMessage whereas DefaultInstance is
168 /// a static property of a specific class. They return the same thing.
169 /// </summary>
170 TMessage DefaultInstanceForType { get; }
171 }
172
173 /// <summary>
174 /// Type-safe interface for all generated messages to implement.
175 /// </summary>
176 public interface IMessage<TMessage, TBuilder> : IMessage<TMessage>
177 where TMessage : IMessage<TMessage, TBuilder>
178 where TBuilder : IBuilder<TMessage, TBuilder> {
179 #region Builders
180 /// <summary>
181 /// Constructs a new builder for a message of the same type as this message.
182 /// </summary>
183 TBuilder CreateBuilderForType();
184 #endregion
185 }
186}