RepeatedPrimitiveAccessor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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;
  17. using System.Collections;
  18. using System.Reflection;
  19. namespace Google.ProtocolBuffers.FieldAccess {
  20. /// <summary>
  21. /// Accesor for a repeated field of type int, ByteString etc.
  22. /// </summary>
  23. internal class RepeatedPrimitiveAccessor<TMessage, TBuilder> : IFieldAccessor<TMessage, TBuilder>
  24. where TMessage : IMessage<TMessage, TBuilder>
  25. where TBuilder : IBuilder<TMessage, TBuilder> {
  26. private readonly Type clrType;
  27. private readonly Func<TMessage, object> getValueDelegate;
  28. private readonly Func<TBuilder, IBuilder> clearDelegate;
  29. private readonly Action<TBuilder, object> addValueDelegate;
  30. private readonly Func<TBuilder, object> getRepeatedWrapperDelegate;
  31. private readonly Func<TMessage, int> countDelegate;
  32. private readonly MethodInfo getElementMethod;
  33. private readonly MethodInfo setElementMethod;
  34. /// <summary>
  35. /// The CLR type of the field (int, the enum type, ByteString, the message etc).
  36. /// This is taken from the return type of the method used to retrieve a single
  37. /// value.
  38. /// </summary>
  39. protected Type ClrType {
  40. get { return clrType; }
  41. }
  42. internal RepeatedPrimitiveAccessor(string name) {
  43. PropertyInfo messageProperty = typeof(TMessage).GetProperty(name + "List");
  44. PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name + "List");
  45. PropertyInfo countProperty = typeof(TMessage).GetProperty(name + "Count");
  46. MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
  47. getElementMethod = typeof(TMessage).GetMethod("Get" + name, new Type[] { typeof(int) });
  48. clrType = getElementMethod.ReturnType;
  49. MethodInfo addMethod = typeof(TBuilder).GetMethod("Add" + name, new Type[] { ClrType });
  50. setElementMethod = typeof(TBuilder).GetMethod("Set" + name, new Type[] { typeof(int), ClrType });
  51. if (messageProperty == null
  52. || builderProperty == null
  53. || countProperty == null
  54. || clearMethod == null
  55. || addMethod == null
  56. || getElementMethod == null
  57. || setElementMethod == null) {
  58. throw new ArgumentException("Not all required properties/methods available");
  59. }
  60. clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod);
  61. countDelegate = (Func<TMessage, int>)Delegate.CreateDelegate
  62. (typeof(Func<TMessage, int>), countProperty.GetGetMethod());
  63. getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
  64. addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn<TBuilder>(addMethod);
  65. getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate<TBuilder>(builderProperty.GetGetMethod());
  66. }
  67. public bool Has(TMessage message) {
  68. throw new InvalidOperationException();
  69. }
  70. public virtual IBuilder CreateBuilder() {
  71. throw new InvalidOperationException();
  72. }
  73. public virtual object GetValue(TMessage message) {
  74. return getValueDelegate(message);
  75. }
  76. public void SetValue(TBuilder builder, object value) {
  77. // Add all the elements individually. This serves two purposes:
  78. // 1) Verifies that each element has the correct type.
  79. // 2) Insures that the caller cannot modify the list later on and
  80. // have the modifications be reflected in the message.
  81. Clear(builder);
  82. foreach (object element in (IEnumerable) value) {
  83. AddRepeated(builder, element);
  84. }
  85. }
  86. public void Clear(TBuilder builder) {
  87. clearDelegate(builder);
  88. }
  89. public int GetRepeatedCount(TMessage message) {
  90. return countDelegate(message);
  91. }
  92. public virtual object GetRepeatedValue(TMessage message, int index) {
  93. return getElementMethod.Invoke(message, new object[] {index } );
  94. }
  95. public virtual void SetRepeated(TBuilder builder, int index, object value) {
  96. setElementMethod.Invoke(builder, new object[] {index, value} );
  97. }
  98. public virtual void AddRepeated(TBuilder builder, object value) {
  99. addValueDelegate(builder, value);
  100. }
  101. /// <summary>
  102. /// The builder class's accessor already builds a read-only wrapper for
  103. /// us, which is exactly what we want.
  104. /// </summary>
  105. public object GetRepeatedWrapper(TBuilder builder) {
  106. return getRepeatedWrapperDelegate(builder);
  107. }
  108. }
  109. }