1 | #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 |
|
---|
37 | using System;
|
---|
38 | using System.Collections;
|
---|
39 | using System.Collections.Generic;
|
---|
40 | using Google.ProtocolBuffers.Descriptors;
|
---|
41 |
|
---|
42 | namespace Google.ProtocolBuffers
|
---|
43 | {
|
---|
44 | /// <summary>
|
---|
45 | /// Implementation of the non-generic IMessage interface as far as possible.
|
---|
46 | /// </summary>
|
---|
47 | public abstract partial class AbstractBuilder<TMessage, TBuilder> : AbstractBuilderLite<TMessage, TBuilder>,
|
---|
48 | IBuilder<TMessage, TBuilder>
|
---|
49 | where TMessage : AbstractMessage<TMessage, TBuilder>
|
---|
50 | where TBuilder : AbstractBuilder<TMessage, TBuilder>
|
---|
51 | {
|
---|
52 | #region Unimplemented members of IBuilder
|
---|
53 |
|
---|
54 | public abstract UnknownFieldSet UnknownFields { get; set; }
|
---|
55 | public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
|
---|
56 | public abstract object this[FieldDescriptor field] { get; set; }
|
---|
57 | public abstract MessageDescriptor DescriptorForType { get; }
|
---|
58 | public abstract int GetRepeatedFieldCount(FieldDescriptor field);
|
---|
59 | public abstract object this[FieldDescriptor field, int index] { get; set; }
|
---|
60 | public abstract bool HasField(FieldDescriptor field);
|
---|
61 | public abstract IBuilder CreateBuilderForField(FieldDescriptor field);
|
---|
62 | public abstract TBuilder ClearField(FieldDescriptor field);
|
---|
63 | public abstract TBuilder AddRepeatedField(FieldDescriptor field, object value);
|
---|
64 |
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | public TBuilder SetUnknownFields(UnknownFieldSet fields)
|
---|
68 | {
|
---|
69 | UnknownFields = fields;
|
---|
70 | return ThisBuilder;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override TBuilder Clear()
|
---|
74 | {
|
---|
75 | foreach (FieldDescriptor field in AllFields.Keys)
|
---|
76 | {
|
---|
77 | ClearField(field);
|
---|
78 | }
|
---|
79 | return ThisBuilder;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override sealed TBuilder MergeFrom(IMessageLite other)
|
---|
83 | {
|
---|
84 | if (other is IMessage)
|
---|
85 | {
|
---|
86 | return MergeFrom((IMessage) other);
|
---|
87 | }
|
---|
88 | throw new ArgumentException("MergeFrom(Message) can only merge messages of the same type.");
|
---|
89 | }
|
---|
90 |
|
---|
91 | /// <summary>
|
---|
92 | /// Merge the specified other message into the message being
|
---|
93 | /// built. Merging occurs as follows. For each field:
|
---|
94 | /// For singular primitive fields, if the field is set in <paramref name="other"/>,
|
---|
95 | /// then <paramref name="other"/>'s value overwrites the value in this message.
|
---|
96 | /// For singular message fields, if the field is set in <paramref name="other"/>,
|
---|
97 | /// it is merged into the corresponding sub-message of this message using the same
|
---|
98 | /// merging rules.
|
---|
99 | /// For repeated fields, the elements in <paramref name="other"/> are concatenated
|
---|
100 | /// with the elements in this message.
|
---|
101 | /// </summary>
|
---|
102 | /// <param name="other"></param>
|
---|
103 | /// <returns></returns>
|
---|
104 | public abstract TBuilder MergeFrom(TMessage other);
|
---|
105 |
|
---|
106 | public virtual TBuilder MergeFrom(IMessage other)
|
---|
107 | {
|
---|
108 | if (other.DescriptorForType != DescriptorForType)
|
---|
109 | {
|
---|
110 | throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
|
---|
111 | }
|
---|
112 |
|
---|
113 | // Note: We don't attempt to verify that other's fields have valid
|
---|
114 | // types. Doing so would be a losing battle. We'd have to verify
|
---|
115 | // all sub-messages as well, and we'd have to make copies of all of
|
---|
116 | // them to insure that they don't change after verification (since
|
---|
117 | // the Message interface itself cannot enforce immutability of
|
---|
118 | // implementations).
|
---|
119 | // TODO(jonskeet): Provide a function somewhere called MakeDeepCopy()
|
---|
120 | // which allows people to make secure deep copies of messages.
|
---|
121 | foreach (KeyValuePair<FieldDescriptor, object> entry in other.AllFields)
|
---|
122 | {
|
---|
123 | FieldDescriptor field = entry.Key;
|
---|
124 | if (field.IsRepeated)
|
---|
125 | {
|
---|
126 | // Concatenate repeated fields
|
---|
127 | foreach (object element in (IEnumerable) entry.Value)
|
---|
128 | {
|
---|
129 | AddRepeatedField(field, element);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | else if (field.MappedType == MappedType.Message)
|
---|
133 | {
|
---|
134 | // Merge singular messages
|
---|
135 | IMessageLite existingValue = (IMessageLite) this[field];
|
---|
136 | if (existingValue == existingValue.WeakDefaultInstanceForType)
|
---|
137 | {
|
---|
138 | this[field] = entry.Value;
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | this[field] = existingValue.WeakCreateBuilderForType()
|
---|
143 | .WeakMergeFrom(existingValue)
|
---|
144 | .WeakMergeFrom((IMessageLite) entry.Value)
|
---|
145 | .WeakBuild();
|
---|
146 | }
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | // Overwrite simple values
|
---|
151 | this[field] = entry.Value;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | //Fix for unknown fields not merging, see java's AbstractMessage.Builder<T> line 236
|
---|
156 | MergeUnknownFields(other.UnknownFields);
|
---|
157 |
|
---|
158 | return ThisBuilder;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public override TBuilder MergeFrom(ICodedInputStream input, ExtensionRegistry extensionRegistry)
|
---|
162 | {
|
---|
163 | UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder(UnknownFields);
|
---|
164 | unknownFields.MergeFrom(input, extensionRegistry, this);
|
---|
165 | UnknownFields = unknownFields.Build();
|
---|
166 | return ThisBuilder;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public virtual TBuilder MergeUnknownFields(UnknownFieldSet unknownFields)
|
---|
170 | {
|
---|
171 | UnknownFields = UnknownFieldSet.CreateBuilder(UnknownFields)
|
---|
172 | .MergeFrom(unknownFields)
|
---|
173 | .Build();
|
---|
174 | return ThisBuilder;
|
---|
175 | }
|
---|
176 |
|
---|
177 | public virtual IBuilder SetField(FieldDescriptor field, object value)
|
---|
178 | {
|
---|
179 | this[field] = value;
|
---|
180 | return ThisBuilder;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public virtual IBuilder SetRepeatedField(FieldDescriptor field, int index, object value)
|
---|
184 | {
|
---|
185 | this[field, index] = value;
|
---|
186 | return ThisBuilder;
|
---|
187 | }
|
---|
188 |
|
---|
189 | #region Explicit Implementations
|
---|
190 |
|
---|
191 | IMessage IBuilder.WeakBuild()
|
---|
192 | {
|
---|
193 | return Build();
|
---|
194 | }
|
---|
195 |
|
---|
196 | IBuilder IBuilder.WeakAddRepeatedField(FieldDescriptor field, object value)
|
---|
197 | {
|
---|
198 | return AddRepeatedField(field, value);
|
---|
199 | }
|
---|
200 |
|
---|
201 | IBuilder IBuilder.WeakClear()
|
---|
202 | {
|
---|
203 | return Clear();
|
---|
204 | }
|
---|
205 |
|
---|
206 | IBuilder IBuilder.WeakMergeFrom(IMessage message)
|
---|
207 | {
|
---|
208 | return MergeFrom(message);
|
---|
209 | }
|
---|
210 |
|
---|
211 | IBuilder IBuilder.WeakMergeFrom(ICodedInputStream input)
|
---|
212 | {
|
---|
213 | return MergeFrom(input);
|
---|
214 | }
|
---|
215 |
|
---|
216 | IBuilder IBuilder.WeakMergeFrom(ICodedInputStream input, ExtensionRegistry registry)
|
---|
217 | {
|
---|
218 | return MergeFrom(input, registry);
|
---|
219 | }
|
---|
220 |
|
---|
221 | IBuilder IBuilder.WeakMergeFrom(ByteString data)
|
---|
222 | {
|
---|
223 | return MergeFrom(data);
|
---|
224 | }
|
---|
225 |
|
---|
226 | IBuilder IBuilder.WeakMergeFrom(ByteString data, ExtensionRegistry registry)
|
---|
227 | {
|
---|
228 | return MergeFrom(data, registry);
|
---|
229 | }
|
---|
230 |
|
---|
231 | IMessage IBuilder.WeakBuildPartial()
|
---|
232 | {
|
---|
233 | return BuildPartial();
|
---|
234 | }
|
---|
235 |
|
---|
236 | IBuilder IBuilder.WeakClone()
|
---|
237 | {
|
---|
238 | return Clone();
|
---|
239 | }
|
---|
240 |
|
---|
241 | IMessage IBuilder.WeakDefaultInstanceForType
|
---|
242 | {
|
---|
243 | get { return DefaultInstanceForType; }
|
---|
244 | }
|
---|
245 |
|
---|
246 | IBuilder IBuilder.WeakClearField(FieldDescriptor field)
|
---|
247 | {
|
---|
248 | return ClearField(field);
|
---|
249 | }
|
---|
250 |
|
---|
251 | #endregion
|
---|
252 | }
|
---|
253 | } |
---|