Average.Generated.tt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. var types = new[]
  12. {
  13. new { source = typeof(int), target = typeof(double) },
  14. new { source = typeof(long), target = typeof(double) },
  15. new { source = typeof(float), target = typeof(float) },
  16. new { source = typeof(double), target = typeof(double) },
  17. new { source = typeof(decimal), target = typeof(decimal) },
  18. };
  19. var name = "Average";
  20. #>
  21. using System.Threading.Tasks;
  22. namespace System.Reactive.Linq
  23. {
  24. public partial class AsyncObservable
  25. {
  26. <#
  27. foreach (var t in types)
  28. {
  29. var sourceType = t.source;
  30. var targetType = t.target;
  31. foreach (var n in new[] { false, true })
  32. {
  33. var sourceTypeName = n ? sourceType.Name + "?" : sourceType.Name;
  34. var targetTypeName = n ? targetType.Name + "?" : targetType.Name;
  35. var methodName = n ? "Nullable" + sourceType.Name : sourceType.Name;
  36. #>
  37. public static IAsyncObservable<<#=targetTypeName#>> <#=name#>(this IAsyncObservable<<#=sourceTypeName#>> source)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. return Create<<#=sourceTypeName#>, <#=targetTypeName#>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.<#=name#><#=methodName#>(observer)));
  42. }
  43. public static IAsyncObservable<<#=targetTypeName#>> <#=name#><TSource>(this IAsyncObservable<TSource> source, Func<TSource, <#=sourceTypeName#>> selector)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (selector == null)
  48. throw new ArgumentNullException(nameof(selector));
  49. return CreateAsyncObservable<<#=targetTypeName#>>.From(
  50. source,
  51. selector,
  52. static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.<#=name#><#=methodName#>(observer, selector)));
  53. }
  54. public static IAsyncObservable<<#=targetTypeName#>> <#=name#><TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<<#=sourceTypeName#>>> selector)
  55. {
  56. if (source == null)
  57. throw new ArgumentNullException(nameof(source));
  58. if (selector == null)
  59. throw new ArgumentNullException(nameof(selector));
  60. return CreateAsyncObservable<<#=targetTypeName#>>.From(
  61. source,
  62. selector,
  63. static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.<#=name#><#=methodName#>(observer, selector)));
  64. }
  65. <#
  66. }
  67. }
  68. #>
  69. }
  70. public partial class AsyncObserver
  71. {
  72. <#
  73. foreach (var t in types)
  74. {
  75. var sourceType = t.source;
  76. var targetType = t.target;
  77. foreach (var n in new[] { false, true })
  78. {
  79. var sourceTypeName = n ? sourceType.Name + "?" : sourceType.Name;
  80. var targetTypeName = n ? targetType.Name + "?" : targetType.Name;
  81. var methodName = n ? "Nullable" + sourceType.Name : sourceType.Name;
  82. #>
  83. public static IAsyncObserver<TSource> <#=name#><#=methodName#><TSource>(IAsyncObserver<<#=targetTypeName#>> observer, Func<TSource, <#=sourceTypeName#>> selector)
  84. {
  85. if (observer == null)
  86. throw new ArgumentNullException(nameof(observer));
  87. if (selector == null)
  88. throw new ArgumentNullException(nameof(selector));
  89. return Select(<#=name#><#=methodName#>(observer), selector);
  90. }
  91. public static IAsyncObserver<TSource> <#=name#><#=methodName#><TSource>(IAsyncObserver<<#=targetTypeName#>> observer, Func<TSource, ValueTask<<#=sourceTypeName#>>> selector)
  92. {
  93. if (observer == null)
  94. throw new ArgumentNullException(nameof(observer));
  95. if (selector == null)
  96. throw new ArgumentNullException(nameof(selector));
  97. return Select(<#=name#><#=methodName#>(observer), selector);
  98. }
  99. <#
  100. }
  101. }
  102. #>
  103. }
  104. }