| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Runtime.InteropServices; |
| | | 5 | | |
| | | 6 | | namespace ZeroC.Slice.Codec; |
| | | 7 | | |
| | | 8 | | /// <summary>Provides extension methods for <see cref="SliceDecoder" /> to decode dictionaries, results, and sequences. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class SliceDecoderExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary>Verifies the Slice decoder has reached the end of its underlying buffer.</summary> |
| | | 13 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 14 | | public static void CheckEndOfBuffer(this ref SliceDecoder decoder) |
| | 7349 | 15 | | { |
| | 7349 | 16 | | if (!decoder.End) |
| | 4 | 17 | | { |
| | 4 | 18 | | throw new InvalidDataException($"There are {decoder.Remaining} bytes remaining in the buffer."); |
| | | 19 | | } |
| | 7345 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary>Decodes a dictionary.</summary> |
| | | 23 | | /// <typeparam name="TDictionary">The type of the returned dictionary.</typeparam> |
| | | 24 | | /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam> |
| | | 25 | | /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam> |
| | | 26 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 27 | | /// <param name="dictionaryFactory">The factory for creating the dictionary instance.</param> |
| | | 28 | | /// <param name="keyDecodeFunc">The decode function for each key of the dictionary.</param> |
| | | 29 | | /// <param name="valueDecodeFunc">The decode function for each value of the dictionary.</param> |
| | | 30 | | /// <returns>The dictionary decoded by this decoder.</returns> |
| | | 31 | | public static TDictionary DecodeDictionary<TDictionary, TKey, TValue>( |
| | | 32 | | this ref SliceDecoder decoder, |
| | | 33 | | Func<int, TDictionary> dictionaryFactory, |
| | | 34 | | DecodeFunc<TKey> keyDecodeFunc, |
| | | 35 | | DecodeFunc<TValue> valueDecodeFunc) |
| | | 36 | | where TKey : notnull |
| | | 37 | | where TDictionary : IDictionary<TKey, TValue> |
| | 983 | 38 | | { |
| | 983 | 39 | | int count = decoder.DecodeSize(); |
| | 981 | 40 | | if (count == 0) |
| | 309 | 41 | | { |
| | 309 | 42 | | return dictionaryFactory(0); |
| | | 43 | | } |
| | | 44 | | else |
| | 672 | 45 | | { |
| | 672 | 46 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<TKey>() + Unsafe.SizeOf<TValue>()); |
| | 667 | 47 | | TDictionary dictionary = dictionaryFactory(count); |
| | 9918 | 48 | | for (int i = 0; i < count; ++i) |
| | 4293 | 49 | | { |
| | 4293 | 50 | | TKey key = keyDecodeFunc(ref decoder); |
| | 4293 | 51 | | TValue value = valueDecodeFunc(ref decoder); |
| | | 52 | | try |
| | 4293 | 53 | | { |
| | 4293 | 54 | | dictionary.Add(key, value); |
| | 4292 | 55 | | } |
| | 1 | 56 | | catch (ArgumentException exception) |
| | 1 | 57 | | { |
| | 1 | 58 | | throw new InvalidDataException($"Received dictionary with duplicate key '{key}'.", exception); |
| | | 59 | | } |
| | 4292 | 60 | | } |
| | 666 | 61 | | return dictionary; |
| | | 62 | | } |
| | 975 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Decodes a dictionary with an optional value type (T? in Slice).</summary> |
| | | 66 | | /// <typeparam name="TDictionary">The type of the returned dictionary.</typeparam> |
| | | 67 | | /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam> |
| | | 68 | | /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam> |
| | | 69 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 70 | | /// <param name="dictionaryFactory">The factory for creating the dictionary instance.</param> |
| | | 71 | | /// <param name="keyDecodeFunc">The decode function for each key of the dictionary.</param> |
| | | 72 | | /// <param name="valueDecodeFunc">The decode function for each non-null value of the dictionary.</param> |
| | | 73 | | /// <returns>The dictionary decoded by this decoder.</returns> |
| | | 74 | | public static TDictionary DecodeDictionaryWithOptionalValueType<TDictionary, TKey, TValue>( |
| | | 75 | | this ref SliceDecoder decoder, |
| | | 76 | | Func<int, TDictionary> dictionaryFactory, |
| | | 77 | | DecodeFunc<TKey> keyDecodeFunc, |
| | | 78 | | DecodeFunc<TValue?> valueDecodeFunc) |
| | | 79 | | where TKey : notnull |
| | | 80 | | where TDictionary : IDictionary<TKey, TValue?> |
| | 17 | 81 | | { |
| | 17 | 82 | | int count = decoder.DecodeSize(); |
| | 17 | 83 | | if (count == 0) |
| | 0 | 84 | | { |
| | 0 | 85 | | return dictionaryFactory(0); |
| | | 86 | | } |
| | | 87 | | else |
| | 17 | 88 | | { |
| | 17 | 89 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<TKey>() + Unsafe.SizeOf<TValue?>()); |
| | 16 | 90 | | TDictionary dictionary = dictionaryFactory(count); |
| | 4208 | 91 | | for (int i = 0; i < count; ++i) |
| | 2088 | 92 | | { |
| | | 93 | | // Each entry is encoded like a: |
| | | 94 | | // compact struct Pair |
| | | 95 | | // { |
| | | 96 | | // key: Key, |
| | | 97 | | // value: Value? |
| | | 98 | | // } |
| | 2088 | 99 | | bool hasValue = decoder.DecodeBool(); // simplified bit sequence |
| | 2088 | 100 | | TKey key = keyDecodeFunc(ref decoder); |
| | 2088 | 101 | | TValue? value = hasValue ? valueDecodeFunc(ref decoder) : default; |
| | | 102 | | try |
| | 2088 | 103 | | { |
| | 2088 | 104 | | dictionary.Add(key, value); |
| | 2088 | 105 | | } |
| | 0 | 106 | | catch (ArgumentException exception) |
| | 0 | 107 | | { |
| | 0 | 108 | | throw new InvalidDataException($"Received dictionary with duplicate key '{key}'.", exception); |
| | | 109 | | } |
| | 2088 | 110 | | } |
| | 16 | 111 | | return dictionary; |
| | | 112 | | } |
| | 16 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary>Decodes a result.</summary> |
| | | 116 | | /// <typeparam name="TSuccess">The type of the success value.</typeparam> |
| | | 117 | | /// <typeparam name="TFailure">The type of the failure value.</typeparam> |
| | | 118 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 119 | | /// <param name="successDecodeFunc">The decode function for the success type.</param> |
| | | 120 | | /// <param name="failureDecodeFunc">The decode function for the failure type.</param> |
| | | 121 | | /// <returns>The decoded result.</returns> |
| | | 122 | | public static Result<TSuccess, TFailure> DecodeResult<TSuccess, TFailure>( |
| | | 123 | | this ref SliceDecoder decoder, |
| | | 124 | | DecodeFunc<TSuccess> successDecodeFunc, |
| | | 125 | | DecodeFunc<TFailure> failureDecodeFunc) => |
| | 8 | 126 | | decoder.DecodeVarInt32() switch |
| | 8 | 127 | | { |
| | 3 | 128 | | 0 => new Result<TSuccess, TFailure>.Success(successDecodeFunc(ref decoder)), |
| | 5 | 129 | | 1 => new Result<TSuccess, TFailure>.Failure(failureDecodeFunc(ref decoder)), |
| | 0 | 130 | | int value => throw new InvalidDataException($"Received invalid discriminant value '{value}' for Result.") |
| | 8 | 131 | | }; |
| | | 132 | | |
| | | 133 | | /// <summary>Decodes a sequence of fixed-size numeric values.</summary> |
| | | 134 | | /// <typeparam name="T">The sequence element type.</typeparam> |
| | | 135 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 136 | | /// <param name="checkElement">A delegate used to check each element of the array (optional).</param> |
| | | 137 | | /// <returns>An array of T.</returns> |
| | | 138 | | public static T[] DecodeSequence<T>(this ref SliceDecoder decoder, Action<T>? checkElement = null) |
| | | 139 | | where T : struct |
| | 3066 | 140 | | { |
| | 3066 | 141 | | int count = decoder.DecodeSize(); |
| | 3066 | 142 | | if (count == 0) |
| | 0 | 143 | | { |
| | 0 | 144 | | return Array.Empty<T>(); |
| | | 145 | | } |
| | | 146 | | else |
| | 3066 | 147 | | { |
| | 3066 | 148 | | int elementSize = Unsafe.SizeOf<T>(); |
| | 3066 | 149 | | decoder.IncreaseCollectionAllocation(count, elementSize); |
| | 3066 | 150 | | var value = new T[count]; |
| | 3066 | 151 | | Span<byte> destination = MemoryMarshal.Cast<T, byte>(value.AsSpan()); |
| | 3066 | 152 | | decoder.CopyTo(destination); |
| | | 153 | | |
| | 3066 | 154 | | if (checkElement is not null) |
| | 3 | 155 | | { |
| | 28 | 156 | | foreach (T e in value) |
| | 10 | 157 | | { |
| | 10 | 158 | | checkElement(e); |
| | 9 | 159 | | } |
| | 2 | 160 | | } |
| | 3065 | 161 | | return value; |
| | | 162 | | } |
| | 3065 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary>Decodes a sequence.</summary> |
| | | 166 | | /// <typeparam name="T">The type of the elements in the array.</typeparam> |
| | | 167 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 168 | | /// <param name="decodeFunc">The decode function for each element of the sequence.</param> |
| | | 169 | | /// <returns>An array of T.</returns> |
| | | 170 | | public static T[] DecodeSequence<T>(this ref SliceDecoder decoder, DecodeFunc<T> decodeFunc) where T : notnull |
| | 32 | 171 | | { |
| | 32 | 172 | | int count = decoder.DecodeSize(); |
| | 31 | 173 | | if (count == 0) |
| | 0 | 174 | | { |
| | 0 | 175 | | return Array.Empty<T>(); |
| | | 176 | | } |
| | | 177 | | else |
| | 31 | 178 | | { |
| | 31 | 179 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<T>()); |
| | 27 | 180 | | var array = new T[count]; |
| | 1500 | 181 | | for (int i = 0; i < count; ++i) |
| | 723 | 182 | | { |
| | 723 | 183 | | array[i] = decodeFunc(ref decoder); |
| | 723 | 184 | | } |
| | 27 | 185 | | return array; |
| | | 186 | | } |
| | 27 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary>Decodes a sequence.</summary> |
| | | 190 | | /// <typeparam name="TSequence">The type of the returned sequence.</typeparam> |
| | | 191 | | /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam> |
| | | 192 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 193 | | /// <param name="sequenceFactory">The factory for creating the sequence instance.</param> |
| | | 194 | | /// <param name="decodeFunc">The decode function for each element of the sequence.</param> |
| | | 195 | | /// <returns>A TSequence.</returns> |
| | | 196 | | public static TSequence DecodeSequence<TSequence, TElement>( |
| | | 197 | | this ref SliceDecoder decoder, |
| | | 198 | | Func<int, TSequence> sequenceFactory, |
| | | 199 | | DecodeFunc<TElement> decodeFunc) where TSequence : ICollection<TElement> |
| | | 200 | | where TElement : notnull |
| | 8 | 201 | | { |
| | 8 | 202 | | int count = decoder.DecodeSize(); |
| | 8 | 203 | | if (count == 0) |
| | 0 | 204 | | { |
| | 0 | 205 | | return sequenceFactory(0); |
| | | 206 | | } |
| | | 207 | | else |
| | 8 | 208 | | { |
| | 8 | 209 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<TElement>()); |
| | 8 | 210 | | TSequence sequence = sequenceFactory(count); |
| | 64 | 211 | | for (int i = 0; i < count; ++i) |
| | 24 | 212 | | { |
| | 24 | 213 | | sequence.Add(decodeFunc(ref decoder)); |
| | 24 | 214 | | } |
| | 8 | 215 | | return sequence; |
| | | 216 | | } |
| | 8 | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary>Decodes a sequence where the element type is an optional Slice type (T?).</summary> |
| | | 220 | | /// <typeparam name="T">The type of the elements in the array.</typeparam> |
| | | 221 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 222 | | /// <param name="decodeFunc">The decode function for each non-null element of the sequence.</param> |
| | | 223 | | /// <returns>An array of T.</returns> |
| | | 224 | | /// <remarks>We return a T? and not a T to avoid ambiguities in the generated code with nullable reference |
| | | 225 | | /// types such as string?.</remarks> |
| | | 226 | | public static T?[] DecodeSequenceOfOptionals<T>(this ref SliceDecoder decoder, DecodeFunc<T> decodeFunc) |
| | 23 | 227 | | { |
| | 23 | 228 | | int count = decoder.DecodeSize(); |
| | 23 | 229 | | if (count == 0) |
| | 0 | 230 | | { |
| | 0 | 231 | | return Array.Empty<T>(); |
| | | 232 | | } |
| | | 233 | | else |
| | 23 | 234 | | { |
| | 23 | 235 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<T?>()); |
| | 19 | 236 | | BitSequenceReader bitSequenceReader = decoder.GetBitSequenceReader(count); |
| | 18 | 237 | | var array = new T?[count]; |
| | 2488 | 238 | | for (int i = 0; i < count; ++i) |
| | 1226 | 239 | | { |
| | 1226 | 240 | | array[i] = bitSequenceReader.Read() ? decodeFunc(ref decoder) : default; |
| | 1226 | 241 | | } |
| | 18 | 242 | | return array; |
| | | 243 | | } |
| | 18 | 244 | | } |
| | | 245 | | |
| | | 246 | | /// <summary>Decodes a sequence where the element type is an optional Slice type (T?).</summary> |
| | | 247 | | /// <typeparam name="TSequence">The type of the returned sequence.</typeparam> |
| | | 248 | | /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam> |
| | | 249 | | /// <param name="decoder">The Slice decoder.</param> |
| | | 250 | | /// <param name="sequenceFactory">The factory for creating the sequence instance.</param> |
| | | 251 | | /// <param name="decodeFunc">The decode function for each non-null element of the sequence.</param> |
| | | 252 | | /// <returns>A TSequence.</returns> |
| | | 253 | | public static TSequence DecodeSequenceOfOptionals<TSequence, TElement>( |
| | | 254 | | this ref SliceDecoder decoder, |
| | | 255 | | Func<int, TSequence> sequenceFactory, |
| | | 256 | | DecodeFunc<TElement> decodeFunc) where TSequence : ICollection<TElement> |
| | 1 | 257 | | { |
| | 1 | 258 | | int count = decoder.DecodeSize(); |
| | 1 | 259 | | if (count == 0) |
| | 0 | 260 | | { |
| | 0 | 261 | | return sequenceFactory(0); |
| | | 262 | | } |
| | | 263 | | else |
| | 1 | 264 | | { |
| | 1 | 265 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<TElement>()); |
| | 1 | 266 | | BitSequenceReader bitSequenceReader = decoder.GetBitSequenceReader(count); |
| | 1 | 267 | | TSequence sequence = sequenceFactory(count); |
| | 8 | 268 | | for (int i = 0; i < count; ++i) |
| | 3 | 269 | | { |
| | 3 | 270 | | sequence.Add(bitSequenceReader.Read() ? decodeFunc(ref decoder) : default!); |
| | 3 | 271 | | } |
| | 1 | 272 | | return sequence; |
| | | 273 | | } |
| | 1 | 274 | | } |
| | | 275 | | } |