Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs @ 8295

Last change on this file since 8295 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: 5.2 KB
Line 
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://github.com/jskeet/dotnet-protobufs/
4// Original C++/Java/Python code:
5// http://code.google.com/p/protobuf/
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11//     * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//     * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17//     * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived from
19// this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32using System.Collections.Generic;
33using Google.ProtocolBuffers.DescriptorProtos;
34
35namespace Google.ProtocolBuffers.Descriptors
36{
37    /// <summary>
38    /// Descriptor for an enum type in a .proto file.
39    /// </summary>
40    public sealed class EnumDescriptor : IndexedDescriptorBase<EnumDescriptorProto, EnumOptions>,
41                                         IEnumLiteMap<EnumValueDescriptor>
42    {
43        private readonly MessageDescriptor containingType;
44        private readonly IList<EnumValueDescriptor> values;
45
46        internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index)
47            : base(proto, file, ComputeFullName(file, parent, proto.Name), index)
48        {
49            containingType = parent;
50
51            if (proto.ValueCount == 0)
52            {
53                // We cannot allow enums with no values because this would mean there
54                // would be no valid default value for fields of this type.
55                throw new DescriptorValidationException(this, "Enums must contain at least one value.");
56            }
57
58            values = DescriptorUtil.ConvertAndMakeReadOnly(proto.ValueList,
59                                                           (value, i) => new EnumValueDescriptor(value, file, this, i));
60
61            File.DescriptorPool.AddSymbol(this);
62        }
63
64        /// <value>
65        /// If this is a nested type, get the outer descriptor, otherwise null.
66        /// </value>
67        public MessageDescriptor ContainingType
68        {
69            get { return containingType; }
70        }
71
72        /// <value>
73        /// An unmodifiable list of defined value descriptors for this enum.
74        /// </value>
75        public IList<EnumValueDescriptor> Values
76        {
77            get { return values; }
78        }
79
80        /// <summary>
81        /// Logic moved from FieldSet to continue current behavior
82        /// </summary>
83        public bool IsValidValue(IEnumLite value)
84        {
85            return value is EnumValueDescriptor && ((EnumValueDescriptor) value).EnumDescriptor == this;
86        }
87
88        /// <summary>
89        /// Finds an enum value by number. If multiple enum values have the
90        /// same number, this returns the first defined value with that number.
91        /// </summary>
92        public EnumValueDescriptor FindValueByNumber(int number)
93        {
94            return File.DescriptorPool.FindEnumValueByNumber(this, number);
95        }
96
97        IEnumLite IEnumLiteMap.FindValueByNumber(int number)
98        {
99            return FindValueByNumber(number);
100        }
101
102        IEnumLite IEnumLiteMap.FindValueByName(string name)
103        {
104            return FindValueByName(name);
105        }
106
107        /// <summary>
108        /// Finds an enum value by name.
109        /// </summary>
110        /// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
111        /// <returns>The value's descriptor, or null if not found.</returns>
112        public EnumValueDescriptor FindValueByName(string name)
113        {
114            return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
115        }
116
117        internal override void ReplaceProto(EnumDescriptorProto newProto)
118        {
119            base.ReplaceProto(newProto);
120            for (int i = 0; i < values.Count; i++)
121            {
122                values[i].ReplaceProto(newProto.GetValue(i));
123            }
124        }
125    }
126}
Note: See TracBrowser for help on using the repository browser.