FieldAccessorTable.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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;
  33. using Google.ProtocolBuffers.Descriptors;
  34. namespace Google.ProtocolBuffers.FieldAccess
  35. {
  36. /// <summary>
  37. /// Provides access to fields in generated messages via reflection.
  38. /// This type is public to allow it to be used by generated messages, which
  39. /// create appropriate instances in the .proto file description class.
  40. /// TODO(jonskeet): See if we can hide it somewhere...
  41. /// </summary>
  42. public sealed class FieldAccessorTable<TMessage, TBuilder>
  43. where TMessage : IMessage<TMessage, TBuilder>
  44. where TBuilder : IBuilder<TMessage, TBuilder>
  45. {
  46. private readonly IFieldAccessor<TMessage, TBuilder>[] accessors;
  47. private readonly MessageDescriptor descriptor;
  48. public MessageDescriptor Descriptor
  49. {
  50. get { return descriptor; }
  51. }
  52. /// <summary>
  53. /// Constructs a FieldAccessorTable for a particular message class.
  54. /// Only one FieldAccessorTable should be constructed per class.
  55. /// The property names should all actually correspond with the field descriptor's
  56. /// CSharpOptions.PropertyName property, but bootstrapping issues currently
  57. /// prevent us from using that. This may be addressed at a future time, in which case
  58. /// we can keep this constructor for backwards compatibility, just ignoring the parameter.
  59. /// TODO(jonskeet): Make it so.
  60. /// </summary>
  61. /// <param name="descriptor">The type's descriptor</param>
  62. /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
  63. public FieldAccessorTable(MessageDescriptor descriptor, String[] propertyNames)
  64. {
  65. this.descriptor = descriptor;
  66. accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
  67. bool supportFieldPresence = false;
  68. if (descriptor.File.GetSyntax() == FileDescriptor.Syntax.PROTO2)
  69. {
  70. supportFieldPresence = true;
  71. }
  72. for (int i = 0; i < accessors.Length; i++)
  73. {
  74. accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence);
  75. }
  76. }
  77. /// <summary>
  78. /// Creates an accessor for a single field
  79. /// </summary>
  80. private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name, bool supportFieldPresence)
  81. {
  82. if (field.IsRepeated)
  83. {
  84. switch (field.MappedType)
  85. {
  86. case MappedType.Message:
  87. return new RepeatedMessageAccessor<TMessage, TBuilder>(name);
  88. case MappedType.Enum:
  89. return new RepeatedEnumAccessor<TMessage, TBuilder>(field, name);
  90. default:
  91. return new RepeatedPrimitiveAccessor<TMessage, TBuilder>(name);
  92. }
  93. }
  94. else
  95. {
  96. switch (field.MappedType)
  97. {
  98. case MappedType.Message:
  99. return new SingleMessageAccessor<TMessage, TBuilder>(name);
  100. case MappedType.Enum:
  101. return new SingleEnumAccessor<TMessage, TBuilder>(field, name, supportFieldPresence);
  102. default:
  103. return new SinglePrimitiveAccessor<TMessage, TBuilder>(field, name, supportFieldPresence);
  104. }
  105. }
  106. }
  107. internal IFieldAccessor<TMessage, TBuilder> this[FieldDescriptor field]
  108. {
  109. get
  110. {
  111. if (field.ContainingType != descriptor)
  112. {
  113. throw new ArgumentException("FieldDescriptor does not match message type.");
  114. }
  115. else if (field.IsExtension)
  116. {
  117. // If this type had extensions, it would subclass ExtendableMessage,
  118. // which overrides the reflection interface to handle extensions.
  119. throw new ArgumentException("This type does not have extensions.");
  120. }
  121. return accessors[field.Index];
  122. }
  123. }
  124. }
  125. }