blob: 9787e159d06f0657a953c04d49d4a6f51dcc4364 [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.)
113 foreach (object element in (IEnumerable)entry.Value) {
114 output.WriteField(field.FieldType, field.FieldNumber, element);
115 }
116 } else {
117 output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
118 }
119 }
120
121 UnknownFieldSet unknownFields = UnknownFields;
122 if (DescriptorForType.Options.MessageSetWireFormat) {
123 unknownFields.WriteAsMessageSetTo(output);
124 } else {
125 unknownFields.WriteTo(output);
126 }
127 }
128
129 public virtual int SerializedSize {
130 get {
131 if (memoizedSize != null) {
132 return memoizedSize.Value;
133 }
134
135 int size = 0;
136 foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
137 FieldDescriptor field = entry.Key;
138 if (field.IsRepeated) {
139 foreach (object element in (IEnumerable) entry.Value) {
140 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
141 }
142 } else {
143 size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
144 }
145 }
146
147 UnknownFieldSet unknownFields = UnknownFields;
148 if (DescriptorForType.Options.MessageSetWireFormat) {
149 size += unknownFields.SerializedSizeAsMessageSet;
150 } else {
151 size += unknownFields.SerializedSize;
152 }
153
154 memoizedSize = size;
155 return size;
156 }
157 }
158
159 public ByteString ToByteString() {
160 ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
161 WriteTo(output.CodedOutput);
162 return output.Build();
163 }
164
165 public byte[] ToByteArray() {
166 byte[] result = new byte[SerializedSize];
167 CodedOutputStream output = CodedOutputStream.CreateInstance(result);
168 WriteTo(output);
169 output.CheckNoSpaceLeft();
170 return result;
171 }
172
173 public void WriteTo(Stream output) {
174 CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
175 WriteTo(codedOutput);
176 codedOutput.Flush();
177 }
178
179 public override bool Equals(object other) {
180 if (other == this) {
181 return true;
182 }
183 IMessage otherMessage = other as IMessage;
184 if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
185 return false;
186 }
187 return Dictionaries.Equals(AllFields, otherMessage.AllFields);
188 }
189
190 public override int GetHashCode() {
191 int hash = 41;
192 hash = (19 * hash) + DescriptorForType.GetHashCode();
193 hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
194 return hash;
195 }
196 }
197}