FieldAccessorTable.cs 4.9 KB

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