[8295] | 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 Google.ProtocolBuffers.Descriptors;
|
---|
| 34 |
|
---|
| 35 | namespace Google.ProtocolBuffers.FieldAccess
|
---|
| 36 | {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Provides access to fields in generated messages via reflection.
|
---|
| 39 | /// This type is public to allow it to be used by generated messages, which
|
---|
| 40 | /// create appropriate instances in the .proto file description class.
|
---|
| 41 | /// TODO(jonskeet): See if we can hide it somewhere...
|
---|
| 42 | /// </summary>
|
---|
| 43 | public sealed class FieldAccessorTable<TMessage, TBuilder>
|
---|
| 44 | where TMessage : IMessage<TMessage, TBuilder>
|
---|
| 45 | where TBuilder : IBuilder<TMessage, TBuilder>
|
---|
| 46 | {
|
---|
| 47 | private readonly IFieldAccessor<TMessage, TBuilder>[] accessors;
|
---|
| 48 |
|
---|
| 49 | private readonly MessageDescriptor descriptor;
|
---|
| 50 |
|
---|
| 51 | public MessageDescriptor Descriptor
|
---|
| 52 | {
|
---|
| 53 | get { return descriptor; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /// <summary>
|
---|
| 57 | /// Constructs a FieldAccessorTable for a particular message class.
|
---|
| 58 | /// Only one FieldAccessorTable should be constructed per class.
|
---|
| 59 | /// The property names should all actually correspond with the field descriptor's
|
---|
| 60 | /// CSharpOptions.PropertyName property, but bootstrapping issues currently
|
---|
| 61 | /// prevent us from using that. This may be addressed at a future time, in which case
|
---|
| 62 | /// we can keep this constructor for backwards compatibility, just ignoring the parameter.
|
---|
| 63 | /// TODO(jonskeet): Make it so.
|
---|
| 64 | /// </summary>
|
---|
| 65 | /// <param name="descriptor">The type's descriptor</param>
|
---|
| 66 | /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
|
---|
| 67 | public FieldAccessorTable(MessageDescriptor descriptor, String[] propertyNames)
|
---|
| 68 | {
|
---|
| 69 | this.descriptor = descriptor;
|
---|
| 70 | accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
|
---|
| 71 | for (int i = 0; i < accessors.Length; i++)
|
---|
| 72 | {
|
---|
| 73 | accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i]);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /// <summary>
|
---|
| 78 | /// Creates an accessor for a single field
|
---|
| 79 | /// </summary>
|
---|
| 80 | private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name)
|
---|
| 81 | {
|
---|
| 82 | if (field.IsRepeated)
|
---|
| 83 | {
|
---|
| 84 | switch (field.MappedType)
|
---|
| 85 | {
|
---|
| 86 | case MappedType.Message:
|
---|
| 87 | return new RepeatedMessageAccessor<TMessage, TBuilder>(name);
|
---|
| 88 | case MappedType.Enum:
|
---|
| 89 | return new RepeatedEnumAccessor<TMessage, TBuilder>(field, name);
|
---|
| 90 | default:
|
---|
| 91 | return new RepeatedPrimitiveAccessor<TMessage, TBuilder>(name);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | else
|
---|
| 95 | {
|
---|
| 96 | switch (field.MappedType)
|
---|
| 97 | {
|
---|
| 98 | case MappedType.Message:
|
---|
| 99 | return new SingleMessageAccessor<TMessage, TBuilder>(name);
|
---|
| 100 | case MappedType.Enum:
|
---|
| 101 | return new SingleEnumAccessor<TMessage, TBuilder>(field, name);
|
---|
| 102 | default:
|
---|
| 103 | return new SinglePrimitiveAccessor<TMessage, TBuilder>(name);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | internal IFieldAccessor<TMessage, TBuilder> this[FieldDescriptor field]
|
---|
| 109 | {
|
---|
| 110 | get
|
---|
| 111 | {
|
---|
| 112 | if (field.ContainingType != descriptor)
|
---|
| 113 | {
|
---|
| 114 | throw new ArgumentException("FieldDescriptor does not match message type.");
|
---|
| 115 | }
|
---|
| 116 | else if (field.IsExtension)
|
---|
| 117 | {
|
---|
| 118 | // If this type had extensions, it would subclass ExtendableMessage,
|
---|
| 119 | // which overrides the reflection interface to handle extensions.
|
---|
| 120 | throw new ArgumentException("This type does not have extensions.");
|
---|
| 121 | }
|
---|
| 122 | return accessors[field.Index];
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | } |
---|