Average.Generated.tt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Create(
  50. source,
  51. selector,
  52. default(<#=targetTypeName#>),
  53. (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.<#=name#><#=methodName#>(observer, selector)));
  54. }
  55. public static IAsyncObservable<<#=targetTypeName#>> <#=name#><TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<<#=sourceTypeName#>>> selector)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. if (selector == null)
  60. throw new ArgumentNullException(nameof(selector));
  61. return Create(
  62. source,
  63. selector,
  64. default(<#=targetTypeName#>),
  65. (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.<#=name#><#=methodName#>(observer, selector)));
  66. }
  67. <#
  68. }
  69. }
  70. #>
  71. }
  72. public partial class AsyncObserver
  73. {
  74. <#
  75. foreach (var t in types)
  76. {
  77. var sourceType = t.source;
  78. var targetType = t.target;
  79. foreach (var n in new[] { false, true })
  80. {
  81. var sourceTypeName = n ? sourceType.Name + "?" : sourceType.Name;
  82. var targetTypeName = n ? targetType.Name + "?" : targetType.Name;
  83. var methodName = n ? "Nullable" + sourceType.Name : sourceType.Name;
  84. #>
  85. public static IAsyncObserver<TSource> <#=name#><#=methodName#><TSource>(IAsyncObserver<<#=targetTypeName#>> observer, Func<TSource, <#=sourceTypeName#>> selector)
  86. {
  87. if (observer == null)
  88. throw new ArgumentNullException(nameof(observer));
  89. if (selector == null)
  90. throw new ArgumentNullException(nameof(selector));
  91. return Select(<#=name#><#=methodName#>(observer), selector);
  92. }
  93. public static IAsyncObserver<TSource> <#=name#><#=methodName#><TSource>(IAsyncObserver<<#=targetTypeName#>> observer, Func<TSource, ValueTask<<#=sourceTypeName#>>> selector)
  94. {
  95. if (observer == null)
  96. throw new ArgumentNullException(nameof(observer));
  97. if (selector == null)
  98. throw new ArgumentNullException(nameof(selector));
  99. return Select(<#=name#><#=methodName#>(observer), selector);
  100. }
  101. <#
  102. }
  103. }
  104. #>
  105. }
  106. }