SinglePrimitiveAccessor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 System.Reflection;
  34. namespace Google.ProtocolBuffers.FieldAccess {
  35. /// <summary>
  36. /// Access for a non-repeated field of a "primitive" type (i.e. not another message or an enum).
  37. /// </summary>
  38. internal class SinglePrimitiveAccessor<TMessage, TBuilder> : IFieldAccessor<TMessage, TBuilder>
  39. where TMessage : IMessage<TMessage, TBuilder>
  40. where TBuilder : IBuilder<TMessage, TBuilder> {
  41. private readonly Type clrType;
  42. private readonly Func<TMessage, object> getValueDelegate;
  43. private readonly Action<TBuilder, object> setValueDelegate;
  44. private readonly Func<TMessage, bool> hasDelegate;
  45. private readonly Func<TBuilder, IBuilder> clearDelegate;
  46. /// <summary>
  47. /// The CLR type of the field (int, the enum type, ByteString, the message etc).
  48. /// As declared by the property.
  49. /// </summary>
  50. protected Type ClrType {
  51. get { return clrType; }
  52. }
  53. internal SinglePrimitiveAccessor(string name) {
  54. string propertyName = name == typeof(TMessage).Name ? name + "_" : name;
  55. PropertyInfo messageProperty = typeof(TMessage).GetProperty(propertyName);
  56. PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name); // FIXME!
  57. if (builderProperty == null) builderProperty = typeof(TBuilder).GetProperty(propertyName); // FIXME!
  58. PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name);
  59. MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name, Type.EmptyTypes);
  60. if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null) {
  61. throw new ArgumentException("Not all required properties/methods available");
  62. }
  63. clrType = messageProperty.PropertyType;
  64. hasDelegate = (Func<TMessage, bool>)Delegate.CreateDelegate(typeof(Func<TMessage, bool>), hasProperty.GetGetMethod());
  65. clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod);
  66. getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
  67. setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
  68. }
  69. public bool Has(TMessage message) {
  70. return hasDelegate(message);
  71. }
  72. public void Clear(TBuilder builder) {
  73. clearDelegate(builder);
  74. }
  75. /// <summary>
  76. /// Only valid for message types - this implementation throws InvalidOperationException.
  77. /// </summary>
  78. public virtual IBuilder CreateBuilder() {
  79. throw new InvalidOperationException();
  80. }
  81. public virtual object GetValue(TMessage message) {
  82. return getValueDelegate(message);
  83. }
  84. public virtual void SetValue(TBuilder builder, object value) {
  85. setValueDelegate(builder, value);
  86. }
  87. #region Methods only related to repeated values
  88. public int GetRepeatedCount(TMessage message) {
  89. throw new InvalidOperationException();
  90. }
  91. public object GetRepeatedValue(TMessage message, int index) {
  92. throw new InvalidOperationException();
  93. }
  94. public void SetRepeated(TBuilder builder, int index, object value) {
  95. throw new InvalidOperationException();
  96. }
  97. public void AddRepeated(TBuilder builder, object value) {
  98. throw new InvalidOperationException();
  99. }
  100. public object GetRepeatedWrapper(TBuilder builder) {
  101. throw new InvalidOperationException();
  102. }
  103. #endregion
  104. }
  105. }