blob: 7b4ae209802d2deed429423d7f7fba7fbb070532 [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
69 // Use a SortedList so we'll end up serializing fields in order
70 var ret = new SortedList<FieldDescriptor, object>();
71 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 {
87 /* if (!DescriptorForType.HasRequiredFields) {
88 return true;
89 }*/
90 // Check that all required fields are present.
91 foreach (FieldDescriptor field in DescriptorForType.Fields) {
92 if (field.IsRequired && !HasField(field)) {
93 return false;
94 }
95 }
96
97 // Check that embedded messages are initialized.
98 // This code is similar to that in AbstractMessage, but we don't
99 // fetch all the field values - just the ones we need to.
100 foreach (FieldDescriptor field in DescriptorForType.Fields) {
101 if (field.MappedType == MappedType.Message) {
102 if (field.IsRepeated) {
103 // We know it's an IList<T>, but not the exact type - so
104 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
105 foreach (IMessage element in (IEnumerable) this[field]) {
106 if (!element.IsInitialized) {
107 return false;
108 }
109 }
110 } else {
111 if (HasField(field) && !((IMessage) this[field]).IsInitialized) {
112 return false;
113 }
114 }
115 }
116 }
117 return true;
118 }
119 }
120
121 public override IDictionary<FieldDescriptor, object> AllFields {
122 get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
123 }
124
125 public override bool HasField(FieldDescriptor field) {
126 return InternalFieldAccessors[field].Has(ThisMessage);
127 }
128
129 public override int GetRepeatedFieldCount(FieldDescriptor field) {
130 return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
131 }
132
133 public override object this[FieldDescriptor field, int index] {
134 get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
135 }
136
137 public override object this[FieldDescriptor field] {
138 get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
139 }
140
141 public override UnknownFieldSet UnknownFields {
142 get { return unknownFields; }
143 }
144
145 /// <summary>
146 /// Replaces the set of unknown fields for this message. This should
147 /// only be used before a message is built, by the builder. (In the
148 /// Java code it is private, but the builder is nested so has access
149 /// to it.)
150 /// </summary>
151 internal void SetUnknownFields(UnknownFieldSet fieldSet) {
152 unknownFields = fieldSet;
153 }
154 }
155}