Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.6/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs

Last change on this file was 3857, checked in by abeham, 14 years ago

#866

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