blob: 203b71a4ae464d67ff74510589812957f384b384 [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeet60c059b2008-10-23 21:17:56 +01002// 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 Skeet68036862008-10-22 13:30:34 +01006// http://code.google.com/p/protobuf/
7//
Jon Skeet60c059b2008-10-23 21:17:56 +01008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
Jon Skeet68036862008-10-22 13:30:34 +010011//
Jon Skeet60c059b2008-10-23 21:17:56 +010012// * 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 Skeet68036862008-10-22 13:30:34 +010021//
Jon Skeet60c059b2008-10-23 21:17:56 +010022// 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 Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet68036862008-10-22 13:30:34 +010035using System.Collections;
36using System.Collections.Generic;
37using System.IO;
38using Google.ProtocolBuffers.Collections;
39using Google.ProtocolBuffers.Descriptors;
40
41namespace Google.ProtocolBuffers {
42 /// <summary>
43 /// Implementation of the non-generic IMessage interface as far as possible.
44 /// </summary>
csharptest80e73b92010-11-05 14:49:08 -050045 public abstract class AbstractMessage<TMessage, TBuilder> : AbstractMessageLite<TMessage, TBuilder>, IMessage<TMessage, TBuilder>
Jon Skeet68036862008-10-22 13:30:34 +010046 where TMessage : AbstractMessage<TMessage, TBuilder>
47 where TBuilder : AbstractBuilder<TMessage, TBuilder> {
48 /// <summary>
49 /// The serialized size if it's already been computed, or null
50 /// if we haven't computed it yet.
51 /// </summary>
52 private int? memoizedSize = null;
53
54 #region Unimplemented members of IMessage
55 public abstract MessageDescriptor DescriptorForType { get; }
56 public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
57 public abstract bool HasField(FieldDescriptor field);
58 public abstract object this[FieldDescriptor field] { get; }
59 public abstract int GetRepeatedFieldCount(FieldDescriptor field);
60 public abstract object this[FieldDescriptor field, int index] { get; }
61 public abstract UnknownFieldSet UnknownFields { get; }
Jon Skeet68036862008-10-22 13:30:34 +010062 #endregion
Jon Skeet68036862008-10-22 13:30:34 +010063
csharptest80e73b92010-11-05 14:49:08 -050064 /// <summary>
65 /// Returns true iff all required fields in the message and all embedded
66 /// messages are set.
67 /// </summary>
68 public override bool IsInitialized {
Jon Skeet68036862008-10-22 13:30:34 +010069 get {
70 // Check that all required fields are present.
71 foreach (FieldDescriptor field in DescriptorForType.Fields) {
72 if (field.IsRequired && !HasField(field)) {
73 return false;
74 }
75 }
76
77 // Check that embedded messages are initialized.
78 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
79 FieldDescriptor field = entry.Key;
80 if (field.MappedType == MappedType.Message) {
81 if (field.IsRepeated) {
82 // We know it's an IList<T>, but not the exact type - so
83 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
csharptest7d396f92010-11-08 20:06:46 -060084 foreach (IMessageLite element in (IEnumerable)entry.Value) {
Jon Skeet68036862008-10-22 13:30:34 +010085 if (!element.IsInitialized) {
86 return false;
87 }
88 }
89 } else {
csharptest7d396f92010-11-08 20:06:46 -060090 if (!((IMessageLite)entry.Value).IsInitialized) {
Jon Skeet68036862008-10-22 13:30:34 +010091 return false;
92 }
93 }
94 }
95 }
96 return true;
97 }
98 }
99
100 public sealed override string ToString() {
101 return TextFormat.PrintToString(this);
102 }
103
csharptest272cb8a2010-11-09 20:49:12 -0600104 public sealed override void PrintTo(TextWriter writer) {
105 TextFormat.Print(this, writer);
106 }
107
csharptest80e73b92010-11-05 14:49:08 -0500108 /// <summary>
109 /// Serializes the message and writes it to the given output stream.
110 /// This does not flush or close the stream.
111 /// </summary>
112 /// <remarks>
113 /// Protocol Buffers are not self-delimiting. Therefore, if you write
114 /// any more data to the stream after the message, you must somehow ensure
115 /// that the parser on the receiving end does not interpret this as being
116 /// part of the protocol message. One way of doing this is by writing the size
117 /// of the message before the data, then making sure you limit the input to
118 /// that size when receiving the data. Alternatively, use WriteDelimitedTo(Stream).
119 /// </remarks>
120 public override void WriteTo(CodedOutputStream output) {
Jon Skeet68036862008-10-22 13:30:34 +0100121 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
122 FieldDescriptor field = entry.Key;
123 if (field.IsRepeated) {
124 // We know it's an IList<T>, but not the exact type - so
125 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
Jon Skeet25a28582009-02-18 16:06:22 +0000126 IEnumerable valueList = (IEnumerable) entry.Value;
127 if (field.IsPacked) {
128 output.WriteTag(field.FieldNumber, WireFormat.WireType.LengthDelimited);
129 int dataSize = 0;
130 foreach (object element in valueList) {
131 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
132 }
133 output.WriteRawVarint32((uint)dataSize);
134 foreach (object element in valueList) {
135 output.WriteFieldNoTag(field.FieldType, element);
136 }
137 } else {
138 foreach (object element in valueList) {
139 output.WriteField(field.FieldType, field.FieldNumber, element);
140 }
Jon Skeet68036862008-10-22 13:30:34 +0100141 }
142 } else {
143 output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
144 }
145 }
146
147 UnknownFieldSet unknownFields = UnknownFields;
148 if (DescriptorForType.Options.MessageSetWireFormat) {
149 unknownFields.WriteAsMessageSetTo(output);
150 } else {
151 unknownFields.WriteTo(output);
152 }
153 }
154
csharptest80e73b92010-11-05 14:49:08 -0500155 /// <summary>
156 /// Returns the number of bytes required to encode this message.
157 /// The result is only computed on the first call and memoized after that.
158 /// </summary>
159 public override int SerializedSize {
Jon Skeet68036862008-10-22 13:30:34 +0100160 get {
161 if (memoizedSize != null) {
162 return memoizedSize.Value;
163 }
164
165 int size = 0;
166 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
167 FieldDescriptor field = entry.Key;
168 if (field.IsRepeated) {
Jon Skeet25a28582009-02-18 16:06:22 +0000169 IEnumerable valueList = (IEnumerable) entry.Value;
170 if (field.IsPacked) {
171 int dataSize = 0;
172 foreach (object element in valueList) {
173 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
174 }
175 size += dataSize;
176 size += CodedOutputStream.ComputeTagSize(field.FieldNumber);
177 size += CodedOutputStream.ComputeRawVarint32Size((uint)dataSize);
178 } else {
179 foreach (object element in valueList) {
180 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
181 }
Jon Skeet68036862008-10-22 13:30:34 +0100182 }
183 } else {
184 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
185 }
186 }
187
188 UnknownFieldSet unknownFields = UnknownFields;
189 if (DescriptorForType.Options.MessageSetWireFormat) {
190 size += unknownFields.SerializedSizeAsMessageSet;
191 } else {
192 size += unknownFields.SerializedSize;
193 }
194
195 memoizedSize = size;
196 return size;
197 }
198 }
199
csharptest80e73b92010-11-05 14:49:08 -0500200 /// <summary>
201 /// Compares the specified object with this message for equality.
202 /// Returns true iff the given object is a message of the same type
203 /// (as defined by DescriptorForType) and has identical values
204 /// for all its fields.
205 /// </summary>
Jon Skeet68036862008-10-22 13:30:34 +0100206 public override bool Equals(object other) {
207 if (other == this) {
208 return true;
209 }
210 IMessage otherMessage = other as IMessage;
211 if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
212 return false;
213 }
Jon Skeet43da7ae2009-05-28 21:45:43 +0100214 return Dictionaries.Equals(AllFields, otherMessage.AllFields) && UnknownFields.Equals(otherMessage.UnknownFields);
Jon Skeet68036862008-10-22 13:30:34 +0100215 }
216
csharptest80e73b92010-11-05 14:49:08 -0500217 /// <summary>
218 /// Returns the hash code value for this message.
219 /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
220 /// </summary>
Jon Skeet68036862008-10-22 13:30:34 +0100221 public override int GetHashCode() {
222 int hash = 41;
223 hash = (19 * hash) + DescriptorForType.GetHashCode();
224 hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
Jon Skeet43da7ae2009-05-28 21:45:43 +0100225 hash = (29 * hash) + UnknownFields.GetHashCode();
Jon Skeet68036862008-10-22 13:30:34 +0100226 return hash;
227 }
csharptestd9c59e62010-11-04 19:36:28 -0500228
csharptest80e73b92010-11-05 14:49:08 -0500229 #region Explicit Members
230
231 IBuilder IMessage.WeakCreateBuilderForType() {
232 return CreateBuilderForType();
csharptestd9c59e62010-11-04 19:36:28 -0500233 }
csharptest80e73b92010-11-05 14:49:08 -0500234
235 IBuilder IMessage.WeakToBuilder() {
236 return ToBuilder();
237 }
238
239 IMessage IMessage.WeakDefaultInstanceForType {
240 get { return DefaultInstanceForType; }
241 }
242
243 #endregion
Jon Skeet68036862008-10-22 13:30:34 +0100244 }
245}