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 36 namespace Google.Protobuf 37 { 38 internal interface IExtensionValue : IEquatable<IExtensionValue>, IDeepCloneable<IExtensionValue> 39 { MergeFrom(CodedInputStream input)40 void MergeFrom(CodedInputStream input); MergeFrom(IExtensionValue value)41 void MergeFrom(IExtensionValue value); WriteTo(CodedOutputStream output)42 void WriteTo(CodedOutputStream output); CalculateSize()43 int CalculateSize(); 44 } 45 46 internal sealed class ExtensionValue<T> : IExtensionValue 47 { 48 private bool hasValue; 49 private T field; 50 private FieldCodec<T> codec; 51 ExtensionValue(FieldCodec<T> codec)52 internal ExtensionValue(FieldCodec<T> codec) 53 { 54 this.codec = codec; 55 field = codec.DefaultValue; 56 } 57 CalculateSize()58 public int CalculateSize() 59 { 60 if (!hasValue) 61 { 62 return 0; 63 } 64 return codec.CalculateSizeWithTag(field); 65 } 66 Clone()67 public IExtensionValue Clone() 68 { 69 return new ExtensionValue<T>(codec) 70 { 71 hasValue = hasValue, 72 field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field 73 }; 74 } 75 Equals(IExtensionValue other)76 public bool Equals(IExtensionValue other) 77 { 78 if (ReferenceEquals(this, other)) 79 return true; 80 81 return other is ExtensionValue<T> 82 && codec.Equals((other as ExtensionValue<T>).codec) 83 && hasValue.Equals((other as ExtensionValue<T>).hasValue) 84 && Equals(field, (other as ExtensionValue<T>).field); 85 // we check for equality in the codec since we could have equal field values however the values could be written in different ways 86 } 87 GetHashCode()88 public override int GetHashCode() 89 { 90 unchecked 91 { 92 int hash = 17; 93 hash = hash * 31 + hasValue.GetHashCode(); 94 hash = hash * 31 + field.GetHashCode(); 95 hash = hash * 31 + codec.GetHashCode(); 96 return hash; 97 } 98 } 99 MergeFrom(CodedInputStream input)100 public void MergeFrom(CodedInputStream input) 101 { 102 hasValue = true; 103 codec.ValueMerger(input, ref field); 104 } 105 MergeFrom(IExtensionValue value)106 public void MergeFrom(IExtensionValue value) 107 { 108 if (value is ExtensionValue<T>) 109 { 110 var extensionValue = value as ExtensionValue<T>; 111 if (extensionValue.hasValue) 112 { 113 hasValue |= codec.FieldMerger(ref field, extensionValue.field); 114 } 115 } 116 } 117 WriteTo(CodedOutputStream output)118 public void WriteTo(CodedOutputStream output) 119 { 120 if (hasValue) 121 { 122 output.WriteTag(codec.Tag); 123 codec.ValueWriter(output, field); 124 if (codec.EndTag != 0) 125 { 126 output.WriteTag(codec.EndTag); 127 } 128 } 129 } 130 GetValue()131 public T GetValue() => field; 132 SetValue(T value)133 public void SetValue(T value) 134 { 135 hasValue = true; 136 field = value; 137 } 138 139 public bool HasValue => hasValue; 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(CodedInputStream input)187 public void MergeFrom(CodedInputStream input) 188 { 189 field.AddEntriesFrom(input, 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(CodedOutputStream output)200 public void WriteTo(CodedOutputStream output) 201 { 202 field.WriteTo(output, codec); 203 } 204 GetValue()205 public RepeatedField<T> GetValue() => field; 206 } 207 } 208