Select.Opt.Generated.tt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. <#@ template debug="false" hostspecific="false" language="C#" #>
  5. <#@ assembly name="System.Core" #>
  6. <#@ import namespace="System.Linq" #>
  7. <#@ import namespace="System.Text" #>
  8. <#@ import namespace="System.Collections.Generic" #>
  9. <#@ output extension=".cs" #>
  10. <#
  11. int maxCombine = 4;
  12. #>
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace System.Linq
  16. {
  17. #if REFERENCE_ASSEMBLY
  18. public static partial class AsyncEnumerableDeprecated
  19. #else
  20. public static partial class AsyncEnumerable
  21. #endif
  22. {
  23. <#
  24. for (var i = 2; i <= maxCombine; i++)
  25. {
  26. Func<int, string> getInputType = j => j == 1 ? "TSource" : "TMiddle" + (j - 1);
  27. Func<int, string> getOutputType = j => j == i ? "TResult" : "TMiddle" + j;
  28. Func<int, string> getSelectorType = j => "Func<" + getInputType(j) + ", " + getOutputType(j) + ">";
  29. var types = string.Join(", ", Enumerable.Range(1, i - 1).Select(j => "TMiddle" + j));
  30. var allSelectors = string.Join(", ", Enumerable.Range(1, i).Select(j => getSelectorType(j) + " selector" + j));
  31. var applyAll = Enumerable.Range(1, i).Reverse().Aggregate("", (s, j) => s + "_selector" + j + "(") + "x" + new string(')', i);
  32. #>
  33. private sealed class CombinedSelectors<#=i#><TSource, <#=types#>, TResult> : ICombinedSelectors<TSource, TResult>
  34. {
  35. <#
  36. for (var j = 1; j <= i; j++)
  37. {
  38. var type = getSelectorType(j);
  39. #>
  40. private readonly <#=type#> _selector<#=j#>;
  41. <#
  42. }
  43. #>
  44. public CombinedSelectors<#=i#>(<#=allSelectors#>)
  45. {
  46. <#
  47. for (var j = 1; j <= i; j++)
  48. {
  49. #>
  50. _selector<#=j#> = selector<#=j#>;
  51. <#
  52. }
  53. #>
  54. }
  55. public ICombinedSelectors<TSource, TNewResult> Combine<TNewResult>(Func<TResult, TNewResult> selector) =>
  56. <#
  57. if (i == maxCombine)
  58. {
  59. #>
  60. new CombinedSelectors2<TSource, TResult, TNewResult>(this.Invoke, selector);
  61. <#
  62. }
  63. else
  64. {
  65. #>
  66. new CombinedSelectors<#=i + 1#><TSource, <#=types#>, TResult, TNewResult>(
  67. <#
  68. for (var j = 1; j <= i; j++)
  69. {
  70. #>
  71. _selector<#=j#>,
  72. <#
  73. }
  74. #>
  75. selector
  76. );
  77. <#
  78. }
  79. #>
  80. public TResult Invoke(TSource x) => <#=applyAll#>;
  81. }
  82. <#
  83. }
  84. #>
  85. <#
  86. for (var i = 2; i <= maxCombine; i++)
  87. {
  88. Func<int, string> getInputType = j => j == 1 ? "TSource" : "TMiddle" + (j - 1);
  89. Func<int, string> getOutputType = j => "ValueTask<" + (j == i ? "TResult" : "TMiddle" + j) + ">";
  90. Func<int, string> getSelectorType = j => "Func<" + getInputType(j) + ", " + getOutputType(j) + ">";
  91. var types = string.Join(", ", Enumerable.Range(1, i - 1).Select(j => "TMiddle" + j));
  92. var allSelectors = string.Join(", ", Enumerable.Range(1, i).Select(j => getSelectorType(j) + " selector" + j));
  93. var applyAll = Enumerable.Range(1, i).Reverse().Aggregate("", (s, j) => s + "await _selector" + j + "(") + "x" + string.Join("", Enumerable.Repeat(").ConfigureAwait(false)", i));
  94. #>
  95. private sealed class CombinedAsyncSelectors<#=i#><TSource, <#=types#>, TResult> : ICombinedAsyncSelectors<TSource, TResult>
  96. {
  97. <#
  98. for (var j = 1; j <= i; j++)
  99. {
  100. var type = getSelectorType(j);
  101. #>
  102. private readonly <#=type#> _selector<#=j#>;
  103. <#
  104. }
  105. #>
  106. public CombinedAsyncSelectors<#=i#>(<#=allSelectors#>)
  107. {
  108. <#
  109. for (var j = 1; j <= i; j++)
  110. {
  111. #>
  112. _selector<#=j#> = selector<#=j#>;
  113. <#
  114. }
  115. #>
  116. }
  117. public ICombinedAsyncSelectors<TSource, TNewResult> Combine<TNewResult>(Func<TResult, ValueTask<TNewResult>> selector) =>
  118. <#
  119. if (i == maxCombine)
  120. {
  121. #>
  122. new CombinedAsyncSelectors2<TSource, TResult, TNewResult>(this.Invoke, selector);
  123. <#
  124. }
  125. else
  126. {
  127. #>
  128. new CombinedAsyncSelectors<#=i + 1#><TSource, <#=types#>, TResult, TNewResult>(
  129. <#
  130. for (var j = 1; j <= i; j++)
  131. {
  132. #>
  133. _selector<#=j#>,
  134. <#
  135. }
  136. #>
  137. selector
  138. );
  139. <#
  140. }
  141. #>
  142. public async ValueTask<TResult> Invoke(TSource x) => <#=applyAll#>;
  143. }
  144. <#
  145. }
  146. #>
  147. #if !NO_DEEP_CANCELLATION
  148. <#
  149. for (var i = 2; i <= maxCombine; i++)
  150. {
  151. Func<int, string> getInputType = j => j == 1 ? "TSource" : "TMiddle" + (j - 1);
  152. Func<int, string> getOutputType = j => "ValueTask<" + (j == i ? "TResult" : "TMiddle" + j) + ">";
  153. Func<int, string> getSelectorType = j => "Func<" + getInputType(j) + ", CancellationToken, " + getOutputType(j) + ">";
  154. var types = string.Join(", ", Enumerable.Range(1, i - 1).Select(j => "TMiddle" + j));
  155. var allSelectors = string.Join(", ", Enumerable.Range(1, i).Select(j => getSelectorType(j) + " selector" + j));
  156. var applyAll = Enumerable.Range(1, i).Reverse().Aggregate("", (s, j) => s + "await _selector" + j + "(") + "x" + string.Join("", Enumerable.Repeat(", ct).ConfigureAwait(false)", i));
  157. #>
  158. private sealed class CombinedAsyncSelectorsWithCancellation<#=i#><TSource, <#=types#>, TResult> : ICombinedAsyncSelectorsWithCancellation<TSource, TResult>
  159. {
  160. <#
  161. for (var j = 1; j <= i; j++)
  162. {
  163. var type = getSelectorType(j);
  164. #>
  165. private readonly <#=type#> _selector<#=j#>;
  166. <#
  167. }
  168. #>
  169. public CombinedAsyncSelectorsWithCancellation<#=i#>(<#=allSelectors#>)
  170. {
  171. <#
  172. for (var j = 1; j <= i; j++)
  173. {
  174. #>
  175. _selector<#=j#> = selector<#=j#>;
  176. <#
  177. }
  178. #>
  179. }
  180. public ICombinedAsyncSelectorsWithCancellation<TSource, TNewResult> Combine<TNewResult>(Func<TResult, CancellationToken, ValueTask<TNewResult>> selector) =>
  181. <#
  182. if (i == maxCombine)
  183. {
  184. #>
  185. new CombinedAsyncSelectorsWithCancellation2<TSource, TResult, TNewResult>(this.Invoke, selector);
  186. <#
  187. }
  188. else
  189. {
  190. #>
  191. new CombinedAsyncSelectorsWithCancellation<#=i + 1#><TSource, <#=types#>, TResult, TNewResult>(
  192. <#
  193. for (var j = 1; j <= i; j++)
  194. {
  195. #>
  196. _selector<#=j#>,
  197. <#
  198. }
  199. #>
  200. selector
  201. );
  202. <#
  203. }
  204. #>
  205. public async ValueTask<TResult> Invoke(TSource x, CancellationToken ct) => <#=applyAll#>;
  206. }
  207. <#
  208. }
  209. #>
  210. #endif
  211. }
  212. }