blob: d60f2b9c434c3e83b60528388ec4b5171e8ce9b5 [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeet60c059b2008-10-23 21:17:56 +01002// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
Jon Skeet68036862008-10-22 13:30:34 +01006// http://code.google.com/p/protobuf/
7//
Jon Skeet60c059b2008-10-23 21:17:56 +01008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
Jon Skeet68036862008-10-22 13:30:34 +010011//
Jon Skeet60c059b2008-10-23 21:17:56 +010012// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
Jon Skeet68036862008-10-22 13:30:34 +010021//
Jon Skeet60c059b2008-10-23 21:17:56 +010022// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet68036862008-10-22 13:30:34 +010035using System;
36using System.Collections.Generic;
37using Google.ProtocolBuffers.Collections;
38using Google.ProtocolBuffers.Descriptors;
39using Google.ProtocolBuffers.FieldAccess;
40using System.Collections;
41
42namespace Google.ProtocolBuffers {
43
44 /// <summary>
45 /// All generated protocol message classes extend this class. It implements
46 /// most of the IMessage interface using reflection. Users
47 /// can ignore this class as an implementation detail.
48 /// </summary>
49 public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
50 where TMessage : GeneratedMessage<TMessage, TBuilder>
51 where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
52
53 private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
54
55 /// <summary>
56 /// Returns the message as a TMessage.
57 /// </summary>
58 protected abstract TMessage ThisMessage { get; }
59
60 internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder {
61 get { return InternalFieldAccessors; }
62 }
63
64 protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
65
66 public override MessageDescriptor DescriptorForType {
67 get { return InternalFieldAccessors.Descriptor; }
68 }
69
70 internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap() {
71
Jon Skeetb49d3c72009-11-03 16:51:01 +000072 // Use a SortedList so we'll end up serializing fields in order
73 var ret = new SortedList<FieldDescriptor, object>();
Jon Skeet68036862008-10-22 13:30:34 +010074 MessageDescriptor descriptor = DescriptorForType;
75 foreach (FieldDescriptor field in descriptor.Fields) {
76 IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
77 if (field.IsRepeated) {
78 if (accessor.GetRepeatedCount(ThisMessage) != 0) {
79 ret[field] = accessor.GetValue(ThisMessage);
80 }
81 } else if (HasField(field)) {
82 ret[field] = accessor.GetValue(ThisMessage);
83 }
84 }
85 return ret;
86 }
87
88 public override bool IsInitialized {
89 get {
Jon Skeet68036862008-10-22 13:30:34 +010090 foreach (FieldDescriptor field in DescriptorForType.Fields) {
Jon Skeet828510c2008-11-24 11:17:17 +000091 // Check that all required fields are present.
Jon Skeet68036862008-10-22 13:30:34 +010092 if (field.IsRequired && !HasField(field)) {
93 return false;
94 }
Jon Skeet828510c2008-11-24 11:17:17 +000095 // Check that embedded messages are initialized.
96 // This code is similar to that in AbstractMessage, but we don't
97 // fetch all the field values - just the ones we need to.
Jon Skeet68036862008-10-22 13:30:34 +010098 if (field.MappedType == MappedType.Message) {
99 if (field.IsRepeated) {
100 // We know it's an IList<T>, but not the exact type - so
101 // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
102 foreach (IMessage element in (IEnumerable) this[field]) {
103 if (!element.IsInitialized) {
104 return false;
105 }
106 }
107 } else {
108 if (HasField(field) && !((IMessage) this[field]).IsInitialized) {
109 return false;
110 }
111 }
112 }
113 }
114 return true;
115 }
116 }
117
118 public override IDictionary<FieldDescriptor, object> AllFields {
119 get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
120 }
121
122 public override bool HasField(FieldDescriptor field) {
123 return InternalFieldAccessors[field].Has(ThisMessage);
124 }
125
126 public override int GetRepeatedFieldCount(FieldDescriptor field) {
127 return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
128 }
129
130 public override object this[FieldDescriptor field, int index] {
131 get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
132 }
133
134 public override object this[FieldDescriptor field] {
135 get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
136 }
137
138 public override UnknownFieldSet UnknownFields {
139 get { return unknownFields; }
140 }
141
142 /// <summary>
143 /// Replaces the set of unknown fields for this message. This should
144 /// only be used before a message is built, by the builder. (In the
145 /// Java code it is private, but the builder is nested so has access
146 /// to it.)
147 /// </summary>
148 internal void SetUnknownFields(UnknownFieldSet fieldSet) {
149 unknownFields = fieldSet;
150 }
151 }
152}