blob: 8147a88ba1d8428e756d453266b2df0fe9950e4c [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc. All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15// * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19// * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System;
csharptest74c5e0c2011-07-14 13:06:22 -050038using System.Collections;
csharptest71f662c2011-05-20 15:15:34 -050039using System.Collections.Generic;
csharptest74c5e0c2011-07-14 13:06:22 -050040using System.IO;
41using System.Xml;
csharptest71f662c2011-05-20 15:15:34 -050042using Google.ProtocolBuffers.Collections;
43using Google.ProtocolBuffers.Descriptors;
44using Google.ProtocolBuffers.FieldAccess;
csharptest74c5e0c2011-07-14 13:06:22 -050045using Google.ProtocolBuffers.Serialization;
csharptest71f662c2011-05-20 15:15:34 -050046
47namespace Google.ProtocolBuffers
48{
49 /// <summary>
50 /// All generated protocol message classes extend this class. It implements
51 /// most of the IMessage interface using reflection. Users
52 /// can ignore this class as an implementation detail.
53 /// </summary>
54 public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
55 where TMessage : GeneratedMessage<TMessage, TBuilder>
csharptestf2925232011-06-11 10:41:57 -050056 where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
csharptest71f662c2011-05-20 15:15:34 -050057 {
58 private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
59
60 /// <summary>
61 /// Returns the message as a TMessage.
62 /// </summary>
63 protected abstract TMessage ThisMessage { get; }
64
65 internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder
66 {
67 get { return InternalFieldAccessors; }
68 }
69
70 protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
71
72 public override MessageDescriptor DescriptorForType
73 {
74 get { return InternalFieldAccessors.Descriptor; }
75 }
76
77 internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap()
78 {
79 // Use a SortedList so we'll end up serializing fields in order
80 var ret = new SortedList<FieldDescriptor, object>();
81 MessageDescriptor descriptor = DescriptorForType;
82 foreach (FieldDescriptor field in descriptor.Fields)
83 {
84 IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
85 if (field.IsRepeated)
86 {
87 if (accessor.GetRepeatedCount(ThisMessage) != 0)
88 {
89 ret[field] = accessor.GetValue(ThisMessage);
90 }
91 }
92 else if (HasField(field))
93 {
94 ret[field] = accessor.GetValue(ThisMessage);
95 }
96 }
97 return ret;
98 }
99
100 public override bool IsInitialized
101 {
102 get
103 {
104 foreach (FieldDescriptor field in DescriptorForType.Fields)
105 {
106 // Check that all required fields are present.
107 if (field.IsRequired && !HasField(field))
108 {
109 return false;
110 }
111 // Check that embedded messages are initialized.
112 // This code is similar to that in AbstractMessage, but we don't
113 // fetch all the field values - just the ones we need to.
114 if (field.MappedType == MappedType.Message)
115 {
116 if (field.IsRepeated)
117 {
118 // We know it's an IList<T>, but not the exact type - so
119 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
120 foreach (IMessageLite element in (IEnumerable) this[field])
121 {
122 if (!element.IsInitialized)
123 {
124 return false;
125 }
126 }
127 }
128 else
129 {
130 if (HasField(field) && !((IMessageLite) this[field]).IsInitialized)
131 {
132 return false;
133 }
134 }
135 }
136 }
137 return true;
138 }
139 }
140
141 public override IDictionary<FieldDescriptor, object> AllFields
142 {
143 get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
144 }
145
146 public override bool HasField(FieldDescriptor field)
147 {
148 return InternalFieldAccessors[field].Has(ThisMessage);
149 }
150
151 public override int GetRepeatedFieldCount(FieldDescriptor field)
152 {
153 return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
154 }
155
156 public override object this[FieldDescriptor field, int index]
157 {
158 get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
159 }
160
161 public override object this[FieldDescriptor field]
162 {
163 get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
164 }
165
166 public override UnknownFieldSet UnknownFields
167 {
168 get { return unknownFields; }
169 }
170
171 /// <summary>
172 /// Replaces the set of unknown fields for this message. This should
173 /// only be used before a message is built, by the builder. (In the
174 /// Java code it is private, but the builder is nested so has access
175 /// to it.)
176 /// </summary>
177 internal void SetUnknownFields(UnknownFieldSet fieldSet)
178 {
179 unknownFields = fieldSet;
180 }
csharptestf2925232011-06-11 10:41:57 -0500181
182 public static TMessage ParseFromJson(string jsonText)
183 {
csharptest74c5e0c2011-07-14 13:06:22 -0500184 return JsonFormatReader.CreateInstance(jsonText)
csharptestf2925232011-06-11 10:41:57 -0500185 .Merge(new TBuilder())
186 .Build();
187 }
188
csharptest74c5e0c2011-07-14 13:06:22 -0500189 public static TMessage ParseFromJson(TextReader reader)
csharptestf2925232011-06-11 10:41:57 -0500190 {
csharptest74c5e0c2011-07-14 13:06:22 -0500191 return ParseFromJson(reader, ExtensionRegistry.Empty);
192 }
193
194 public static TMessage ParseFromJson(TextReader reader, ExtensionRegistry extensionRegistry)
195 {
196 return JsonFormatReader.CreateInstance(reader)
csharptestf2925232011-06-11 10:41:57 -0500197 .Merge(new TBuilder(), extensionRegistry)
198 .Build();
199 }
200
csharptest74c5e0c2011-07-14 13:06:22 -0500201 public static TMessage ParseFromXml(XmlReader reader)
csharptestf2925232011-06-11 10:41:57 -0500202 {
csharptest74c5e0c2011-07-14 13:06:22 -0500203 return ParseFromXml(XmlFormatReader.DefaultRootElementName, reader, ExtensionRegistry.Empty);
204 }
205
206 public static TMessage ParseFromXml(string rootElementName, XmlReader reader)
207 {
208 return ParseFromXml(rootElementName, reader, ExtensionRegistry.Empty);
209 }
210
211 public static TMessage ParseFromXml(string rootElementName, XmlReader reader,
212 ExtensionRegistry extensionRegistry)
213 {
214 return XmlFormatReader.CreateInstance(reader)
csharptestf2925232011-06-11 10:41:57 -0500215 .Merge(rootElementName, new TBuilder(), extensionRegistry)
216 .Build();
217 }
csharptest71f662c2011-05-20 15:15:34 -0500218 }
219}