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