blob: 0a69f2947f3972d042e22e7b222b6bdf826aebd4 [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();
61 #endregion
62
63 public IBuilder WeakCreateBuilderForType() {
64 return CreateBuilderForType();
65 }
66
67 public IMessage WeakDefaultInstanceForType {
68 get { return DefaultInstanceForType; }
69 }
70
71 public virtual bool IsInitialized {
72 get {
73 // Check that all required fields are present.
74 foreach (FieldDescriptor field in DescriptorForType.Fields) {
75 if (field.IsRequired && !HasField(field)) {
76 return false;
77 }
78 }
79
80 // Check that embedded messages are initialized.
81 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
82 FieldDescriptor field = entry.Key;
83 if (field.MappedType == MappedType.Message) {
84 if (field.IsRepeated) {
85 // We know it's an IList<T>, but not the exact type - so
86 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
87 foreach (IMessage element in (IEnumerable) entry.Value) {
88 if (!element.IsInitialized) {
89 return false;
90 }
91 }
92 } else {
93 if (!((IMessage)entry.Value).IsInitialized) {
94 return false;
95 }
96 }
97 }
98 }
99 return true;
100 }
101 }
102
103 public sealed override string ToString() {
104 return TextFormat.PrintToString(this);
105 }
106
107 public virtual void WriteTo(CodedOutputStream output) {
108 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
109 FieldDescriptor field = entry.Key;
110 if (field.IsRepeated) {
111 // We know it's an IList<T>, but not the exact type - so
112 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
Jon Skeet25a28582009-02-18 16:06:22 +0000113 IEnumerable valueList = (IEnumerable) entry.Value;
114 if (field.IsPacked) {
115 output.WriteTag(field.FieldNumber, WireFormat.WireType.LengthDelimited);
116 int dataSize = 0;
117 foreach (object element in valueList) {
118 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
119 }
120 output.WriteRawVarint32((uint)dataSize);
121 foreach (object element in valueList) {
122 output.WriteFieldNoTag(field.FieldType, element);
123 }
124 } else {
125 foreach (object element in valueList) {
126 output.WriteField(field.FieldType, field.FieldNumber, element);
127 }
Jon Skeet68036862008-10-22 13:30:34 +0100128 }
129 } else {
130 output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
131 }
132 }
133
134 UnknownFieldSet unknownFields = UnknownFields;
135 if (DescriptorForType.Options.MessageSetWireFormat) {
136 unknownFields.WriteAsMessageSetTo(output);
137 } else {
138 unknownFields.WriteTo(output);
139 }
140 }
141
142 public virtual int SerializedSize {
143 get {
144 if (memoizedSize != null) {
145 return memoizedSize.Value;
146 }
147
148 int size = 0;
149 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
150 FieldDescriptor field = entry.Key;
151 if (field.IsRepeated) {
Jon Skeet25a28582009-02-18 16:06:22 +0000152 IEnumerable valueList = (IEnumerable) entry.Value;
153 if (field.IsPacked) {
154 int dataSize = 0;
155 foreach (object element in valueList) {
156 dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
157 }
158 size += dataSize;
159 size += CodedOutputStream.ComputeTagSize(field.FieldNumber);
160 size += CodedOutputStream.ComputeRawVarint32Size((uint)dataSize);
161 } else {
162 foreach (object element in valueList) {
163 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
164 }
Jon Skeet68036862008-10-22 13:30:34 +0100165 }
166 } else {
167 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
168 }
169 }
170
171 UnknownFieldSet unknownFields = UnknownFields;
172 if (DescriptorForType.Options.MessageSetWireFormat) {
173 size += unknownFields.SerializedSizeAsMessageSet;
174 } else {
175 size += unknownFields.SerializedSize;
176 }
177
178 memoizedSize = size;
179 return size;
180 }
181 }
182
183 public ByteString ToByteString() {
184 ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
185 WriteTo(output.CodedOutput);
186 return output.Build();
187 }
188
189 public byte[] ToByteArray() {
190 byte[] result = new byte[SerializedSize];
191 CodedOutputStream output = CodedOutputStream.CreateInstance(result);
192 WriteTo(output);
193 output.CheckNoSpaceLeft();
194 return result;
195 }
196
197 public void WriteTo(Stream output) {
198 CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
199 WriteTo(codedOutput);
200 codedOutput.Flush();
201 }
202
203 public override bool Equals(object other) {
204 if (other == this) {
205 return true;
206 }
207 IMessage otherMessage = other as IMessage;
208 if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
209 return false;
210 }
211 return Dictionaries.Equals(AllFields, otherMessage.AllFields);
212 }
213
214 public override int GetHashCode() {
215 int hash = 41;
216 hash = (19 * hash) + DescriptorForType.GetHashCode();
217 hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
218 return hash;
219 }
220 }
221}