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 namespace System.Threading.Tasks.Dataflow {
25 /// This block is used by the version of <see cref="DataflowBlock.LinkTo"/>
26 /// that has a predicate to wrap the target block,
27 /// so that the predicate can be checked.
29 class PredicateBlock<T> : ITargetBlock<T> {
31 /// Wraps the source block of the link.
32 /// This is necessary so that the communication from target to source works correctly.
34 class SourceBlock : ISourceBlock<T> {
35 readonly ISourceBlock<T> actualSource;
36 readonly PredicateBlock<T> predicateBlock;
38 public SourceBlock (ISourceBlock<T> actualSource,
39 PredicateBlock<T> predicateBlock)
41 this.actualSource = actualSource;
42 this.predicateBlock = predicateBlock;
45 public Task Completion
47 get { return actualSource.Completion; }
50 public void Complete ()
52 actualSource.Complete ();
55 public void Fault (Exception exception)
57 actualSource.Fault (exception);
60 public T ConsumeMessage (DataflowMessageHeader messageHeader,
61 ITargetBlock<T> target, out bool messageConsumed)
63 return actualSource.ConsumeMessage (messageHeader, predicateBlock,
67 public IDisposable LinkTo (ITargetBlock<T> target,
68 DataflowLinkOptions linkOptions)
70 return actualSource.LinkTo (target, linkOptions);
73 public void ReleaseReservation (DataflowMessageHeader messageHeader,
74 ITargetBlock<T> target)
76 actualSource.ReleaseReservation (messageHeader, predicateBlock);
79 public bool ReserveMessage (DataflowMessageHeader messageHeader,
80 ITargetBlock<T> target)
82 return actualSource.ReserveMessage (messageHeader, predicateBlock);
86 readonly ITargetBlock<T> actualTarget;
87 readonly Predicate<T> predicate;
88 readonly SourceBlock sourceBlock;
90 public PredicateBlock (ISourceBlock<T> actualSource,
91 ITargetBlock<T> actualTarget, Predicate<T> predicate)
93 this.actualTarget = actualTarget;
94 this.predicate = predicate;
95 sourceBlock = new SourceBlock (actualSource, this);
98 public DataflowMessageStatus OfferMessage (
99 DataflowMessageHeader messageHeader, T messageValue, ISourceBlock<T> source,
100 bool consumeToAccept)
102 if (!messageHeader.IsValid)
103 throw new ArgumentException ("The messageHeader is not valid.",
105 if (consumeToAccept && source == null)
106 throw new ArgumentException (
107 "consumeToAccept may only be true if provided with a non-null source.",
110 if (!predicate(messageValue))
111 return DataflowMessageStatus.Declined;
113 return actualTarget.OfferMessage (messageHeader, messageValue, sourceBlock,
117 public Task Completion {
118 get { return actualTarget.Completion; }
121 public void Complete ()
123 actualTarget.Complete ();
126 public void Fault (Exception exception)
128 actualTarget.Fault (exception);