Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/GeneratedExtensionBase.cs @ 4095

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

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 6.3 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections;
37using System.Collections.Generic;
38using System.Reflection;
39using Google.ProtocolBuffers.Descriptors;
40
41namespace Google.ProtocolBuffers {
42  /// <summary>
43  /// Base type for all generated extensions.
44  /// </summary>
45  /// <remarks>
46  /// The protocol compiler generates a static singleton instance of this
47  /// class for each extension. For exmaple, imagine a .proto file with:
48  /// <code>
49  /// message Foo {
50  ///   extensions 1000 to max
51  /// }
52  ///
53  /// extend Foo {
54  ///   optional int32 bar;
55  /// }
56  /// </code>
57  /// Then MyProto.Foo.Bar has type GeneratedExtensionBase&lt;MyProto.Foo,int&gt;.
58  /// <para />
59  /// In general, users should ignore the details of this type, and
60  /// simply use the static singletons as parameters to the extension accessors
61  /// in ExtendableMessage and ExtendableBuilder.
62  /// The interface implemented by both GeneratedException and GeneratedRepeatException,
63  /// to make it easier to cope with repeats separately.
64  /// </remarks>
65  public abstract class GeneratedExtensionBase<TExtension> {
66
67    private readonly FieldDescriptor descriptor;
68    private readonly IMessage messageDefaultInstance;
69
70    protected GeneratedExtensionBase(FieldDescriptor descriptor, Type singularExtensionType) {
71      if (!descriptor.IsExtension) {
72        throw new ArgumentException("GeneratedExtension given a regular (non-extension) field.");
73      }
74
75      this.descriptor = descriptor;
76      if (descriptor.MappedType == MappedType.Message) {
77        PropertyInfo defaultInstanceProperty = singularExtensionType
78            .GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public);
79        if (defaultInstanceProperty == null) {
80          throw new ArgumentException("No public static DefaultInstance property for type " + typeof(TExtension).Name);
81        }
82        messageDefaultInstance = (IMessage)defaultInstanceProperty.GetValue(null, null);
83      }
84    }
85
86    public FieldDescriptor Descriptor {
87      get { return descriptor; }
88    }
89
90    /// <summary>
91    /// Returns the default message instance for extensions which are message types.
92    /// </summary>
93    public IMessage MessageDefaultInstance {
94      get { return messageDefaultInstance; }
95    }
96
97    public object SingularFromReflectionType(object value) {
98      switch (Descriptor.MappedType) {
99        case MappedType.Message:
100          if (value is TExtension) {
101            return value;
102          } else {
103            // It seems the copy of the embedded message stored inside the
104            // extended message is not of the exact type the user was
105            // expecting.  This can happen if a user defines a
106            // GeneratedExtension manually and gives it a different type.
107            // This should not happen in normal use.  But, to be nice, we'll
108            // copy the message to whatever type the caller was expecting.
109            return MessageDefaultInstance.WeakCreateBuilderForType()
110                           .WeakMergeFrom((IMessage)value).WeakBuild();
111          }
112        case MappedType.Enum:
113          // Just return a boxed int - that can be unboxed to the enum
114          EnumValueDescriptor enumValue = (EnumValueDescriptor) value;
115          return enumValue.Number;
116        default:
117          return value;
118      }
119    }
120
121    /// <summary>
122    /// Converts from the type used by the native accessors to the type
123    /// used by reflection accessors. For example, the reflection accessors
124    /// for enums use EnumValueDescriptors but the native accessors use
125    /// the generated enum type.
126    /// </summary>
127    public object ToReflectionType(object value) {
128      if (descriptor.IsRepeated) {
129        if (descriptor.MappedType == MappedType.Enum) {
130          // Must convert the whole list.
131          IList<object> result = new List<object>();
132          foreach (object element in (IEnumerable) value) {
133            result.Add(SingularToReflectionType(element));
134          }
135          return result;
136        } else {
137          return value;
138        }
139      } else {
140        return SingularToReflectionType(value);
141      }
142    }
143
144    /// <summary>
145    /// Like ToReflectionType(object) but for a single element.
146    /// </summary>
147    internal Object SingularToReflectionType(object value) {
148      return descriptor.MappedType == MappedType.Enum
149          ? descriptor.EnumType.FindValueByNumber((int) value)
150          : value;
151    }
152
153    public abstract object FromReflectionType(object value);
154  }
155}
Note: See TracBrowser for help on using the repository browser.