/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs

https://code.google.com/p/protobuf-csharp-port/ · C# · 126 lines · 59 code · 13 blank · 54 comment · 5 complexity · bf55405a8c7ce0cc2625dfdb6f2e8d0c MD5 · raw file

  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.
  32. using System.Collections.Generic;
  33. using Google.ProtocolBuffers.DescriptorProtos;
  34. namespace Google.ProtocolBuffers.Descriptors
  35. {
  36. /// <summary>
  37. /// Descriptor for an enum type in a .proto file.
  38. /// </summary>
  39. public sealed class EnumDescriptor : IndexedDescriptorBase<EnumDescriptorProto, EnumOptions>,
  40. IEnumLiteMap<EnumValueDescriptor>
  41. {
  42. private readonly MessageDescriptor containingType;
  43. private readonly IList<EnumValueDescriptor> values;
  44. internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index)
  45. : base(proto, file, ComputeFullName(file, parent, proto.Name), index)
  46. {
  47. containingType = parent;
  48. if (proto.ValueCount == 0)
  49. {
  50. // We cannot allow enums with no values because this would mean there
  51. // would be no valid default value for fields of this type.
  52. throw new DescriptorValidationException(this, "Enums must contain at least one value.");
  53. }
  54. values = DescriptorUtil.ConvertAndMakeReadOnly(proto.ValueList,
  55. (value, i) => new EnumValueDescriptor(value, file, this, i));
  56. File.DescriptorPool.AddSymbol(this);
  57. }
  58. /// <value>
  59. /// If this is a nested type, get the outer descriptor, otherwise null.
  60. /// </value>
  61. public MessageDescriptor ContainingType
  62. {
  63. get { return containingType; }
  64. }
  65. /// <value>
  66. /// An unmodifiable list of defined value descriptors for this enum.
  67. /// </value>
  68. public IList<EnumValueDescriptor> Values
  69. {
  70. get { return values; }
  71. }
  72. /// <summary>
  73. /// Logic moved from FieldSet to continue current behavior
  74. /// </summary>
  75. public bool IsValidValue(IEnumLite value)
  76. {
  77. return value is EnumValueDescriptor && ((EnumValueDescriptor) value).EnumDescriptor == this;
  78. }
  79. /// <summary>
  80. /// Finds an enum value by number. If multiple enum values have the
  81. /// same number, this returns the first defined value with that number.
  82. /// </summary>
  83. public EnumValueDescriptor FindValueByNumber(int number)
  84. {
  85. return File.DescriptorPool.FindEnumValueByNumber(this, number);
  86. }
  87. IEnumLite IEnumLiteMap.FindValueByNumber(int number)
  88. {
  89. return FindValueByNumber(number);
  90. }
  91. IEnumLite IEnumLiteMap.FindValueByName(string name)
  92. {
  93. return FindValueByName(name);
  94. }
  95. /// <summary>
  96. /// Finds an enum value by name.
  97. /// </summary>
  98. /// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
  99. /// <returns>The value's descriptor, or null if not found.</returns>
  100. public EnumValueDescriptor FindValueByName(string name)
  101. {
  102. return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
  103. }
  104. internal override void ReplaceProto(EnumDescriptorProto newProto)
  105. {
  106. base.ReplaceProto(newProto);
  107. for (int i = 0; i < values.Count; i++)
  108. {
  109. values[i].ReplaceProto(newProto.GetValue(i));
  110. }
  111. }
  112. }
  113. }