This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from JacopoWolf/dev/generics
Dev/generics
- Loading branch information
Showing
36 changed files
with
694 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using StackInjector.Wrappers; | ||
|
||
namespace StackInjector.Core | ||
{ | ||
internal abstract partial class AsyncStackWrapperCore<T> : AsyncStackWrapperCore, IAsyncStackWrapperCore<T> | ||
{ | ||
|
||
// used to cancel everything | ||
protected internal readonly CancellationTokenSource cancelPendingTasksSource = new CancellationTokenSource(); | ||
|
||
// exposes the token | ||
public CancellationToken PendingTasksCancellationToken | ||
=> this.cancelPendingTasksSource.Token; | ||
|
||
// used to lock access to tasks | ||
protected internal readonly object listAccessLock = new object(); | ||
|
||
// asyncronously waited for new events if TaskList is empty | ||
protected internal readonly SemaphoreSlim emptyListAwaiter = new SemaphoreSlim(0); | ||
|
||
// pending tasks | ||
protected internal LinkedList<Task<T>> tasks = new LinkedList<Task<T>>(); | ||
|
||
|
||
internal AsyncStackWrapperCore ( WrapperCore core, Type toRegister ) : base(core, toRegister) | ||
{ | ||
// register an event that in case the list is empty, release the empty event listener. | ||
this.cancelPendingTasksSource.Token.Register(this.ReleaseListAwaiter); | ||
} | ||
|
||
|
||
|
||
#region IDisposable Support | ||
|
||
private bool disposedValue = false; | ||
|
||
public override void Dispose () | ||
{ | ||
if( !this.disposedValue ) | ||
{ | ||
|
||
// managed resources | ||
this.cancelPendingTasksSource.Cancel(); | ||
this.ReleaseListAwaiter(); // in case it's waiting on the empty list | ||
|
||
this.cancelPendingTasksSource.Dispose(); | ||
this.emptyListAwaiter.Dispose(); | ||
|
||
|
||
// big objects | ||
this.tasks.Clear(); | ||
this.tasks = null; | ||
|
||
// clean instantiated objects | ||
this.Core.RemoveInstancesDiff(); | ||
|
||
|
||
this.disposedValue = true; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using StackInjector.Wrappers; | ||
using StackInjector.Wrappers.Generic; | ||
|
||
namespace StackInjector.Core.Cloning | ||
{ | ||
internal class ClonedCore : IClonedCore | ||
{ | ||
|
||
private readonly WrapperCore clonedCore; | ||
|
||
internal ClonedCore ( WrapperCore clonedCore ) | ||
=> | ||
this.clonedCore = clonedCore; | ||
|
||
|
||
public IAsyncStackWrapper ToAsyncWrapper<T> () where T : IAsyncStackEntryPoint | ||
{ | ||
var wrapper = new AsyncStackWrapper( this.clonedCore ); | ||
|
||
this.clonedCore.entryPoint = typeof(T); | ||
this.clonedCore.ServeAll(); | ||
|
||
return wrapper; | ||
} | ||
|
||
//todo implement | ||
public IAsyncStackWrapper<TEntry, TIn, TOut> ToGenericAsync<TEntry, TIn, TOut> ( AsyncStackDigest<TEntry, TIn, TOut> digest ) | ||
=> throw new NotImplementedException(); | ||
|
||
|
||
public IStackWrapper ToWrapper<T> () where T : IStackEntryPoint | ||
{ | ||
var wrapper = new StackWrapper( this.clonedCore ); | ||
|
||
this.clonedCore.entryPoint = typeof(T); | ||
this.clonedCore.ServeAll(); | ||
|
||
return wrapper; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using StackInjector.Settings; | ||
|
||
namespace StackInjector.Core.Cloning | ||
{ | ||
|
||
/// <summary> | ||
/// Allows for a wrapper to have its core cloned. | ||
/// </summary> | ||
public interface ICloneableCore | ||
{ | ||
|
||
/// <summary> | ||
/// Clone the core of this wrapper, copying the already existing structure | ||
/// and making instantiation of objects faster. | ||
/// </summary> | ||
/// <param name="settings">if set, overrides the previus core settings.</param> | ||
/// <returns>A generic object allowing conversion of the cloned core</returns> | ||
IClonedCore CloneCore ( StackWrapperSettings settings = null ); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using StackInjector.Wrappers; | ||
using StackInjector.Wrappers.Generic; | ||
|
||
namespace StackInjector.Core.Cloning | ||
{ | ||
/// <summary> | ||
/// A cloned structure of a wrapper. | ||
/// </summary> | ||
public interface IClonedCore | ||
{ | ||
|
||
/// <summary> | ||
/// convert this to an <see cref="IStackWrapper"/> | ||
/// </summary> | ||
/// <typeparam name="T">entry point of the new wrapper</typeparam> | ||
/// <returns>the new wrapper</returns> | ||
IStackWrapper ToWrapper<T> () where T : IStackEntryPoint; | ||
|
||
|
||
/// <summary> | ||
/// convert this to an <see cref="IAsyncStackWrapper"/> | ||
/// </summary> | ||
/// <typeparam name="T">entry point of the new wrapper</typeparam> | ||
/// <returns>the new wrapper</returns> | ||
IAsyncStackWrapper ToAsyncWrapper<T> () where T : IAsyncStackEntryPoint; | ||
|
||
|
||
/// <summary> | ||
/// convert this to an <see cref="IAsyncStackWrapper{TEntry, TIn, TOut}"/> | ||
/// </summary> | ||
/// <typeparam name="TEntry">entry instantiation poin of the new wrapper</typeparam> | ||
/// <typeparam name="TIn">type of input elements</typeparam> | ||
/// <typeparam name="TOut">type of output elements</typeparam> | ||
/// <param name="digest">action to perform on elements</param> | ||
/// <returns>the new wrapper</returns> | ||
IAsyncStackWrapper<TEntry, TIn, TOut> ToGenericAsync<TEntry, TIn, TOut> ( AsyncStackDigest<TEntry, TIn,TOut> digest ); | ||
|
||
} | ||
|
||
} |
27 changes: 13 additions & 14 deletions
27
StackInjector/IAsyncStackWrapper.cs → StackInjector/Core/IAsyncStackWrapperCore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace StackInjector | ||
namespace StackInjector.Core | ||
{ | ||
/// <summary> | ||
/// Wraps a Stack of dependency-injected classes, and manages an <see cref="IAsyncEnumerable{T}"/> of completed tasks. | ||
/// base interface for all asyncronous stackwrappers. | ||
/// </summary> | ||
public interface IAsyncStackWrapper : IStackWrapperStructure | ||
/// <typeparam name="T">the type tasks will return</typeparam> | ||
public interface IAsyncStackWrapperCore<T> : IStackWrapperCore | ||
{ | ||
|
||
/// <summary> | ||
/// Used to signal cancellation of every pending job. | ||
/// Used to signal cancellation of every pending task | ||
/// </summary> | ||
public CancellationToken CancelPendingTasksToken { get; } | ||
CancellationToken PendingTasksCancellationToken { get; } | ||
|
||
|
||
/// <summary> | ||
/// Submit a new object to be elaborated asyncronously in this stack | ||
/// submit new work to this wrapper | ||
/// </summary> | ||
/// <param name="submitted">The object to elaborate</param> | ||
void Submit ( object submitted ); | ||
|
||
/// <param name="work"></param> | ||
void Submit ( Task<T> work ); | ||
|
||
/// <summary> | ||
/// The loop you ca use to <c>await foreach</c> tasks in elaboration, converted to the specified type. | ||
/// When the pending tasks list is empty, unless <see cref="IDisposable.Dispose"/> is explocitly called | ||
/// this will wait indefinitively. | ||
/// </summary> | ||
/// <typeparam name="T">Type to cast the object returned by the entry point</typeparam> | ||
/// <exception cref="InvalidCastException"></exception> | ||
/// <returns>An asyncronous enumerable of completed tasks</returns> | ||
IAsyncEnumerable<T> Elaborated<T> (); | ||
IAsyncEnumerable<T> Elaborated (); | ||
|
||
/// <summary> | ||
/// check if there are tasks left to elaborate | ||
/// </summary> | ||
/// <returns>true if there are pending tasks</returns> | ||
bool AnyTaskLeft (); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using StackInjector.Core.Cloning; | ||
using StackInjector.Settings; | ||
|
||
namespace StackInjector.Core | ||
{ | ||
|
||
/// <summary> | ||
/// base interface for all stackwrappers | ||
/// </summary> | ||
public interface IStackWrapperCore : IDisposable, ICloneableCore | ||
{ | ||
|
||
/// <summary> | ||
/// the settings of this stackwrapper | ||
/// </summary> | ||
ref readonly StackWrapperSettings Settings { get; } | ||
|
||
} | ||
} |
Oops, something went wrong.