xref: /aosp_15_r20/external/cronet/third_party/protobuf/csharp/src/Google.Protobuf/ExtensionValue.cs (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 Google.Protobuf.Collections;
34 using System;
35 using System.Linq;
36 
37 namespace Google.Protobuf
38 {
39     internal interface IExtensionValue : IEquatable<IExtensionValue>, IDeepCloneable<IExtensionValue>
40     {
MergeFrom(ref ParseContext ctx)41         void MergeFrom(ref ParseContext ctx);
42 
MergeFrom(IExtensionValue value)43         void MergeFrom(IExtensionValue value);
WriteTo(ref WriteContext ctx)44         void WriteTo(ref WriteContext ctx);
CalculateSize()45         int CalculateSize();
IsInitialized()46         bool IsInitialized();
GetValue()47         object GetValue();
48     }
49 
50     internal sealed class ExtensionValue<T> : IExtensionValue
51     {
52         private T field;
53         private FieldCodec<T> codec;
54 
ExtensionValue(FieldCodec<T> codec)55         internal ExtensionValue(FieldCodec<T> codec)
56         {
57             this.codec = codec;
58             field = codec.DefaultValue;
59         }
60 
CalculateSize()61         public int CalculateSize()
62         {
63             return codec.CalculateUnconditionalSizeWithTag(field);
64         }
65 
Clone()66         public IExtensionValue Clone()
67         {
68             return new ExtensionValue<T>(codec)
69             {
70                 field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field
71             };
72         }
73 
Equals(IExtensionValue other)74         public bool Equals(IExtensionValue other)
75         {
76             if (ReferenceEquals(this, other))
77                 return true;
78 
79             return other is ExtensionValue<T>
80                 && codec.Equals((other as ExtensionValue<T>).codec)
81                 && Equals(field, (other as ExtensionValue<T>).field);
82             // we check for equality in the codec since we could have equal field values however the values could be written in different ways
83         }
84 
GetHashCode()85         public override int GetHashCode()
86         {
87             unchecked
88             {
89                 int hash = 17;
90                 hash = hash * 31 + field.GetHashCode();
91                 hash = hash * 31 + codec.GetHashCode();
92                 return hash;
93             }
94         }
95 
MergeFrom(ref ParseContext ctx)96         public void MergeFrom(ref ParseContext ctx)
97         {
98             codec.ValueMerger(ref ctx, ref field);
99         }
100 
MergeFrom(IExtensionValue value)101         public void MergeFrom(IExtensionValue value)
102         {
103             if (value is ExtensionValue<T>)
104             {
105                 var extensionValue = value as ExtensionValue<T>;
106                 codec.FieldMerger(ref field, extensionValue.field);
107             }
108         }
109 
WriteTo(ref WriteContext ctx)110         public void WriteTo(ref WriteContext ctx)
111         {
112             ctx.WriteTag(codec.Tag);
113             codec.ValueWriter(ref ctx, field);
114             if (codec.EndTag != 0)
115             {
116                 ctx.WriteTag(codec.EndTag);
117             }
118         }
119 
GetValue()120         public T GetValue() => field;
121 
IExtensionValue.GetValue()122         object IExtensionValue.GetValue() => field;
123 
SetValue(T value)124         public void SetValue(T value)
125         {
126             field = value;
127         }
128 
IsInitialized()129         public bool IsInitialized()
130         {
131             if (field is IMessage)
132             {
133                 return (field as IMessage).IsInitialized();
134             }
135             else
136             {
137                 return true;
138             }
139         }
140     }
141 
142     internal sealed class RepeatedExtensionValue<T> : IExtensionValue
143     {
144         private RepeatedField<T> field;
145         private readonly FieldCodec<T> codec;
146 
RepeatedExtensionValue(FieldCodec<T> codec)147         internal RepeatedExtensionValue(FieldCodec<T> codec)
148         {
149             this.codec = codec;
150             field = new RepeatedField<T>();
151         }
152 
CalculateSize()153         public int CalculateSize()
154         {
155             return field.CalculateSize(codec);
156         }
157 
Clone()158         public IExtensionValue Clone()
159         {
160             return new RepeatedExtensionValue<T>(codec)
161             {
162                 field = field.Clone()
163             };
164         }
165 
Equals(IExtensionValue other)166         public bool Equals(IExtensionValue other)
167         {
168             if (ReferenceEquals(this, other))
169                 return true;
170 
171             return other is RepeatedExtensionValue<T>
172                 && field.Equals((other as RepeatedExtensionValue<T>).field)
173                 && codec.Equals((other as RepeatedExtensionValue<T>).codec);
174         }
175 
GetHashCode()176         public override int GetHashCode()
177         {
178             unchecked
179             {
180                 int hash = 17;
181                 hash = hash * 31 + field.GetHashCode();
182                 hash = hash * 31 + codec.GetHashCode();
183                 return hash;
184             }
185         }
186 
MergeFrom(ref ParseContext ctx)187         public void MergeFrom(ref ParseContext ctx)
188         {
189             field.AddEntriesFrom(ref ctx, codec);
190         }
191 
MergeFrom(IExtensionValue value)192         public void MergeFrom(IExtensionValue value)
193         {
194             if (value is RepeatedExtensionValue<T>)
195             {
196                 field.Add((value as RepeatedExtensionValue<T>).field);
197             }
198         }
199 
WriteTo(ref WriteContext ctx)200         public void WriteTo(ref WriteContext ctx)
201         {
202             field.WriteTo(ref ctx, codec);
203         }
204 
GetValue()205         public RepeatedField<T> GetValue() => field;
206 
IExtensionValue.GetValue()207         object IExtensionValue.GetValue() => field;
208 
IsInitialized()209         public bool IsInitialized()
210         {
211             for (int i = 0; i < field.Count; i++)
212             {
213                 var element = field[i];
214                 if (element is IMessage)
215                 {
216                     if (!(element as IMessage).IsInitialized())
217                     {
218                         return false;
219                     }
220                 }
221                 else
222                 {
223                     break;
224                 }
225             }
226 
227             return true;
228         }
229     }
230 }
231