Average.Generated.tt 4.1 KB

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