blob: e2cf38274648c2ed67096365642738b6e7a6763e [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
csharptest80e73b92010-11-05 14:49:08 -0500104 /// <summary>
105 /// Serializes the message and writes it to the given output stream.
106 /// This does not flush or close the stream.
107 /// </summary>
108 /// <remarks>
109 /// Protocol Buffers are not self-delimiting. Therefore, if you write
110 /// any more data to the stream after the message, you must somehow ensure
111 /// that the parser on the receiving end does not interpret this as being
112 /// part of the protocol message. One way of doing this is by writing the size
113 /// of the message before the data, then making sure you limit the input to
114 /// that size when receiving the data. Alternatively, use WriteDelimitedTo(Stream).
115 /// </remarks>
116 public override void WriteTo(CodedOutputStream output) {
Jon Skeet68036862008-10-22 13:30:34 +0100117 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
118 FieldDescriptor field = entry.Key;
119 if (field.IsRepeated) {
120 // We know it's an IList<T>, but not the exact type - so
121 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
Jon Skeet25a28582009-02-18 16:06:22 +0000122 IEnumerable valueList = (IEnumerable) entry.Value;
123 if (field.IsPacked) {
124 output.WriteTag(field.FieldNumber, WireFormat.WireType.LengthDelimited);
125 int dataSize = 0;
126 foreach (object element in valueList) {
127 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
128 }
129 output.WriteRawVarint32((uint)dataSize);
130 foreach (object element in valueList) {
131 output.WriteFieldNoTag(field.FieldType, element);
132 }
133 } else {
134 foreach (object element in valueList) {
135 output.WriteField(field.FieldType, field.FieldNumber, element);
136 }
Jon Skeet68036862008-10-22 13:30:34 +0100137 }
138 } else {
139 output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
140 }
141 }
142
143 UnknownFieldSet unknownFields = UnknownFields;
144 if (DescriptorForType.Options.MessageSetWireFormat) {
145 unknownFields.WriteAsMessageSetTo(output);
146 } else {
147 unknownFields.WriteTo(output);
148 }
149 }
150
csharptest80e73b92010-11-05 14:49:08 -0500151 /// <summary>
152 /// Returns the number of bytes required to encode this message.
153 /// The result is only computed on the first call and memoized after that.
154 /// </summary>
155 public override int SerializedSize {
Jon Skeet68036862008-10-22 13:30:34 +0100156 get {
157 if (memoizedSize != null) {
158 return memoizedSize.Value;
159 }
160
161 int size = 0;
162 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
163 FieldDescriptor field = entry.Key;
164 if (field.IsRepeated) {
Jon Skeet25a28582009-02-18 16:06:22 +0000165 IEnumerable valueList = (IEnumerable) entry.Value;
166 if (field.IsPacked) {
167 int dataSize = 0;
168 foreach (object element in valueList) {
169 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
170 }
171 size += dataSize;
172 size += CodedOutputStream.ComputeTagSize(field.FieldNumber);
173 size += CodedOutputStream.ComputeRawVarint32Size((uint)dataSize);
174 } else {
175 foreach (object element in valueList) {
176 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
177 }
Jon Skeet68036862008-10-22 13:30:34 +0100178 }
179 } else {
180 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
181 }
182 }
183
184 UnknownFieldSet unknownFields = UnknownFields;
185 if (DescriptorForType.Options.MessageSetWireFormat) {
186 size += unknownFields.SerializedSizeAsMessageSet;
187 } else {
188 size += unknownFields.SerializedSize;
189 }
190
191 memoizedSize = size;
192 return size;
193 }
194 }
195
csharptest80e73b92010-11-05 14:49:08 -0500196 /// <summary>
197 /// Compares the specified object with this message for equality.
198 /// Returns true iff the given object is a message of the same type
199 /// (as defined by DescriptorForType) and has identical values
200 /// for all its fields.
201 /// </summary>
Jon Skeet68036862008-10-22 13:30:34 +0100202 public override bool Equals(object other) {
203 if (other == this) {
204 return true;
205 }
206 IMessage otherMessage = other as IMessage;
207 if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
208 return false;
209 }
Jon Skeet43da7ae2009-05-28 21:45:43 +0100210 return Dictionaries.Equals(AllFields, otherMessage.AllFields) && UnknownFields.Equals(otherMessage.UnknownFields);
Jon Skeet68036862008-10-22 13:30:34 +0100211 }
212
csharptest80e73b92010-11-05 14:49:08 -0500213 /// <summary>
214 /// Returns the hash code value for this message.
215 /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
216 /// </summary>
Jon Skeet68036862008-10-22 13:30:34 +0100217 public override int GetHashCode() {
218 int hash = 41;
219 hash = (19 * hash) + DescriptorForType.GetHashCode();
220 hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
Jon Skeet43da7ae2009-05-28 21:45:43 +0100221 hash = (29 * hash) + UnknownFields.GetHashCode();
Jon Skeet68036862008-10-22 13:30:34 +0100222 return hash;
223 }
csharptestd9c59e62010-11-04 19:36:28 -0500224
csharptest80e73b92010-11-05 14:49:08 -0500225 #region Explicit Members
226
227 IBuilder IMessage.WeakCreateBuilderForType() {
228 return CreateBuilderForType();
csharptestd9c59e62010-11-04 19:36:28 -0500229 }
csharptest80e73b92010-11-05 14:49:08 -0500230
231 IBuilder IMessage.WeakToBuilder() {
232 return ToBuilder();
233 }
234
235 IMessage IMessage.WeakDefaultInstanceForType {
236 get { return DefaultInstanceForType; }
237 }
238
239 #endregion
Jon Skeet68036862008-10-22 13:30:34 +0100240 }
241}