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