MessageDescriptor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// Describes a message type.
  21. /// </summary>
  22. public sealed class MessageDescriptor : IndexedDescriptorBase<DescriptorProto, MessageOptions> {
  23. private readonly MessageDescriptor containingType;
  24. private readonly IList<MessageDescriptor> nestedTypes;
  25. private readonly IList<EnumDescriptor> enumTypes;
  26. private readonly IList<FieldDescriptor> fields;
  27. private readonly IList<FieldDescriptor> extensions;
  28. internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex)
  29. : base(proto, file, ComputeFullName(file, parent, proto.Name), typeIndex) {
  30. containingType = parent;
  31. nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedTypeList,
  32. (type, index) => new MessageDescriptor(type, file, this, index));
  33. enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumTypeList,
  34. (type, index) => new EnumDescriptor(type, file, this, index));
  35. fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.FieldList,
  36. (field, index) => new FieldDescriptor(field, file, this, index, false));
  37. extensions = DescriptorUtil.ConvertAndMakeReadOnly(proto.ExtensionList,
  38. (field, index) => new FieldDescriptor(field, file, this, index, true));
  39. file.DescriptorPool.AddSymbol(this);
  40. }
  41. /// <value>
  42. /// If this is a nested type, get the outer descriptor, otherwise null.
  43. /// </value>
  44. public MessageDescriptor ContainingType {
  45. get { return containingType; }
  46. }
  47. /// <value>
  48. /// An unmodifiable list of this message type's fields.
  49. /// </value>
  50. public IList<FieldDescriptor> Fields {
  51. get { return fields; }
  52. }
  53. /// <value>
  54. /// An unmodifiable list of this message type's extensions.
  55. /// </value>
  56. public IList<FieldDescriptor> Extensions {
  57. get { return extensions; }
  58. }
  59. /// <value>
  60. /// An unmodifiable list of this message type's nested types.
  61. /// </value>
  62. public IList<MessageDescriptor> NestedTypes {
  63. get { return nestedTypes; }
  64. }
  65. /// <value>
  66. /// An unmodifiable list of this message type's enum types.
  67. /// </value>
  68. public IList<EnumDescriptor> EnumTypes {
  69. get { return enumTypes; }
  70. }
  71. /// <summary>
  72. /// Determines if the given field number is an extension.
  73. /// </summary>
  74. public bool IsExtensionNumber(int number) {
  75. foreach (DescriptorProto.Types.ExtensionRange range in Proto.ExtensionRangeList) {
  76. if (range.Start <= number && number < range.End) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. /// <summary>
  83. /// Finds a field by field number.
  84. /// </summary>
  85. /// <param name="number">The field number within this message type.</param>
  86. /// <returns>The field's descriptor, or null if not found.</returns>
  87. public FieldDescriptor FindFieldByNumber(int number) {
  88. return File.DescriptorPool.FindFieldByNumber(this, number);
  89. }
  90. /// <summary>
  91. /// Finds a nested descriptor by name. The is valid for fields, nested
  92. /// message types and enums.
  93. /// </summary>
  94. /// <param name="name">The unqualified name of the descriptor, e.g. "Foo"</param>
  95. /// <returns>The descriptor, or null if not found.</returns>
  96. public T FindDescriptor<T>(string name)
  97. where T : class, IDescriptor {
  98. return File.DescriptorPool.FindSymbol<T>(FullName + "." + name);
  99. }
  100. /// <summary>
  101. /// Looks up and cross-links all fields, nested types, and extensions.
  102. /// </summary>
  103. internal void CrossLink() {
  104. foreach (MessageDescriptor message in nestedTypes) {
  105. message.CrossLink();
  106. }
  107. foreach (FieldDescriptor field in fields) {
  108. field.CrossLink();
  109. }
  110. foreach (FieldDescriptor extension in extensions) {
  111. extension.CrossLink();
  112. }
  113. }
  114. }
  115. }