diff options
Diffstat (limited to 'csharp/ProtocolBuffers/FieldAccess')
9 files changed, 0 insertions, 750 deletions
diff --git a/csharp/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs deleted file mode 100644 index c7f5da6d..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using Google.ProtocolBuffers.Descriptors; - -namespace Google.ProtocolBuffers.FieldAccess { - /// <summary> - /// Provides access to fields in generated messages via reflection. - /// This type is public to allow it to be used by generated messages, which - /// create appropriate instances in the .proto file description class. - /// TODO(jonskeet): See if we can hide it somewhere... - /// </summary> - public sealed class FieldAccessorTable<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - readonly IFieldAccessor<TMessage, TBuilder>[] accessors; - - readonly MessageDescriptor descriptor; - - public MessageDescriptor Descriptor { - get { return descriptor; } - } - - /// <summary> - /// Constructs a FieldAccessorTable for a particular message class. - /// Only one FieldAccessorTable should be constructed per class. - /// </summary> - /// <param name="descriptor">The type's descriptor</param> - /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param> - public FieldAccessorTable(MessageDescriptor descriptor, String[] propertyNames) { - this.descriptor = descriptor; - accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count]; - for (int i=0; i < accessors.Length; i++) { - accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i]); - } - } - - /// <summary> - /// Creates an accessor for a single field - /// </summary> - private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name) { - if (field.IsRepeated) { - switch (field.MappedType) { - case MappedType.Message: return new RepeatedMessageAccessor<TMessage, TBuilder>(name); - case MappedType.Enum: return new RepeatedEnumAccessor<TMessage, TBuilder>(field, name); - default: return new RepeatedPrimitiveAccessor<TMessage, TBuilder>(name); - } - } else { - switch (field.MappedType) { - case MappedType.Message: return new SingleMessageAccessor<TMessage, TBuilder>(name); - case MappedType.Enum: return new SingleEnumAccessor<TMessage, TBuilder>(field, name); - default: return new SinglePrimitiveAccessor<TMessage, TBuilder>(name); - } - } - } - - internal IFieldAccessor<TMessage, TBuilder> this[FieldDescriptor field] { - get { - if (field.ContainingType != descriptor) { - throw new ArgumentException("FieldDescriptor does not match message type."); - } else if (field.IsExtension) { - // If this type had extensions, it would subclass ExtendableMessage, - // which overrides the reflection interface to handle extensions. - throw new ArgumentException("This type does not have extensions."); - } - return accessors[field.Index]; - } - } - } -} diff --git a/csharp/ProtocolBuffers/FieldAccess/IFieldAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/IFieldAccessor.cs deleted file mode 100644 index ec079f92..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/IFieldAccessor.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Google.ProtocolBuffers.FieldAccess { - - /// <summary> - /// Allows fields to be reflectively accessed in a smart manner. - /// The property descriptors for each field are created once and then cached. - /// In addition, this interface holds knowledge of repeated fields, builders etc. - /// </summary> - internal interface IFieldAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - /// <summary> - /// Indicates whether the specified message contains the field. - /// </summary> - bool Has(TMessage message); - - /// <summary> - /// Gets the count of the repeated field in the specified message. - /// </summary> - int GetRepeatedCount(TMessage message); - - /// <summary> - /// Clears the field in the specified builder. - /// </summary> - /// <param name="builder"></param> - void Clear(TBuilder builder); - - /// <summary> - /// Creates a builder for the type of this field (which must be a message field). - /// </summary> - IBuilder CreateBuilder(); - - /// <summary> - /// Accessor for single fields - /// </summary> - object GetValue(TMessage message); - /// <summary> - /// Mutator for single fields - /// </summary> - void SetValue(TBuilder builder, object value); - - /// <summary> - /// Accessor for repeated fields - /// </summary> - object GetRepeatedValue(TMessage message, int index); - /// <summary> - /// Mutator for repeated fields - /// </summary> - void SetRepeated(TBuilder builder, int index, object value); - /// <summary> - /// Adds the specified value to the field in the given builder. - /// </summary> - void AddRepeated(TBuilder builder, object value); - /// <summary> - /// Returns a read-only wrapper around the value of a repeated field. - /// </summary> - object GetRepeatedWrapper(TBuilder builder); - } -} diff --git a/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs deleted file mode 100644 index ddec2229..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs +++ /dev/null @@ -1,109 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using System.Reflection; - -namespace Google.ProtocolBuffers.FieldAccess { - - /// <summary> - /// The methods in this class are somewhat evil, and should not be tampered with lightly. - /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos - /// which are more strongly typed. They do this by creating an appropriate strongly typed - /// delegate from the MethodInfo, and then calling that within an anonymous method. - /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are - /// very fast compared with calling Invoke later on. - /// </summary> - internal static class ReflectionUtil { - - /// <summary> - /// Creates a delegate which will execute the given method and then return - /// the result as an object. - /// </summary> - public static Func<T, object> CreateUpcastDelegate<T>(MethodInfo method) { - - // The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters - MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl"); - MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.ReturnType); - return (Func<T, object>) closedImpl.Invoke(null, new object[] { method }); - } - - /// <summary> - /// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues - /// in low-trust scenarios, e.g. Silverlight. - /// TODO(jonskeet): Check any of this actually works in Silverlight... - /// </summary> - public static Func<TSource, object> CreateUpcastDelegateImpl<TSource, TResult>(MethodInfo method) { - // Convert the reflection call into an open delegate, i.e. instead of calling x.Method() - // we'll call getter(x). - Func<TSource, TResult> getter = (Func<TSource, TResult>)Delegate.CreateDelegate(typeof(Func<TSource, TResult>), method); - - // Implicit upcast to object (within the delegate) - return delegate(TSource source) { return getter(source); }; - } - - - /// <summary> - /// Creates a delegate which will execute the given method after casting the parameter - /// down from object to the required parameter type. - /// </summary> - public static Action<T, object> CreateDowncastDelegate<T>(MethodInfo method) { - MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl"); - MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType); - return (Action<T, object>) closedImpl.Invoke(null, new object[] { method }); - } - - public static Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) { - // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll - // call Method(x, y) - Action<TSource, TParam> call = (Action<TSource, TParam>) Delegate.CreateDelegate(typeof(Action<TSource, TParam>), method); - - return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; - } - - /// <summary> - /// Creates a delegate which will execute the given method after casting the parameter - /// down from object to the required parameter type. - /// </summary> - public static Action<T, object> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method) { - MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl"); - MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType); - return (Action<T, object>)closedImpl.Invoke(null, new object[] { method }); - } - - public static Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) { - // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll - // call Method(x, y) - Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>) - Delegate.CreateDelegate(typeof(Func<TSource, TParam, TReturn>), method); - - return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; - } - - /// <summary> - /// Creates a delegate which will execute the given static method and cast the result up to IBuilder. - /// </summary> - public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method) { - MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl"); - MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType); - return (Func<IBuilder>)closedImpl.Invoke(null, new object[] { method }); - } - - public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method) { - Func<T> call = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), method); - return delegate { return (IBuilder)call(); }; - } - } -} diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedEnumAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedEnumAccessor.cs deleted file mode 100644 index f00f7ee2..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedEnumAccessor.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using System.Collections; -using System.Collections.Generic; -using Google.ProtocolBuffers.Collections; -using Google.ProtocolBuffers.Descriptors; - -namespace Google.ProtocolBuffers.FieldAccess { - - /// <summary> - /// Accessor for a repeated enum field. - /// </summary> - internal sealed class RepeatedEnumAccessor<TMessage, TBuilder> : RepeatedPrimitiveAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - private readonly EnumDescriptor enumDescriptor; - - internal RepeatedEnumAccessor(FieldDescriptor field, string name) : base(name) { - enumDescriptor = field.EnumType; - } - - public override object GetValue(TMessage message) { - List<EnumValueDescriptor> ret = new List<EnumValueDescriptor>(); - foreach (int rawValue in (IEnumerable) base.GetValue(message)) { - ret.Add(enumDescriptor.FindValueByNumber(rawValue)); - } - return Lists.AsReadOnly(ret); - } - - public override object GetRepeatedValue(TMessage message, int index) { - // Note: This relies on the fact that the CLR allows unboxing from an enum to - // its underlying value - int rawValue = (int) base.GetRepeatedValue(message, index); - return enumDescriptor.FindValueByNumber(rawValue); - } - - public override void AddRepeated(TBuilder builder, object value) { - base.AddRepeated(builder, ((EnumValueDescriptor) value).Number); - } - - public override void SetRepeated(TBuilder builder, int index, object value) { - base.SetRepeated(builder, index, ((EnumValueDescriptor) value).Number); - } - } -} diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs deleted file mode 100644 index 35d7b1a8..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using System.Reflection; - -namespace Google.ProtocolBuffers.FieldAccess { - - /// <summary> - /// Accessor for a repeated message field. - /// - /// TODO(jonskeet): Try to extract the commonality between this and SingleMessageAccessor. - /// We almost want multiple inheritance... - /// </summary> - internal sealed class RepeatedMessageAccessor<TMessage, TBuilder> : RepeatedPrimitiveAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - /// <summary> - /// The static method to create a builder for the property type. For example, - /// in a message type "Foo", a field called "bar" might be of type "Baz". This - /// method is Baz.CreateBuilder. - /// </summary> - private readonly Func<IBuilder> createBuilderDelegate; - - internal RepeatedMessageAccessor(string name) : base(name) { - MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", Type.EmptyTypes); - if (createBuilderMethod == null) { - throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name); - } - createBuilderDelegate = ReflectionUtil.CreateStaticUpcastDelegate(createBuilderMethod); - } - - /// <summary> - /// Creates a message of the appropriate CLR type from the given value, - /// which may already be of the right type or may be a dynamic message. - /// </summary> - private object CoerceType(object value) { - - // If it's already of the right type, we're done - if (ClrType.IsInstanceOfType(value)) { - return value; - } - - // No... so let's create a builder of the right type, and merge the value in. - IMessage message = (IMessage) value; - return CreateBuilder().WeakMergeFrom(message).WeakBuild(); - } - - public override void SetRepeated(TBuilder builder, int index, object value) { - base.SetRepeated(builder, index, CoerceType(value)); - } - - public override IBuilder CreateBuilder() { - return createBuilderDelegate(); - } - - public override void AddRepeated(TBuilder builder, object value) { - base.AddRepeated(builder, CoerceType(value)); - } - } -} diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs deleted file mode 100644 index 35a2a204..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs +++ /dev/null @@ -1,124 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using System.Collections; -using System.Reflection; - -namespace Google.ProtocolBuffers.FieldAccess { - /// <summary> - /// Accesor for a repeated field of type int, ByteString etc. - /// </summary> - internal class RepeatedPrimitiveAccessor<TMessage, TBuilder> : IFieldAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - private readonly Type clrType; - private readonly Func<TMessage, object> getValueDelegate; - private readonly Func<TBuilder, IBuilder> clearDelegate; - private readonly Action<TBuilder, object> addValueDelegate; - private readonly Func<TBuilder, object> getRepeatedWrapperDelegate; - private readonly Func<TMessage, int> countDelegate; - private readonly MethodInfo getElementMethod; - private readonly MethodInfo setElementMethod; - - - /// <summary> - /// The CLR type of the field (int, the enum type, ByteString, the message etc). - /// This is taken from the return type of the method used to retrieve a single - /// value. - /// </summary> - protected Type ClrType { - get { return clrType; } - } - - internal RepeatedPrimitiveAccessor(string name) { - PropertyInfo messageProperty = typeof(TMessage).GetProperty(name + "List"); - PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name + "List"); - PropertyInfo countProperty = typeof(TMessage).GetProperty(name + "Count"); - MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name, Type.EmptyTypes); - getElementMethod = typeof(TMessage).GetMethod("Get" + name, new Type[] { typeof(int) }); - clrType = getElementMethod.ReturnType; - MethodInfo addMethod = typeof(TBuilder).GetMethod("Add" + name, new Type[] { ClrType }); - setElementMethod = typeof(TBuilder).GetMethod("Set" + name, new Type[] { typeof(int), ClrType }); - if (messageProperty == null - || builderProperty == null - || countProperty == null - || clearMethod == null - || addMethod == null - || getElementMethod == null - || setElementMethod == null) { - throw new ArgumentException("Not all required properties/methods available"); - } - clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod); - countDelegate = (Func<TMessage, int>)Delegate.CreateDelegate - (typeof(Func<TMessage, int>), countProperty.GetGetMethod()); - getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod()); - addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn<TBuilder>(addMethod); - getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate<TBuilder>(builderProperty.GetGetMethod()); - } - - public bool Has(TMessage message) { - throw new InvalidOperationException(); - } - - public virtual IBuilder CreateBuilder() { - throw new InvalidOperationException(); - } - - public virtual object GetValue(TMessage message) { - return getValueDelegate(message); - } - - public void SetValue(TBuilder builder, object value) { - // Add all the elements individually. This serves two purposes: - // 1) Verifies that each element has the correct type. - // 2) Insures that the caller cannot modify the list later on and - // have the modifications be reflected in the message. - Clear(builder); - foreach (object element in (IEnumerable) value) { - AddRepeated(builder, element); - } - } - - public void Clear(TBuilder builder) { - clearDelegate(builder); - } - - public int GetRepeatedCount(TMessage message) { - return countDelegate(message); - } - - public virtual object GetRepeatedValue(TMessage message, int index) { - return getElementMethod.Invoke(message, new object[] {index } ); - } - - public virtual void SetRepeated(TBuilder builder, int index, object value) { - setElementMethod.Invoke(builder, new object[] {index, value} ); - } - - public virtual void AddRepeated(TBuilder builder, object value) { - addValueDelegate(builder, value); - } - - /// <summary> - /// The builder class's accessor already builds a read-only wrapper for - /// us, which is exactly what we want. - /// </summary> - public object GetRepeatedWrapper(TBuilder builder) { - return getRepeatedWrapperDelegate(builder); - } - } -}
\ No newline at end of file diff --git a/csharp/ProtocolBuffers/FieldAccess/SingleEnumAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/SingleEnumAccessor.cs deleted file mode 100644 index eb28160a..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/SingleEnumAccessor.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using Google.ProtocolBuffers.Descriptors; - -namespace Google.ProtocolBuffers.FieldAccess { - /// <summary> - /// Accessor for fields representing a non-repeated enum value. - /// </summary> - internal sealed class SingleEnumAccessor<TMessage, TBuilder> : SinglePrimitiveAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - private readonly EnumDescriptor enumDescriptor; - - internal SingleEnumAccessor(FieldDescriptor field, string name) : base(name) { - enumDescriptor = field.EnumType; - } - - /// <summary> - /// Returns an EnumValueDescriptor representing the value in the builder. - /// Note that if an enum has multiple values for the same number, the descriptor - /// for the first value with that number will be returned. - /// </summary> - public override object GetValue(TMessage message) { - // Note: This relies on the fact that the CLR allows unboxing from an enum to - // its underlying value - int rawValue = (int) base.GetValue(message); - return enumDescriptor.FindValueByNumber(rawValue); - } - - /// <summary> - /// Sets the value as an enum (via an int) in the builder, - /// from an EnumValueDescriptor parameter. - /// </summary> - public override void SetValue(TBuilder builder, object value) { - EnumValueDescriptor valueDescriptor = (EnumValueDescriptor) value; - base.SetValue(builder, valueDescriptor.Number); - } - } -}
\ No newline at end of file diff --git a/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs deleted file mode 100644 index 5aa9f777..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs +++ /dev/null @@ -1,66 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using System.Reflection; - -namespace Google.ProtocolBuffers.FieldAccess { - /// <summary> - /// Accessor for fields representing a non-repeated message value. - /// </summary> - internal sealed class SingleMessageAccessor<TMessage, TBuilder> : SinglePrimitiveAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - /// <summary> - /// The static method to create a builder for the property type. For example, - /// in a message type "Foo", a field called "bar" might be of type "Baz". This - /// method is Baz.CreateBuilder. - /// </summary> - private readonly Func<IBuilder> createBuilderDelegate; - - internal SingleMessageAccessor(string name) : base(name) { - MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", Type.EmptyTypes); - if (createBuilderMethod == null) { - throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name); - } - createBuilderDelegate = ReflectionUtil.CreateStaticUpcastDelegate(createBuilderMethod); - } - - /// <summary> - /// Creates a message of the appropriate CLR type from the given value, - /// which may already be of the right type or may be a dynamic message. - /// </summary> - private object CoerceType(object value) { - - // If it's already of the right type, we're done - if (ClrType.IsInstanceOfType(value)) { - return value; - } - - // No... so let's create a builder of the right type, and merge the value in. - IMessage message = (IMessage) value; - return CreateBuilder().WeakMergeFrom(message).WeakBuild(); - } - - public override void SetValue(TBuilder builder, object value) { - base.SetValue(builder, CoerceType(value)); - } - - public override IBuilder CreateBuilder() { - return createBuilderDelegate(); - } - } -}
\ No newline at end of file diff --git a/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs deleted file mode 100644 index c10936d4..00000000 --- a/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs +++ /dev/null @@ -1,104 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. -// http://code.google.com/p/protobuf/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -using System; -using System.Reflection; - -namespace Google.ProtocolBuffers.FieldAccess { - /// <summary> - /// Access for a non-repeated field of a "primitive" type (i.e. not another message or an enum). - /// </summary> - internal class SinglePrimitiveAccessor<TMessage, TBuilder> : IFieldAccessor<TMessage, TBuilder> - where TMessage : IMessage<TMessage, TBuilder> - where TBuilder : IBuilder<TMessage, TBuilder> { - - private readonly Type clrType; - private readonly Func<TMessage, object> getValueDelegate; - private readonly Action<TBuilder, object> setValueDelegate; - private readonly Func<TMessage, bool> hasDelegate; - private readonly Func<TBuilder, IBuilder> clearDelegate; - - /// <summary> - /// The CLR type of the field (int, the enum type, ByteString, the message etc). - /// As declared by the property. - /// </summary> - protected Type ClrType { - get { return clrType; } - } - - internal SinglePrimitiveAccessor(string name) { - - string propertyName = name == typeof(TMessage).Name ? name + "_" : name; - PropertyInfo messageProperty = typeof(TMessage).GetProperty(propertyName); - PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name); // FIXME! - if (builderProperty == null) builderProperty = typeof(TBuilder).GetProperty(propertyName); // FIXME! - PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name); - MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name, Type.EmptyTypes); - if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null) { - throw new ArgumentException("Not all required properties/methods available"); - } - clrType = messageProperty.PropertyType; - hasDelegate = (Func<TMessage, bool>)Delegate.CreateDelegate(typeof(Func<TMessage, bool>), hasProperty.GetGetMethod()); - clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod); - getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod()); - setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod()); - } - - public bool Has(TMessage message) { - return hasDelegate(message); - } - - public void Clear(TBuilder builder) { - clearDelegate(builder); - } - - /// <summary> - /// Only valid for message types - this implementation throws InvalidOperationException. - /// </summary> - public virtual IBuilder CreateBuilder() { - throw new InvalidOperationException(); - } - - public virtual object GetValue(TMessage message) { - return getValueDelegate(message); - } - - public virtual void SetValue(TBuilder builder, object value) { - setValueDelegate(builder, value); - } - - #region Methods only related to repeated values - public int GetRepeatedCount(TMessage message) { - throw new InvalidOperationException(); - } - - public object GetRepeatedValue(TMessage message, int index) { - throw new InvalidOperationException(); - } - - public void SetRepeated(TBuilder builder, int index, object value) { - throw new InvalidOperationException(); - } - - public void AddRepeated(TBuilder builder, object value) { - throw new InvalidOperationException(); - } - - public object GetRepeatedWrapper(TBuilder builder) { - throw new InvalidOperationException(); - } - #endregion - } -}
\ No newline at end of file |