• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 // https://developers.google.com/protocol-buffers/
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 //     * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #endregion
32 
33 using System;
34 
35 namespace Google.Protobuf
36 {
37     /// <summary>
38     /// Represents a non-generic extension definition. This API is experimental and subject to change.
39     /// </summary>
40     public abstract class Extension
41     {
42         internal abstract Type TargetType { get; }
43 
44         /// <summary>
45         /// Internal use. Creates a new extension with the specified field number.
46         /// </summary>
Extension(int fieldNumber)47         protected Extension(int fieldNumber)
48         {
49             FieldNumber = fieldNumber;
50         }
51 
CreateValue()52         internal abstract IExtensionValue CreateValue();
53 
54         /// <summary>
55         /// Gets the field number of this extension
56         /// </summary>
57         public int FieldNumber { get; }
58 
59         internal abstract bool IsRepeated { get; }
60     }
61 
62     /// <summary>
63     /// Represents a type-safe extension identifier used for getting and setting single extension values in <see cref="IExtendableMessage{T}"/> instances.
64     /// This API is experimental and subject to change.
65     /// </summary>
66     /// <typeparam name="TTarget">The message type this field applies to</typeparam>
67     /// <typeparam name="TValue">The field value type of this extension</typeparam>
68     public sealed class Extension<TTarget, TValue> : Extension where TTarget : IExtendableMessage<TTarget>
69     {
70         private readonly FieldCodec<TValue> codec;
71 
72         /// <summary>
73         /// Creates a new extension identifier with the specified field number and codec
74         /// </summary>
Extension(int fieldNumber, FieldCodec<TValue> codec)75         public Extension(int fieldNumber, FieldCodec<TValue> codec) : base(fieldNumber)
76         {
77             this.codec = codec;
78         }
79 
80         internal TValue DefaultValue => codec.DefaultValue;
81 
82         internal override Type TargetType => typeof(TTarget);
83 
84         internal override bool IsRepeated => false;
85 
CreateValue()86         internal override IExtensionValue CreateValue()
87         {
88             return new ExtensionValue<TValue>(codec);
89         }
90     }
91 
92     /// <summary>
93     /// Represents a type-safe extension identifier used for getting repeated extension values in <see cref="IExtendableMessage{T}"/> instances.
94     /// This API is experimental and subject to change.
95     /// </summary>
96     /// <typeparam name="TTarget">The message type this field applies to</typeparam>
97     /// <typeparam name="TValue">The repeated field value type of this extension</typeparam>
98     public sealed class RepeatedExtension<TTarget, TValue> : Extension where TTarget : IExtendableMessage<TTarget>
99     {
100         private readonly FieldCodec<TValue> codec;
101 
102         /// <summary>
103         /// Creates a new repeated extension identifier with the specified field number and codec
104         /// </summary>
RepeatedExtension(int fieldNumber, FieldCodec<TValue> codec)105         public RepeatedExtension(int fieldNumber, FieldCodec<TValue> codec) : base(fieldNumber)
106         {
107             this.codec = codec;
108         }
109 
110         internal override Type TargetType => typeof(TTarget);
111 
112         internal override bool IsRepeated => true;
113 
CreateValue()114         internal override IExtensionValue CreateValue()
115         {
116             return new RepeatedExtensionValue<TValue>(codec);
117         }
118     }
119 }
120