Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/ExtendableMessageLite.cs

Last change on this file 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.5 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 System.IO;
41using Google.ProtocolBuffers.Collections;
42
43namespace Google.ProtocolBuffers
44{
45    public abstract partial class ExtendableMessageLite<TMessage, TBuilder> : GeneratedMessageLite<TMessage, TBuilder>
46        where TMessage : GeneratedMessageLite<TMessage, TBuilder>
47        where TBuilder : GeneratedBuilderLite<TMessage, TBuilder>
48    {
49        protected ExtendableMessageLite()
50        {
51        }
52
53        private readonly FieldSet extensions = FieldSet.CreateInstance();
54
55        /// <summary>
56        /// Access for the builder.
57        /// </summary>
58        internal FieldSet Extensions
59        {
60            get { return extensions; }
61        }
62
63        public override bool Equals(object obj)
64        {
65            ExtendableMessageLite<TMessage, TBuilder> other = obj as ExtendableMessageLite<TMessage, TBuilder>;
66            return !ReferenceEquals(null, other) &&
67                   Dictionaries.Equals(extensions.AllFields, other.extensions.AllFields);
68        }
69
70        public override int GetHashCode()
71        {
72            return Dictionaries.GetHashCode(extensions.AllFields);
73        }
74
75        /// <summary>
76        /// writes the extensions to the text stream
77        /// </summary>
78        public override void PrintTo(TextWriter writer)
79        {
80            foreach (KeyValuePair<IFieldDescriptorLite, object> entry in extensions.AllFields)
81            {
82                string fn = string.Format("[{0}]", entry.Key.FullName);
83                if (entry.Key.IsRepeated)
84                {
85                    foreach (object o in ((IEnumerable) entry.Value))
86                    {
87                        PrintField(fn, true, o, writer);
88                    }
89                }
90                else
91                {
92                    PrintField(fn, true, entry.Value, writer);
93                }
94            }
95        }
96
97        /// <summary>
98        /// Checks if a singular extension is present.
99        /// </summary>
100        public bool HasExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
101        {
102            VerifyExtensionContainingType(extension);
103            return extensions.HasField(extension.Descriptor);
104        }
105
106        /// <summary>
107        /// Returns the number of elements in a repeated extension.
108        /// </summary>
109        public int GetExtensionCount<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension)
110        {
111            VerifyExtensionContainingType(extension);
112            return extensions.GetRepeatedFieldCount(extension.Descriptor);
113        }
114
115        /// <summary>
116        /// Returns the value of an extension.
117        /// </summary>
118        public TExtension GetExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
119        {
120            VerifyExtensionContainingType(extension);
121            object value = extensions[extension.Descriptor];
122            if (value == null)
123            {
124                return extension.DefaultValue;
125            }
126            else
127            {
128                return (TExtension) extension.FromReflectionType(value);
129            }
130        }
131
132        /// <summary>
133        /// Returns one element of a repeated extension.
134        /// </summary>
135        public TExtension GetExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
136                                                   int index)
137        {
138            VerifyExtensionContainingType(extension);
139            return (TExtension) extension.SingularFromReflectionType(extensions[extension.Descriptor, index]);
140        }
141
142        /// <summary>
143        /// Called to check if all extensions are initialized.
144        /// </summary>
145        protected bool ExtensionsAreInitialized
146        {
147            get { return extensions.IsInitialized; }
148        }
149
150        public override bool IsInitialized
151        {
152            get { return ExtensionsAreInitialized; }
153        }
154
155        /// <summary>
156        /// Used by subclasses to serialize extensions. Extension ranges may be
157        /// interleaves with field numbers, but we must write them in canonical
158        /// (sorted by field number) order. This class helps us to write individual
159        /// ranges of extensions at once.
160        ///
161        /// TODO(jonskeet): See if we can improve this in terms of readability.
162        /// </summary>
163        protected class ExtensionWriter
164        {
165            private readonly IEnumerator<KeyValuePair<IFieldDescriptorLite, object>> iterator;
166            private readonly FieldSet extensions;
167            private KeyValuePair<IFieldDescriptorLite, object>? next = null;
168
169            internal ExtensionWriter(ExtendableMessageLite<TMessage, TBuilder> message)
170            {
171                extensions = message.extensions;
172                iterator = message.extensions.GetEnumerator();
173                if (iterator.MoveNext())
174                {
175                    next = iterator.Current;
176                }
177            }
178
179            public void WriteUntil(int end, ICodedOutputStream output)
180            {
181                while (next != null && next.Value.Key.FieldNumber < end)
182                {
183                    extensions.WriteField(next.Value.Key, next.Value.Value, output);
184                    if (iterator.MoveNext())
185                    {
186                        next = iterator.Current;
187                    }
188                    else
189                    {
190                        next = null;
191                    }
192                }
193            }
194        }
195
196        protected ExtensionWriter CreateExtensionWriter(ExtendableMessageLite<TMessage, TBuilder> message)
197        {
198            return new ExtensionWriter(message);
199        }
200
201        /// <summary>
202        /// Called by subclasses to compute the size of extensions.
203        /// </summary>
204        protected int ExtensionsSerializedSize
205        {
206            get { return extensions.SerializedSize; }
207        }
208
209        internal void VerifyExtensionContainingType<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
210        {
211            if (!ReferenceEquals(extension.ContainingTypeDefaultInstance, DefaultInstanceForType))
212            {
213                // This can only happen if someone uses unchecked operations.
214                throw new ArgumentException(
215                    String.Format("Extension is for type \"{0}\" which does not match message type \"{1}\".",
216                                  extension.ContainingTypeDefaultInstance, DefaultInstanceForType
217                        ));
218            }
219        }
220    }
221}
Note: See TracBrowser for help on using the repository browser.