blob: 11bb948e6247342946d2f0911cf7000f5309dfdc [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.Collections;
33using System.Collections.Generic;
34using System.IO;
35using Google.ProtocolBuffers.Collections;
36using Google.ProtocolBuffers.Descriptors;
37
38namespace Google.ProtocolBuffers {
39 /// <summary>
40 /// Implementation of the non-generic IMessage interface as far as possible.
41 /// </summary>
42 public abstract class AbstractMessage<TMessage, TBuilder> : IMessage<TMessage, TBuilder>
43 where TMessage : AbstractMessage<TMessage, TBuilder>
44 where TBuilder : AbstractBuilder<TMessage, TBuilder> {
45 /// <summary>
46 /// The serialized size if it's already been computed, or null
47 /// if we haven't computed it yet.
48 /// </summary>
49 private int? memoizedSize = null;
50
51 #region Unimplemented members of IMessage
52 public abstract MessageDescriptor DescriptorForType { get; }
53 public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
54 public abstract bool HasField(FieldDescriptor field);
55 public abstract object this[FieldDescriptor field] { get; }
56 public abstract int GetRepeatedFieldCount(FieldDescriptor field);
57 public abstract object this[FieldDescriptor field, int index] { get; }
58 public abstract UnknownFieldSet UnknownFields { get; }
59 public abstract TMessage DefaultInstanceForType { get; }
60 public abstract TBuilder CreateBuilderForType();
Jon Skeete81a9d72009-02-24 16:50:56 +000061 public abstract TBuilder ToBuilder();
Jon Skeet68036862008-10-22 13:30:34 +010062 #endregion
63
64 public IBuilder WeakCreateBuilderForType() {
65 return CreateBuilderForType();
66 }
67
Jon Skeete81a9d72009-02-24 16:50:56 +000068 public IBuilder WeakToBuilder() {
69 return ToBuilder();
70 }
71
Jon Skeet68036862008-10-22 13:30:34 +010072 public IMessage WeakDefaultInstanceForType {
73 get { return DefaultInstanceForType; }
74 }
75
76 public virtual bool IsInitialized {
77 get {
78 // Check that all required fields are present.
79 foreach (FieldDescriptor field in DescriptorForType.Fields) {
80 if (field.IsRequired && !HasField(field)) {
81 return false;
82 }
83 }
84
85 // Check that embedded messages are initialized.
86 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
87 FieldDescriptor field = entry.Key;
88 if (field.MappedType == MappedType.Message) {
89 if (field.IsRepeated) {
90 // We know it's an IList<T>, but not the exact type - so
91 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
92 foreach (IMessage element in (IEnumerable) entry.Value) {
93 if (!element.IsInitialized) {
94 return false;
95 }
96 }
97 } else {
98 if (!((IMessage)entry.Value).IsInitialized) {
99 return false;
100 }
101 }
102 }
103 }
104 return true;
105 }
106 }
107
108 public sealed override string ToString() {
109 return TextFormat.PrintToString(this);
110 }
111
112 public virtual void WriteTo(CodedOutputStream output) {
113 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
114 FieldDescriptor field = entry.Key;
115 if (field.IsRepeated) {
116 // We know it's an IList<T>, but not the exact type - so
117 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
Jon Skeet25a28582009-02-18 16:06:22 +0000118 IEnumerable valueList = (IEnumerable) entry.Value;
119 if (field.IsPacked) {
120 output.WriteTag(field.FieldNumber, WireFormat.WireType.LengthDelimited);
121 int dataSize = 0;
122 foreach (object element in valueList) {
123 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
124 }
125 output.WriteRawVarint32((uint)dataSize);
126 foreach (object element in valueList) {
127 output.WriteFieldNoTag(field.FieldType, element);
128 }
129 } else {
130 foreach (object element in valueList) {
131 output.WriteField(field.FieldType, field.FieldNumber, element);
132 }
Jon Skeet68036862008-10-22 13:30:34 +0100133 }
134 } else {
135 output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
136 }
137 }
138
139 UnknownFieldSet unknownFields = UnknownFields;
140 if (DescriptorForType.Options.MessageSetWireFormat) {
141 unknownFields.WriteAsMessageSetTo(output);
142 } else {
143 unknownFields.WriteTo(output);
144 }
145 }
146
147 public virtual int SerializedSize {
148 get {
149 if (memoizedSize != null) {
150 return memoizedSize.Value;
151 }
152
153 int size = 0;
154 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
155 FieldDescriptor field = entry.Key;
156 if (field.IsRepeated) {
Jon Skeet25a28582009-02-18 16:06:22 +0000157 IEnumerable valueList = (IEnumerable) entry.Value;
158 if (field.IsPacked) {
159 int dataSize = 0;
160 foreach (object element in valueList) {
161 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
162 }
163 size += dataSize;
164 size += CodedOutputStream.ComputeTagSize(field.FieldNumber);
165 size += CodedOutputStream.ComputeRawVarint32Size((uint)dataSize);
166 } else {
167 foreach (object element in valueList) {
168 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
169 }
Jon Skeet68036862008-10-22 13:30:34 +0100170 }
171 } else {
172 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
173 }
174 }
175
176 UnknownFieldSet unknownFields = UnknownFields;
177 if (DescriptorForType.Options.MessageSetWireFormat) {
178 size += unknownFields.SerializedSizeAsMessageSet;
179 } else {
180 size += unknownFields.SerializedSize;
181 }
182
183 memoizedSize = size;
184 return size;
185 }
186 }
187
188 public ByteString ToByteString() {
189 ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
190 WriteTo(output.CodedOutput);
191 return output.Build();
192 }
193
194 public byte[] ToByteArray() {
195 byte[] result = new byte[SerializedSize];
196 CodedOutputStream output = CodedOutputStream.CreateInstance(result);
197 WriteTo(output);
198 output.CheckNoSpaceLeft();
199 return result;
200 }
201
202 public void WriteTo(Stream output) {
203 CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
204 WriteTo(codedOutput);
205 codedOutput.Flush();
206 }
207
208 public override bool Equals(object other) {
209 if (other == this) {
210 return true;
211 }
212 IMessage otherMessage = other as IMessage;
213 if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
214 return false;
215 }
216 return Dictionaries.Equals(AllFields, otherMessage.AllFields);
217 }
218
219 public override int GetHashCode() {
220 int hash = 41;
221 hash = (19 * hash) + DescriptorForType.GetHashCode();
222 hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
223 return hash;
224 }
225 }
226}