blob: 055bc722c9ef7a07d5326efa2da03139e3fcba6e [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;
33using System.Collections.Generic;
34using Google.ProtocolBuffers.Collections;
35using Google.ProtocolBuffers.Descriptors;
36using Google.ProtocolBuffers.FieldAccess;
37using System.Collections;
38
39namespace Google.ProtocolBuffers {
40
41 /// <summary>
42 /// All generated protocol message classes extend this class. It implements
43 /// most of the IMessage interface using reflection. Users
44 /// can ignore this class as an implementation detail.
45 /// </summary>
46 public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
47 where TMessage : GeneratedMessage<TMessage, TBuilder>
48 where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
49
50 private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
51
52 /// <summary>
53 /// Returns the message as a TMessage.
54 /// </summary>
55 protected abstract TMessage ThisMessage { get; }
56
57 internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder {
58 get { return InternalFieldAccessors; }
59 }
60
61 protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
62
63 public override MessageDescriptor DescriptorForType {
64 get { return InternalFieldAccessors.Descriptor; }
65 }
66
67 internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap() {
68
Jon Skeet60fb63e2009-06-20 20:46:28 +010069 // Use a SortedDictionary so we'll end up serializing fields in order
70 var ret = new SortedDictionary<FieldDescriptor, object>();
Jon Skeet68036862008-10-22 13:30:34 +010071 MessageDescriptor descriptor = DescriptorForType;
72 foreach (FieldDescriptor field in descriptor.Fields) {
73 IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
74 if (field.IsRepeated) {
75 if (accessor.GetRepeatedCount(ThisMessage) != 0) {
76 ret[field] = accessor.GetValue(ThisMessage);
77 }
78 } else if (HasField(field)) {
79 ret[field] = accessor.GetValue(ThisMessage);
80 }
81 }
82 return ret;
83 }
84
85 public override bool IsInitialized {
86 get {
Jon Skeet68036862008-10-22 13:30:34 +010087 foreach (FieldDescriptor field in DescriptorForType.Fields) {
Jon Skeet828510c2008-11-24 11:17:17 +000088 // Check that all required fields are present.
Jon Skeet68036862008-10-22 13:30:34 +010089 if (field.IsRequired && !HasField(field)) {
90 return false;
91 }
Jon Skeet828510c2008-11-24 11:17:17 +000092 // Check that embedded messages are initialized.
93 // This code is similar to that in AbstractMessage, but we don't
94 // fetch all the field values - just the ones we need to.
Jon Skeet68036862008-10-22 13:30:34 +010095 if (field.MappedType == MappedType.Message) {
96 if (field.IsRepeated) {
97 // We know it's an IList<T>, but not the exact type - so
98 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
99 foreach (IMessage element in (IEnumerable) this[field]) {
100 if (!element.IsInitialized) {
101 return false;
102 }
103 }
104 } else {
105 if (HasField(field) && !((IMessage) this[field]).IsInitialized) {
106 return false;
107 }
108 }
109 }
110 }
111 return true;
112 }
113 }
114
115 public override IDictionary<FieldDescriptor, object> AllFields {
116 get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
117 }
118
119 public override bool HasField(FieldDescriptor field) {
120 return InternalFieldAccessors[field].Has(ThisMessage);
121 }
122
123 public override int GetRepeatedFieldCount(FieldDescriptor field) {
124 return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
125 }
126
127 public override object this[FieldDescriptor field, int index] {
128 get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
129 }
130
131 public override object this[FieldDescriptor field] {
132 get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
133 }
134
135 public override UnknownFieldSet UnknownFields {
136 get { return unknownFields; }
137 }
138
139 /// <summary>
140 /// Replaces the set of unknown fields for this message. This should
141 /// only be used before a message is built, by the builder. (In the
142 /// Java code it is private, but the builder is nested so has access
143 /// to it.)
144 /// </summary>
145 internal void SetUnknownFields(UnknownFieldSet fieldSet) {
146 unknownFields = fieldSet;
147 }
148 }
149}