3 // Copyright (c) 2012 Petr Onderka
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 using System.Collections.Generic;
24 using System.Threading;
25 using System.Threading.Tasks.Dataflow;
28 public static class Blocks {
29 public static IEnumerable<IDataflowBlock> CreateBlocksWithOptions (
30 DataflowBlockOptions dataflowBlockOptions,
31 ExecutionDataflowBlockOptions executionDataflowBlockOptions,
32 GroupingDataflowBlockOptions groupingDataflowBlockOptions)
34 yield return new ActionBlock<int> (i => { }, executionDataflowBlockOptions);
35 yield return new BatchBlock<double> (10, groupingDataflowBlockOptions);
36 yield return new BatchedJoinBlock<dynamic, object> (
37 10, groupingDataflowBlockOptions);
38 yield return new BatchedJoinBlock<dynamic, object, char> (
39 10, groupingDataflowBlockOptions);
40 yield return new BroadcastBlock<byte> (x => x, dataflowBlockOptions);
41 yield return new BufferBlock<int> (dataflowBlockOptions);
42 yield return new JoinBlock<double, dynamic> (groupingDataflowBlockOptions);
43 yield return new JoinBlock<object, char, byte> (
44 groupingDataflowBlockOptions);
45 yield return new TransformBlock<int, int> (
46 i => i, executionDataflowBlockOptions);
47 yield return new TransformManyBlock<double, dynamic>(
48 x => new dynamic[0], executionDataflowBlockOptions);
49 yield return new WriteOnceBlock<object> (x => x, dataflowBlockOptions);
52 public static IEnumerable<IDataflowBlock> CreateBlocks()
54 return CreateBlocksWithOptions (new DataflowBlockOptions (),
55 new ExecutionDataflowBlockOptions (), new GroupingDataflowBlockOptions ());
58 public static IEnumerable<IDataflowBlock> CreateBlocksWithNameFormat(string nameFormat)
60 var dataflowBlockOptions = new DataflowBlockOptions { NameFormat = nameFormat };
61 var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { NameFormat = nameFormat };
62 var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { NameFormat = nameFormat };
64 return CreateBlocksWithOptions (dataflowBlockOptions,
65 executionDataflowBlockOptions, groupingDataflowBlockOptions);
68 public static IEnumerable<IDataflowBlock> CreateBlocksWithCancellationToken(CancellationToken cancellationToken)
70 var dataflowBlockOptions = new DataflowBlockOptions { CancellationToken = cancellationToken};
71 var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { CancellationToken = cancellationToken};
72 var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { CancellationToken = cancellationToken};
74 return CreateBlocksWithOptions (dataflowBlockOptions,
75 executionDataflowBlockOptions, groupingDataflowBlockOptions);
78 public static IEnumerable<IReceivableSourceBlock<T>> CreateSimpleSourceBlocks<T>()
80 yield return new BroadcastBlock<T> (x => x);
81 yield return new BufferBlock<T> ();
82 yield return new TransformBlock<T, T> (
84 yield return new TransformManyBlock<T, T>(
86 yield return new WriteOnceBlock<T> (x => x);
89 public static IEnumerable<ITargetBlock<T>> CreateTargetBlocks<T> ()
91 yield return new ActionBlock<T> (i => { });
92 yield return new BatchBlock<T> (10);
93 yield return new BatchedJoinBlock<T, T> (10).Target1;
94 yield return new BatchedJoinBlock<T, T> (10).Target2;
95 yield return new BatchedJoinBlock<T, T, T> (10).Target1;
96 yield return new BatchedJoinBlock<T, T, T> (10).Target2;
97 yield return new BatchedJoinBlock<T, T, T> (10).Target3;
98 yield return new BroadcastBlock<T> (x => x);
99 yield return new BufferBlock<T> ();
100 yield return new JoinBlock<T, T> ().Target1;
101 yield return new JoinBlock<T, T> ().Target2;
102 yield return new JoinBlock<T, T, T> ().Target1;
103 yield return new JoinBlock<T, T, T> ().Target2;
104 yield return new JoinBlock<T, T, T> ().Target3;
105 yield return new TransformBlock<T, T> (i => i);
106 yield return new TransformManyBlock<T, T>(x => new T[0]);
107 yield return new WriteOnceBlock<T> (x => x);
108 yield return DataflowBlock.NullTarget<T> ();