• 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 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 
12 namespace Google.Protobuf
13 {
14     /// <summary>
15     /// Represents a non-generic extension definition. This API is experimental and subject to change.
16     /// </summary>
17     public abstract class Extension
18     {
19         internal abstract Type TargetType { get; }
20 
21         /// <summary>
22         /// Internal use. Creates a new extension with the specified field number.
23         /// </summary>
Extension(int fieldNumber)24         protected Extension(int fieldNumber)
25         {
26             FieldNumber = fieldNumber;
27         }
28 
CreateValue()29         internal abstract IExtensionValue CreateValue();
30 
31         /// <summary>
32         /// Gets the field number of this extension
33         /// </summary>
34         public int FieldNumber { get; }
35 
36         internal abstract bool IsRepeated { get; }
37     }
38 
39     /// <summary>
40     /// Represents a type-safe extension identifier used for getting and setting single extension values in <see cref="IExtendableMessage{T}"/> instances.
41     /// This API is experimental and subject to change.
42     /// </summary>
43     /// <typeparam name="TTarget">The message type this field applies to</typeparam>
44     /// <typeparam name="TValue">The field value type of this extension</typeparam>
45     public sealed class Extension<TTarget, TValue> : Extension where TTarget : IExtendableMessage<TTarget>
46     {
47         private readonly FieldCodec<TValue> codec;
48 
49         /// <summary>
50         /// Creates a new extension identifier with the specified field number and codec
51         /// </summary>
Extension(int fieldNumber, FieldCodec<TValue> codec)52         public Extension(int fieldNumber, FieldCodec<TValue> codec) : base(fieldNumber)
53         {
54             this.codec = codec;
55         }
56 
57         internal TValue DefaultValue => codec != null ? codec.DefaultValue : default;
58 
59         internal override Type TargetType => typeof(TTarget);
60 
61         internal override bool IsRepeated => false;
62 
CreateValue()63         internal override IExtensionValue CreateValue()
64         {
65             return new ExtensionValue<TValue>(codec);
66         }
67     }
68 
69     /// <summary>
70     /// Represents a type-safe extension identifier used for getting repeated extension values in <see cref="IExtendableMessage{T}"/> instances.
71     /// This API is experimental and subject to change.
72     /// </summary>
73     /// <typeparam name="TTarget">The message type this field applies to</typeparam>
74     /// <typeparam name="TValue">The repeated field value type of this extension</typeparam>
75     public sealed class RepeatedExtension<TTarget, TValue> : Extension where TTarget : IExtendableMessage<TTarget>
76     {
77         private readonly FieldCodec<TValue> codec;
78 
79         /// <summary>
80         /// Creates a new repeated extension identifier with the specified field number and codec
81         /// </summary>
RepeatedExtension(int fieldNumber, FieldCodec<TValue> codec)82         public RepeatedExtension(int fieldNumber, FieldCodec<TValue> codec) : base(fieldNumber)
83         {
84             this.codec = codec;
85         }
86 
87         internal override Type TargetType => typeof(TTarget);
88 
89         internal override bool IsRepeated => true;
90 
CreateValue()91         internal override IExtensionValue CreateValue()
92         {
93             return new RepeatedExtensionValue<TValue>(codec);
94         }
95     }
96 }
97