File tree Expand file tree Collapse file tree 4 files changed +80
-0
lines changed
src/CodeOfChaos.Extensions.ObjectPool Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ // ---------------------------------------------------------------------------------------------------------------------
2+ // Imports
3+ // ---------------------------------------------------------------------------------------------------------------------
4+ using Microsoft . Extensions . ObjectPool ;
5+
6+ namespace CodeOfChaos . Extensions . ObjectPool ;
7+ // ---------------------------------------------------------------------------------------------------------------------
8+ // Code
9+ // ---------------------------------------------------------------------------------------------------------------------
10+ public class QueuePoolPolicy < T , TItem > : IPooledObjectPolicy < T >
11+ where T : Queue < TItem > , new ( )
12+ {
13+ public T Create ( ) => new ( ) ;
14+ public bool Return ( T obj ) {
15+ obj . Clear ( ) ;
16+ return obj . Count == 0 ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ // ---------------------------------------------------------------------------------------------------------------------
2+ // Imports
3+ // ---------------------------------------------------------------------------------------------------------------------
4+ using Microsoft . Extensions . ObjectPool ;
5+
6+ namespace CodeOfChaos . Extensions . ObjectPool ;
7+ // ---------------------------------------------------------------------------------------------------------------------
8+ // Code
9+ // ---------------------------------------------------------------------------------------------------------------------
10+ public class ResettableQueuePoolPolicy < T , TItem > : IPooledObjectPolicy < T >
11+ where T : Queue < TItem > , new ( )
12+ where TItem : IResettable
13+ {
14+ public T Create ( ) => new ( ) ;
15+ public bool Return ( T obj ) {
16+ while ( obj . TryDequeue ( out TItem ? item ) ) {
17+ item . TryReset ( ) ;
18+ }
19+ obj . Clear ( ) ;
20+ return obj . Count == 0 ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ // ---------------------------------------------------------------------------------------------------------------------
2+ // Imports
3+ // ---------------------------------------------------------------------------------------------------------------------
4+ using Microsoft . Extensions . ObjectPool ;
5+
6+ namespace CodeOfChaos . Extensions . ObjectPool ;
7+ // ---------------------------------------------------------------------------------------------------------------------
8+ // Code
9+ // ---------------------------------------------------------------------------------------------------------------------
10+ public class ResettableStackPoolPolicy < T , TItem > : IPooledObjectPolicy < T >
11+ where T : Stack < TItem > , new ( )
12+ where TItem : IResettable
13+ {
14+ public T Create ( ) => new ( ) ;
15+ public bool Return ( T obj ) {
16+ while ( obj . TryPop ( out TItem ? item ) ) {
17+ item . TryReset ( ) ;
18+ }
19+ obj . Clear ( ) ;
20+ return obj . Count == 0 ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ // ---------------------------------------------------------------------------------------------------------------------
2+ // Imports
3+ // ---------------------------------------------------------------------------------------------------------------------
4+ using Microsoft . Extensions . ObjectPool ;
5+
6+ namespace CodeOfChaos . Extensions . ObjectPool ;
7+ // ---------------------------------------------------------------------------------------------------------------------
8+ // Code
9+ // ---------------------------------------------------------------------------------------------------------------------
10+ public class StackPoolPolicy < T , TItem > : IPooledObjectPolicy < T >
11+ where T : Stack < TItem > , new ( )
12+ {
13+ public T Create ( ) => new ( ) ;
14+ public bool Return ( T obj ) {
15+ obj . Clear ( ) ;
16+ return obj . Count == 0 ;
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments