1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing Google.Protobuf.Collections;
34ffe3c632Sopenharmony_ciusing System;
35ffe3c632Sopenharmony_ciusing System.Linq;
36ffe3c632Sopenharmony_ci
37ffe3c632Sopenharmony_cinamespace Google.Protobuf
38ffe3c632Sopenharmony_ci{
39ffe3c632Sopenharmony_ci    internal interface IExtensionValue : IEquatable<IExtensionValue>, IDeepCloneable<IExtensionValue>
40ffe3c632Sopenharmony_ci    {
41ffe3c632Sopenharmony_ci        void MergeFrom(ref ParseContext ctx);
42ffe3c632Sopenharmony_ci
43ffe3c632Sopenharmony_ci        void MergeFrom(IExtensionValue value);
44ffe3c632Sopenharmony_ci        void WriteTo(ref WriteContext ctx);
45ffe3c632Sopenharmony_ci        int CalculateSize();
46ffe3c632Sopenharmony_ci        bool IsInitialized();
47ffe3c632Sopenharmony_ci    }
48ffe3c632Sopenharmony_ci
49ffe3c632Sopenharmony_ci    internal sealed class ExtensionValue<T> : IExtensionValue
50ffe3c632Sopenharmony_ci    {
51ffe3c632Sopenharmony_ci        private T field;
52ffe3c632Sopenharmony_ci        private FieldCodec<T> codec;
53ffe3c632Sopenharmony_ci
54ffe3c632Sopenharmony_ci        internal ExtensionValue(FieldCodec<T> codec)
55ffe3c632Sopenharmony_ci        {
56ffe3c632Sopenharmony_ci            this.codec = codec;
57ffe3c632Sopenharmony_ci            field = codec.DefaultValue;
58ffe3c632Sopenharmony_ci        }
59ffe3c632Sopenharmony_ci
60ffe3c632Sopenharmony_ci        public int CalculateSize()
61ffe3c632Sopenharmony_ci        {
62ffe3c632Sopenharmony_ci            return codec.CalculateSizeWithTag(field);
63ffe3c632Sopenharmony_ci        }
64ffe3c632Sopenharmony_ci
65ffe3c632Sopenharmony_ci        public IExtensionValue Clone()
66ffe3c632Sopenharmony_ci        {
67ffe3c632Sopenharmony_ci            return new ExtensionValue<T>(codec)
68ffe3c632Sopenharmony_ci            {
69ffe3c632Sopenharmony_ci                field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field
70ffe3c632Sopenharmony_ci            };
71ffe3c632Sopenharmony_ci        }
72ffe3c632Sopenharmony_ci
73ffe3c632Sopenharmony_ci        public bool Equals(IExtensionValue other)
74ffe3c632Sopenharmony_ci        {
75ffe3c632Sopenharmony_ci            if (ReferenceEquals(this, other))
76ffe3c632Sopenharmony_ci                return true;
77ffe3c632Sopenharmony_ci
78ffe3c632Sopenharmony_ci            return other is ExtensionValue<T>
79ffe3c632Sopenharmony_ci                && codec.Equals((other as ExtensionValue<T>).codec)
80ffe3c632Sopenharmony_ci                && Equals(field, (other as ExtensionValue<T>).field);
81ffe3c632Sopenharmony_ci            // we check for equality in the codec since we could have equal field values however the values could be written in different ways
82ffe3c632Sopenharmony_ci        }
83ffe3c632Sopenharmony_ci
84ffe3c632Sopenharmony_ci        public override int GetHashCode()
85ffe3c632Sopenharmony_ci        {
86ffe3c632Sopenharmony_ci            unchecked
87ffe3c632Sopenharmony_ci            {
88ffe3c632Sopenharmony_ci                int hash = 17;
89ffe3c632Sopenharmony_ci                hash = hash * 31 + field.GetHashCode();
90ffe3c632Sopenharmony_ci                hash = hash * 31 + codec.GetHashCode();
91ffe3c632Sopenharmony_ci                return hash;
92ffe3c632Sopenharmony_ci            }
93ffe3c632Sopenharmony_ci        }
94ffe3c632Sopenharmony_ci
95ffe3c632Sopenharmony_ci        public void MergeFrom(ref ParseContext ctx)
96ffe3c632Sopenharmony_ci        {
97ffe3c632Sopenharmony_ci            codec.ValueMerger(ref ctx, ref field);
98ffe3c632Sopenharmony_ci        }
99ffe3c632Sopenharmony_ci
100ffe3c632Sopenharmony_ci        public void MergeFrom(IExtensionValue value)
101ffe3c632Sopenharmony_ci        {
102ffe3c632Sopenharmony_ci            if (value is ExtensionValue<T>)
103ffe3c632Sopenharmony_ci            {
104ffe3c632Sopenharmony_ci                var extensionValue = value as ExtensionValue<T>;
105ffe3c632Sopenharmony_ci                codec.FieldMerger(ref field, extensionValue.field);
106ffe3c632Sopenharmony_ci            }
107ffe3c632Sopenharmony_ci        }
108ffe3c632Sopenharmony_ci
109ffe3c632Sopenharmony_ci        public void WriteTo(ref WriteContext ctx)
110ffe3c632Sopenharmony_ci        {
111ffe3c632Sopenharmony_ci            ctx.WriteTag(codec.Tag);
112ffe3c632Sopenharmony_ci            codec.ValueWriter(ref ctx, field);
113ffe3c632Sopenharmony_ci            if (codec.EndTag != 0)
114ffe3c632Sopenharmony_ci            {
115ffe3c632Sopenharmony_ci                ctx.WriteTag(codec.EndTag);
116ffe3c632Sopenharmony_ci            }
117ffe3c632Sopenharmony_ci        }
118ffe3c632Sopenharmony_ci
119ffe3c632Sopenharmony_ci        public T GetValue() => field;
120ffe3c632Sopenharmony_ci
121ffe3c632Sopenharmony_ci        public void SetValue(T value)
122ffe3c632Sopenharmony_ci        {
123ffe3c632Sopenharmony_ci            field = value;
124ffe3c632Sopenharmony_ci        }
125ffe3c632Sopenharmony_ci
126ffe3c632Sopenharmony_ci        public bool IsInitialized()
127ffe3c632Sopenharmony_ci        {
128ffe3c632Sopenharmony_ci            if (field is IMessage)
129ffe3c632Sopenharmony_ci            {
130ffe3c632Sopenharmony_ci                return (field as IMessage).IsInitialized();
131ffe3c632Sopenharmony_ci            }
132ffe3c632Sopenharmony_ci            else
133ffe3c632Sopenharmony_ci            {
134ffe3c632Sopenharmony_ci                return true;
135ffe3c632Sopenharmony_ci            }
136ffe3c632Sopenharmony_ci        }
137ffe3c632Sopenharmony_ci    }
138ffe3c632Sopenharmony_ci
139ffe3c632Sopenharmony_ci    internal sealed class RepeatedExtensionValue<T> : IExtensionValue
140ffe3c632Sopenharmony_ci    {
141ffe3c632Sopenharmony_ci        private RepeatedField<T> field;
142ffe3c632Sopenharmony_ci        private readonly FieldCodec<T> codec;
143ffe3c632Sopenharmony_ci
144ffe3c632Sopenharmony_ci        internal RepeatedExtensionValue(FieldCodec<T> codec)
145ffe3c632Sopenharmony_ci        {
146ffe3c632Sopenharmony_ci            this.codec = codec;
147ffe3c632Sopenharmony_ci            field = new RepeatedField<T>();
148ffe3c632Sopenharmony_ci        }
149ffe3c632Sopenharmony_ci
150ffe3c632Sopenharmony_ci        public int CalculateSize()
151ffe3c632Sopenharmony_ci        {
152ffe3c632Sopenharmony_ci            return field.CalculateSize(codec);
153ffe3c632Sopenharmony_ci        }
154ffe3c632Sopenharmony_ci
155ffe3c632Sopenharmony_ci        public IExtensionValue Clone()
156ffe3c632Sopenharmony_ci        {
157ffe3c632Sopenharmony_ci            return new RepeatedExtensionValue<T>(codec)
158ffe3c632Sopenharmony_ci            {
159ffe3c632Sopenharmony_ci                field = field.Clone()
160ffe3c632Sopenharmony_ci            };
161ffe3c632Sopenharmony_ci        }
162ffe3c632Sopenharmony_ci
163ffe3c632Sopenharmony_ci        public bool Equals(IExtensionValue other)
164ffe3c632Sopenharmony_ci        {
165ffe3c632Sopenharmony_ci            if (ReferenceEquals(this, other))
166ffe3c632Sopenharmony_ci                return true;
167ffe3c632Sopenharmony_ci
168ffe3c632Sopenharmony_ci            return other is RepeatedExtensionValue<T>
169ffe3c632Sopenharmony_ci                && field.Equals((other as RepeatedExtensionValue<T>).field)
170ffe3c632Sopenharmony_ci                && codec.Equals((other as RepeatedExtensionValue<T>).codec);
171ffe3c632Sopenharmony_ci        }
172ffe3c632Sopenharmony_ci
173ffe3c632Sopenharmony_ci        public override int GetHashCode()
174ffe3c632Sopenharmony_ci        {
175ffe3c632Sopenharmony_ci            unchecked
176ffe3c632Sopenharmony_ci            {
177ffe3c632Sopenharmony_ci                int hash = 17;
178ffe3c632Sopenharmony_ci                hash = hash * 31 + field.GetHashCode();
179ffe3c632Sopenharmony_ci                hash = hash * 31 + codec.GetHashCode();
180ffe3c632Sopenharmony_ci                return hash;
181ffe3c632Sopenharmony_ci            }
182ffe3c632Sopenharmony_ci        }
183ffe3c632Sopenharmony_ci
184ffe3c632Sopenharmony_ci        public void MergeFrom(ref ParseContext ctx)
185ffe3c632Sopenharmony_ci        {
186ffe3c632Sopenharmony_ci            field.AddEntriesFrom(ref ctx, codec);
187ffe3c632Sopenharmony_ci        }
188ffe3c632Sopenharmony_ci
189ffe3c632Sopenharmony_ci        public void MergeFrom(IExtensionValue value)
190ffe3c632Sopenharmony_ci        {
191ffe3c632Sopenharmony_ci            if (value is RepeatedExtensionValue<T>)
192ffe3c632Sopenharmony_ci            {
193ffe3c632Sopenharmony_ci                field.Add((value as RepeatedExtensionValue<T>).field);
194ffe3c632Sopenharmony_ci            }
195ffe3c632Sopenharmony_ci        }
196ffe3c632Sopenharmony_ci
197ffe3c632Sopenharmony_ci        public void WriteTo(ref WriteContext ctx)
198ffe3c632Sopenharmony_ci        {
199ffe3c632Sopenharmony_ci            field.WriteTo(ref ctx, codec);
200ffe3c632Sopenharmony_ci        }
201ffe3c632Sopenharmony_ci
202ffe3c632Sopenharmony_ci        public RepeatedField<T> GetValue() => field;
203ffe3c632Sopenharmony_ci
204ffe3c632Sopenharmony_ci        public bool IsInitialized()
205ffe3c632Sopenharmony_ci        {
206ffe3c632Sopenharmony_ci            for (int i = 0; i < field.Count; i++)
207ffe3c632Sopenharmony_ci            {
208ffe3c632Sopenharmony_ci                var element = field[i];
209ffe3c632Sopenharmony_ci                if (element is IMessage)
210ffe3c632Sopenharmony_ci                {
211ffe3c632Sopenharmony_ci                    if (!(element as IMessage).IsInitialized())
212ffe3c632Sopenharmony_ci                    {
213ffe3c632Sopenharmony_ci                        return false;
214ffe3c632Sopenharmony_ci                    }
215ffe3c632Sopenharmony_ci                }
216ffe3c632Sopenharmony_ci                else
217ffe3c632Sopenharmony_ci                {
218ffe3c632Sopenharmony_ci                    break;
219ffe3c632Sopenharmony_ci                }
220ffe3c632Sopenharmony_ci            }
221ffe3c632Sopenharmony_ci
222ffe3c632Sopenharmony_ci            return true;
223ffe3c632Sopenharmony_ci        }
224ffe3c632Sopenharmony_ci    }
225ffe3c632Sopenharmony_ci}
226