blob: ce755bed4ec74f53040f487a423ac49995acb401 [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;
csharptest71f662c2011-05-20 15:15:34 -050045
46namespace Google.ProtocolBuffers
47{
48 /// <summary>
49 /// All generated protocol message classes extend this class. It implements
50 /// most of the IMessage interface using reflection. Users
51 /// can ignore this class as an implementation detail.
52 /// </summary>
53 public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
54 where TMessage : GeneratedMessage<TMessage, TBuilder>
csharptestf2925232011-06-11 10:41:57 -050055 where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
csharptest71f662c2011-05-20 15:15:34 -050056 {
57 private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
58
59 /// <summary>
60 /// Returns the message as a TMessage.
61 /// </summary>
62 protected abstract TMessage ThisMessage { get; }
63
64 internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder
65 {
66 get { return InternalFieldAccessors; }
67 }
68
69 protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
70
71 public override MessageDescriptor DescriptorForType
72 {
73 get { return InternalFieldAccessors.Descriptor; }
74 }
75
76 internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap()
77 {
78 // Use a SortedList so we'll end up serializing fields in order
79 var ret = new SortedList<FieldDescriptor, object>();
80 MessageDescriptor descriptor = DescriptorForType;
81 foreach (FieldDescriptor field in descriptor.Fields)
82 {
83 IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
84 if (field.IsRepeated)
85 {
86 if (accessor.GetRepeatedCount(ThisMessage) != 0)
87 {
88 ret[field] = accessor.GetValue(ThisMessage);
89 }
90 }
91 else if (HasField(field))
92 {
93 ret[field] = accessor.GetValue(ThisMessage);
94 }
95 }
96 return ret;
97 }
98
99 public override bool IsInitialized
100 {
101 get
102 {
103 foreach (FieldDescriptor field in DescriptorForType.Fields)
104 {
105 // Check that all required fields are present.
106 if (field.IsRequired && !HasField(field))
107 {
108 return false;
109 }
110 // Check that embedded messages are initialized.
111 // This code is similar to that in AbstractMessage, but we don't
112 // fetch all the field values - just the ones we need to.
113 if (field.MappedType == MappedType.Message)
114 {
115 if (field.IsRepeated)
116 {
117 // We know it's an IList<T>, but not the exact type - so
118 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
119 foreach (IMessageLite element in (IEnumerable) this[field])
120 {
121 if (!element.IsInitialized)
122 {
123 return false;
124 }
125 }
126 }
127 else
128 {
129 if (HasField(field) && !((IMessageLite) this[field]).IsInitialized)
130 {
131 return false;
132 }
133 }
134 }
135 }
136 return true;
137 }
138 }
139
140 public override IDictionary<FieldDescriptor, object> AllFields
141 {
142 get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
143 }
144
145 public override bool HasField(FieldDescriptor field)
146 {
147 return InternalFieldAccessors[field].Has(ThisMessage);
148 }
149
150 public override int GetRepeatedFieldCount(FieldDescriptor field)
151 {
152 return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
153 }
154
155 public override object this[FieldDescriptor field, int index]
156 {
157 get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
158 }
159
160 public override object this[FieldDescriptor field]
161 {
162 get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
163 }
164
165 public override UnknownFieldSet UnknownFields
166 {
167 get { return unknownFields; }
168 }
169
170 /// <summary>
171 /// Replaces the set of unknown fields for this message. This should
172 /// only be used before a message is built, by the builder. (In the
173 /// Java code it is private, but the builder is nested so has access
174 /// to it.)
175 /// </summary>
176 internal void SetUnknownFields(UnknownFieldSet fieldSet)
177 {
178 unknownFields = fieldSet;
179 }
180 }
181}