EnumDescriptor.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System.Collections.Generic;
  17. using Google.ProtocolBuffers.DescriptorProtos;
  18. namespace Google.ProtocolBuffers.Descriptors {
  19. /// <summary>
  20. /// Descriptor for an enum type in a .proto file.
  21. /// </summary>
  22. public sealed class EnumDescriptor : IndexedDescriptorBase<EnumDescriptorProto, EnumOptions> {
  23. private readonly MessageDescriptor containingType;
  24. private readonly IList<EnumValueDescriptor> values;
  25. internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index)
  26. : base(proto, file, ComputeFullName(file, parent, proto.Name), index) {
  27. containingType = parent;
  28. if (proto.ValueCount == 0) {
  29. // We cannot allow enums with no values because this would mean there
  30. // would be no valid default value for fields of this type.
  31. throw new DescriptorValidationException(this, "Enums must contain at least one value.");
  32. }
  33. values = DescriptorUtil.ConvertAndMakeReadOnly(proto.ValueList,
  34. (value, i) => new EnumValueDescriptor(value, file, this, i));
  35. File.DescriptorPool.AddSymbol(this);
  36. }
  37. /// <value>
  38. /// If this is a nested type, get the outer descriptor, otherwise null.
  39. /// </value>
  40. public MessageDescriptor ContainingType {
  41. get { return containingType; }
  42. }
  43. /// <value>
  44. /// An unmodifiable list of defined value descriptors for this enum.
  45. /// </value>
  46. public IList<EnumValueDescriptor> Values {
  47. get { return values; }
  48. }
  49. /// <summary>
  50. /// Finds an enum value by number. If multiple enum values have the
  51. /// same number, this returns the first defined value with that number.
  52. /// </summary>
  53. internal EnumValueDescriptor FindValueByNumber(int number) {
  54. return File.DescriptorPool.FindEnumValueByNumber(this, number);
  55. }
  56. /// <summary>
  57. /// Finds an enum value by name.
  58. /// </summary>
  59. /// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
  60. /// <returns>The value's descriptor, or null if not found.</returns>
  61. internal EnumValueDescriptor FindValueByName(string name) {
  62. return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
  63. }
  64. }
  65. }