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