blob: 9cb38c3c79a5333ba92fb0edea87822f65efc888 [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>
45 public abstract class AbstractMessage<TMessage, TBuilder> : IMessage<TMessage, TBuilder>
46 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; }
62 public abstract TMessage DefaultInstanceForType { get; }
63 public abstract TBuilder CreateBuilderForType();
Jon Skeete81a9d72009-02-24 16:50:56 +000064 public abstract TBuilder ToBuilder();
Jon Skeet68036862008-10-22 13:30:34 +010065 #endregion
66
67 public IBuilder WeakCreateBuilderForType() {
68 return CreateBuilderForType();
69 }
70
Jon Skeete81a9d72009-02-24 16:50:56 +000071 public IBuilder WeakToBuilder() {
72 return ToBuilder();
73 }
74
csharptestd9c59e62010-11-04 19:36:28 -050075 IMessageLite IMessageLite.WeakDefaultInstanceForType {
76 get { return DefaultInstanceForType; }
77 }
78
Jon Skeet68036862008-10-22 13:30:34 +010079 public IMessage WeakDefaultInstanceForType {
80 get { return DefaultInstanceForType; }
81 }
82
83 public virtual bool IsInitialized {
84 get {
85 // Check that all required fields are present.
86 foreach (FieldDescriptor field in DescriptorForType.Fields) {
87 if (field.IsRequired && !HasField(field)) {
88 return false;
89 }
90 }
91
92 // Check that embedded messages are initialized.
93 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
94 FieldDescriptor field = entry.Key;
95 if (field.MappedType == MappedType.Message) {
96 if (field.IsRepeated) {
97 // We know it's an IList<T>, but not the exact type - so
98 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
99 foreach (IMessage element in (IEnumerable) entry.Value) {
100 if (!element.IsInitialized) {
101 return false;
102 }
103 }
104 } else {
105 if (!((IMessage)entry.Value).IsInitialized) {
106 return false;
107 }
108 }
109 }
110 }
111 return true;
112 }
113 }
114
115 public sealed override string ToString() {
116 return TextFormat.PrintToString(this);
117 }
118
119 public virtual void WriteTo(CodedOutputStream output) {
120 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
121 FieldDescriptor field = entry.Key;
122 if (field.IsRepeated) {
123 // We know it's an IList<T>, but not the exact type - so
124 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
Jon Skeet25a28582009-02-18 16:06:22 +0000125 IEnumerable valueList = (IEnumerable) entry.Value;
126 if (field.IsPacked) {
127 output.WriteTag(field.FieldNumber, WireFormat.WireType.LengthDelimited);
128 int dataSize = 0;
129 foreach (object element in valueList) {
130 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
131 }
132 output.WriteRawVarint32((uint)dataSize);
133 foreach (object element in valueList) {
134 output.WriteFieldNoTag(field.FieldType, element);
135 }
136 } else {
137 foreach (object element in valueList) {
138 output.WriteField(field.FieldType, field.FieldNumber, element);
139 }
Jon Skeet68036862008-10-22 13:30:34 +0100140 }
141 } else {
142 output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
143 }
144 }
145
146 UnknownFieldSet unknownFields = UnknownFields;
147 if (DescriptorForType.Options.MessageSetWireFormat) {
148 unknownFields.WriteAsMessageSetTo(output);
149 } else {
150 unknownFields.WriteTo(output);
151 }
152 }
153
154 public virtual int SerializedSize {
155 get {
156 if (memoizedSize != null) {
157 return memoizedSize.Value;
158 }
159
160 int size = 0;
161 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
162 FieldDescriptor field = entry.Key;
163 if (field.IsRepeated) {
Jon Skeet25a28582009-02-18 16:06:22 +0000164 IEnumerable valueList = (IEnumerable) entry.Value;
165 if (field.IsPacked) {
166 int dataSize = 0;
167 foreach (object element in valueList) {
168 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
169 }
170 size += dataSize;
171 size += CodedOutputStream.ComputeTagSize(field.FieldNumber);
172 size += CodedOutputStream.ComputeRawVarint32Size((uint)dataSize);
173 } else {
174 foreach (object element in valueList) {
175 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
176 }
Jon Skeet68036862008-10-22 13:30:34 +0100177 }
178 } else {
179 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
180 }
181 }
182
183 UnknownFieldSet unknownFields = UnknownFields;
184 if (DescriptorForType.Options.MessageSetWireFormat) {
185 size += unknownFields.SerializedSizeAsMessageSet;
186 } else {
187 size += unknownFields.SerializedSize;
188 }
189
190 memoizedSize = size;
191 return size;
192 }
193 }
194
195 public ByteString ToByteString() {
196 ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
197 WriteTo(output.CodedOutput);
198 return output.Build();
199 }
200
201 public byte[] ToByteArray() {
202 byte[] result = new byte[SerializedSize];
203 CodedOutputStream output = CodedOutputStream.CreateInstance(result);
204 WriteTo(output);
205 output.CheckNoSpaceLeft();
206 return result;
207 }
208
209 public void WriteTo(Stream output) {
210 CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
211 WriteTo(codedOutput);
212 codedOutput.Flush();
213 }
214
Jon Skeet2e6dc122009-05-29 06:34:52 +0100215 public void WriteDelimitedTo(Stream output) {
216 CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
217 codedOutput.WriteRawVarint32((uint) SerializedSize);
218 WriteTo(codedOutput);
219 codedOutput.Flush();
220 }
221
Jon Skeet68036862008-10-22 13:30:34 +0100222 public override bool Equals(object other) {
223 if (other == this) {
224 return true;
225 }
226 IMessage otherMessage = other as IMessage;
227 if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
228 return false;
229 }
Jon Skeet43da7ae2009-05-28 21:45:43 +0100230 return Dictionaries.Equals(AllFields, otherMessage.AllFields) && UnknownFields.Equals(otherMessage.UnknownFields);
Jon Skeet68036862008-10-22 13:30:34 +0100231 }
232
233 public override int GetHashCode() {
234 int hash = 41;
235 hash = (19 * hash) + DescriptorForType.GetHashCode();
236 hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
Jon Skeet43da7ae2009-05-28 21:45:43 +0100237 hash = (29 * hash) + UnknownFields.GetHashCode();
Jon Skeet68036862008-10-22 13:30:34 +0100238 return hash;
239 }
csharptestd9c59e62010-11-04 19:36:28 -0500240
241 IBuilderLite IMessageLite.WeakCreateBuilderForType() {
242 return WeakCreateBuilderForType(); }
243
244 IBuilderLite IMessageLite.WeakToBuilder() {
245 return WeakToBuilder();
246 }
Jon Skeet68036862008-10-22 13:30:34 +0100247 }
248}