Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/GeneratedBuilder.cs @ 17399

Last change on this file since 17399 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 8.4 KB
Line 
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
37using System;
38using System.Collections;
39using System.Collections.Generic;
40using Google.ProtocolBuffers.Descriptors;
41using Google.ProtocolBuffers.FieldAccess;
42
43namespace Google.ProtocolBuffers
44{
45    /// <summary>
46    /// All generated protocol message builder classes extend this class. It implements
47    /// most of the IBuilder interface using reflection. Users can ignore this class
48    /// as an implementation detail.
49    /// </summary>
50    public abstract partial class GeneratedBuilder<TMessage, TBuilder> : AbstractBuilder<TMessage, TBuilder>
51        where TMessage : GeneratedMessage<TMessage, TBuilder>
52        where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
53    {
54        /// <summary>
55        /// Returns the message being built at the moment.
56        /// </summary>
57        protected abstract TMessage MessageBeingBuilt { get; }
58
59        protected internal FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors
60        {
61            get { return DefaultInstanceForType.FieldAccessorsFromBuilder; }
62        }
63
64        public override IDictionary<FieldDescriptor, object> AllFields
65        {
66            get { return MessageBeingBuilt.AllFields; }
67        }
68
69        public override object this[FieldDescriptor field]
70        {
71            get
72            {
73                // For repeated fields, the underlying list object is still modifiable at this point.
74                // Make sure not to expose the modifiable list to the caller.
75                return field.IsRepeated
76                           ? InternalFieldAccessors[field].GetRepeatedWrapper(ThisBuilder)
77                           : MessageBeingBuilt[field];
78            }
79            set { InternalFieldAccessors[field].SetValue(ThisBuilder, value); }
80        }
81
82        /// <summary>
83        /// Called by derived classes to parse an unknown field.
84        /// </summary>
85        /// <returns>true unless the tag is an end-group tag</returns>
86        [CLSCompliant(false)]
87        protected virtual bool ParseUnknownField(ICodedInputStream input, UnknownFieldSet.Builder unknownFields,
88                                                 ExtensionRegistry extensionRegistry, uint tag, string fieldName)
89        {
90            return unknownFields.MergeFieldFrom(tag, input);
91        }
92
93        public override MessageDescriptor DescriptorForType
94        {
95            get { return DefaultInstanceForType.DescriptorForType; }
96        }
97
98        public override int GetRepeatedFieldCount(FieldDescriptor field)
99        {
100            return MessageBeingBuilt.GetRepeatedFieldCount(field);
101        }
102
103        public override object this[FieldDescriptor field, int index]
104        {
105            get { return MessageBeingBuilt[field, index]; }
106            set { InternalFieldAccessors[field].SetRepeated(ThisBuilder, index, value); }
107        }
108
109        public override bool HasField(FieldDescriptor field)
110        {
111            return MessageBeingBuilt.HasField(field);
112        }
113
114        public override IBuilder CreateBuilderForField(FieldDescriptor field)
115        {
116            return InternalFieldAccessors[field].CreateBuilder();
117        }
118
119        public override TBuilder ClearField(FieldDescriptor field)
120        {
121            InternalFieldAccessors[field].Clear(ThisBuilder);
122            return ThisBuilder;
123        }
124
125        public override TBuilder MergeFrom(TMessage other)
126        {
127            if (other.DescriptorForType != InternalFieldAccessors.Descriptor)
128            {
129                throw new ArgumentException("Message type mismatch");
130            }
131
132            foreach (KeyValuePair<FieldDescriptor, object> entry in other.AllFields)
133            {
134                FieldDescriptor field = entry.Key;
135                if (field.IsRepeated)
136                {
137                    // Concatenate repeated fields
138                    foreach (object element in (IEnumerable) entry.Value)
139                    {
140                        AddRepeatedField(field, element);
141                    }
142                }
143                else if (field.MappedType == MappedType.Message && HasField(field))
144                {
145                    // Merge singular embedded messages
146                    IMessageLite oldValue = (IMessageLite) this[field];
147                    this[field] = oldValue.WeakCreateBuilderForType()
148                        .WeakMergeFrom(oldValue)
149                        .WeakMergeFrom((IMessageLite) entry.Value)
150                        .WeakBuildPartial();
151                }
152                else
153                {
154                    // Just overwrite
155                    this[field] = entry.Value;
156                }
157            }
158
159            //Fix for unknown fields not merging, see java's AbstractMessage.Builder<T> line 236
160            MergeUnknownFields(other.UnknownFields);
161
162            return ThisBuilder;
163        }
164
165        public override TBuilder MergeUnknownFields(UnknownFieldSet unknownFields)
166        {
167            if (unknownFields != UnknownFieldSet.DefaultInstance)
168            {
169                TMessage result = MessageBeingBuilt;
170                result.SetUnknownFields(UnknownFieldSet.CreateBuilder(result.UnknownFields)
171                                            .MergeFrom(unknownFields)
172                                            .Build());
173            }
174            return ThisBuilder;
175        }
176
177        public override TBuilder AddRepeatedField(FieldDescriptor field, object value)
178        {
179            InternalFieldAccessors[field].AddRepeated(ThisBuilder, value);
180            return ThisBuilder;
181        }
182
183        /// <summary>
184        /// Like Build(), but will wrap UninitializedMessageException in
185        /// InvalidProtocolBufferException.
186        /// </summary>
187        public TMessage BuildParsed()
188        {
189            if (!IsInitialized)
190            {
191                throw new UninitializedMessageException(MessageBeingBuilt).AsInvalidProtocolBufferException();
192            }
193            return BuildPartial();
194        }
195
196        /// <summary>
197        /// Implementation of <see cref="IBuilder{TMessage, TBuilder}.Build" />.
198        /// </summary>
199        public override TMessage Build()
200        {
201            // If the message is null, we'll throw a more appropriate exception in BuildPartial.
202            if (!IsInitialized)
203            {
204                throw new UninitializedMessageException(MessageBeingBuilt);
205            }
206            return BuildPartial();
207        }
208
209        public override UnknownFieldSet UnknownFields
210        {
211            get { return MessageBeingBuilt.UnknownFields; }
212            set { MessageBeingBuilt.SetUnknownFields(value); }
213        }
214    }
215}
Note: See TracBrowser for help on using the repository browser.