Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/ExtendableBuilder.cs @ 4032

Last change on this file since 4032 was 3857, checked in by abeham, 15 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 7.2 KB
Line 
1#region Copyright notice and license
2// 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:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * 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.
21//
22// 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.
33#endregion
34
35using System;
36using System.Collections.Generic;
37using Google.ProtocolBuffers.Descriptors;
38
39namespace Google.ProtocolBuffers {
40  public abstract class ExtendableBuilder<TMessage, TBuilder> : GeneratedBuilder<TMessage, TBuilder>
41    where TMessage : ExtendableMessage<TMessage, TBuilder>
42    where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
43
44    protected ExtendableBuilder() {}
45
46    /// <summary>
47    /// Checks if a singular extension is present
48    /// </summary>
49    public bool HasExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
50      return MessageBeingBuilt.HasExtension(extension);
51    }
52
53    /// <summary>
54    /// Returns the number of elements in a repeated extension.
55    /// </summary>
56    public int GetExtensionCount<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension) {
57      return MessageBeingBuilt.GetExtensionCount(extension);
58    }
59
60    /// <summary>
61    /// Returns the value of an extension.
62    /// </summary>
63    public TExtension GetExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
64      return MessageBeingBuilt.GetExtension(extension);
65    }
66
67    /// <summary>
68    /// Returns one element of a repeated extension.
69    /// </summary>
70    public TExtension GetExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, int index) {
71      return MessageBeingBuilt.GetExtension(extension, index);
72    }
73
74    /// <summary>
75    /// Sets the value of an extension.
76    /// </summary>
77    public TBuilder SetExtension<TExtension>(GeneratedExtensionBase<TExtension> extension, TExtension value) {
78      ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
79      message.VerifyExtensionContainingType(extension);
80      message.Extensions[extension.Descriptor] = extension.ToReflectionType(value);
81      return ThisBuilder;
82    }
83
84    /// <summary>
85    /// Sets the value of one element of a repeated extension.
86    /// </summary>
87    public TBuilder SetExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, int index, TExtension value) {
88      ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
89      message.VerifyExtensionContainingType(extension);
90      message.Extensions[extension.Descriptor, index] = extension.SingularToReflectionType(value);
91      return ThisBuilder;
92    }
93
94    /// <summary>
95    /// Appends a value to a repeated extension.
96    /// </summary>
97    public ExtendableBuilder<TMessage, TBuilder> AddExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, TExtension value) {
98      ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
99      message.VerifyExtensionContainingType(extension);
100      message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
101      return this;
102    }
103
104    /// <summary>
105    /// Clears an extension.
106    /// </summary>
107    public ExtendableBuilder<TMessage, TBuilder> ClearExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
108      ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
109      message.VerifyExtensionContainingType(extension);
110      message.Extensions.ClearField(extension.Descriptor);
111      return this;
112    }
113
114    /// <summary>
115    /// Called by subclasses to parse an unknown field or an extension.
116    /// </summary>
117    /// <returns>true unless the tag is an end-group tag</returns>
118    [CLSCompliant(false)]
119    protected override bool ParseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
120        ExtensionRegistry extensionRegistry, uint tag) {
121      return unknownFields.MergeFieldFrom(input, extensionRegistry, this, tag);
122    }
123
124    // ---------------------------------------------------------------
125    // Reflection
126
127
128    public override object this[FieldDescriptor field, int index] {
129      set {
130        if (field.IsExtension) {
131          ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
132          message.VerifyContainingType(field);
133          message.Extensions[field, index] = value;
134        } else {
135          base[field, index] = value;
136        }
137      }
138    }
139
140   
141    public override object this[FieldDescriptor field] {
142      set {
143        if (field.IsExtension) {
144          ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
145          message.VerifyContainingType(field);
146          message.Extensions[field] = value;
147        } else {
148          base[field] = value;
149        }
150      }
151    }
152
153    public override TBuilder ClearField(FieldDescriptor field) {
154      if (field.IsExtension) {
155        ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
156        message.VerifyContainingType(field);
157        message.Extensions.ClearField(field);
158        return ThisBuilder;
159      } else {
160        return base.ClearField(field);
161      }
162    }
163
164    public override TBuilder AddRepeatedField(FieldDescriptor field, object value) {
165      if (field.IsExtension) {
166        ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
167        message.VerifyContainingType(field);
168        message.Extensions.AddRepeatedField(field, value);
169        return ThisBuilder;
170      } else {
171        return base.AddRepeatedField(field, value);
172      }
173    }
174
175    protected void MergeExtensionFields(ExtendableMessage<TMessage, TBuilder> other) {
176      MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.