| | | 1 | | /* |
| | | 2 | | * Copyright 2018 Stanislav Muhametsin. All rights Reserved. |
| | | 3 | | * |
| | | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 5 | | * you may not use this file except in compliance with the License. |
| | | 6 | | * You may obtain a copy of the License at |
| | | 7 | | * |
| | | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | | 9 | | * |
| | | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | | 13 | | * implied. |
| | | 14 | | * |
| | | 15 | | * See the License for the specific language governing permissions and |
| | | 16 | | * limitations under the License. |
| | | 17 | | */ |
| | | 18 | | using AsyncEnumeration.Abstractions; |
| | | 19 | | using System; |
| | | 20 | | using System.Collections.Generic; |
| | | 21 | | using System.Text; |
| | | 22 | | using System.Threading.Tasks; |
| | | 23 | | using UtilPack; |
| | | 24 | | |
| | | 25 | | namespace AsyncEnumeration.Implementation.Enumerable |
| | | 26 | | { |
| | | 27 | | |
| | | 28 | | internal sealed class StatefulAsyncEnumerableWrapper<T> : IAsyncEnumerable<T> |
| | | 29 | | { |
| | | 30 | | private readonly Func<WrappingEnumerationStartInfo<T>> _factory; |
| | | 31 | | |
| | | 32 | | public StatefulAsyncEnumerableWrapper( |
| | | 33 | | Func<WrappingEnumerationStartInfo<T>> startInfoFactory, |
| | | 34 | | IAsyncProvider asyncProvider |
| | | 35 | | ) |
| | | 36 | | { |
| | | 37 | | this._factory = ArgumentValidator.ValidateNotNull( nameof( startInfoFactory ), startInfoFactory ); |
| | | 38 | | this.AsyncProvider = ArgumentValidator.ValidateNotNull( nameof( asyncProvider ), asyncProvider ); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public IAsyncProvider AsyncProvider { get; } |
| | | 42 | | |
| | | 43 | | public IAsyncEnumerator<T> GetAsyncEnumerator() |
| | | 44 | | { |
| | | 45 | | return new AsyncEnumeratorWrapper<T>( this._factory() ); |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | internal sealed class StatelessAsyncEnumerableWrapper<T> : IAsyncEnumerable<T> |
| | | 50 | | { |
| | | 51 | | private readonly AsyncEnumeratorWrapper<T> _enumerator; |
| | | 52 | | |
| | | 53 | | public StatelessAsyncEnumerableWrapper( |
| | | 54 | | WrappingEnumerationStartInfo<T> startInfo, |
| | | 55 | | IAsyncProvider asyncProvider |
| | | 56 | | ) |
| | | 57 | | { |
| | | 58 | | this._enumerator = new AsyncEnumeratorWrapper<T>( startInfo ); |
| | | 59 | | this.AsyncProvider = ArgumentValidator.ValidateNotNull( nameof( asyncProvider ), asyncProvider ); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public IAsyncProvider AsyncProvider { get; } |
| | | 63 | | |
| | | 64 | | public IAsyncEnumerator<T> GetAsyncEnumerator() |
| | | 65 | | { |
| | | 66 | | return this._enumerator; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | internal sealed class AsyncEnumeratorWrapper<T> : IAsyncEnumerator<T> |
| | | 71 | | { |
| | | 72 | | private readonly WaitForNextDelegate _waitForNext; |
| | | 73 | | private readonly TryGetNextDelegate<T> _tryGetNext; |
| | | 74 | | private readonly EnumerationEndedDelegate _dispose; |
| | | 75 | | |
| | | 76 | | public AsyncEnumeratorWrapper( |
| | | 77 | | WrappingEnumerationStartInfo<T> startInfo |
| | | 78 | | ) |
| | | 79 | | { |
| | | 80 | | AsyncEnumeratorWrapperInitializer.InitVars( startInfo.WaitForNext, startInfo.TryGetNext, startInfo.Dispose, out |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public Task<Boolean> WaitForNextAsync() => this._waitForNext(); |
| | | 84 | | |
| | | 85 | | public T TryGetNext( out Boolean success ) => this._tryGetNext( out success ); |
| | | 86 | | |
| | | 87 | | public Task DisposeAsync() => this._dispose(); |
| | | 88 | | |
| | | 89 | | |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | internal static class AsyncEnumeratorWrapperInitializer |
| | | 93 | | { |
| | | 94 | | internal static void InitVars<T>( |
| | | 95 | | WaitForNextDelegate waitForNext, |
| | | 96 | | TryGetNextDelegate<T> tryGetNext, |
| | | 97 | | EnumerationEndedDelegate dispose, |
| | | 98 | | out WaitForNextDelegate fieldWaitForNext, |
| | | 99 | | out TryGetNextDelegate<T> fieldTryGetNext, |
| | | 100 | | out EnumerationEndedDelegate fieldDispose |
| | | 101 | | ) |
| | | 102 | | { |
| | 21 | 103 | | fieldWaitForNext = ArgumentValidator.ValidateNotNull( nameof( waitForNext ), waitForNext ); |
| | 21 | 104 | | fieldTryGetNext = ArgumentValidator.ValidateNotNull( nameof( tryGetNext ), tryGetNext ); |
| | 41 | 105 | | fieldDispose = dispose ?? ( () => TaskUtils.CompletedTask ); |
| | 21 | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |