Lean  $LEAN_TAG$
PipeDataProcessor.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System.Collections.Generic;
17 using System.Linq;
18 using QuantConnect.Data;
19 
20 namespace QuantConnect.ToolBox
21 {
22  /// <summary>
23  /// Provides an implementation of <see cref="IDataProcessor"/> that simply forwards all
24  /// received data to other attached processors
25  /// </summary>
27  {
28  private readonly HashSet<IDataProcessor> _processors;
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="PipeDataProcessor"/> class
32  /// </summary>
33  /// <param name="processors">The processors to pipe the data to</param>
34  public PipeDataProcessor(IEnumerable<IDataProcessor> processors)
35  {
36  _processors = processors.ToHashSet();
37  }
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="PipeDataProcessor"/> class
41  /// </summary>
42  /// <param name="processors">The processors to pipe the data to</param>
43  public PipeDataProcessor(params IDataProcessor[] processors)
44  : this((IEnumerable<IDataProcessor>)processors)
45  {
46  }
47 
48  /// <summary>
49  /// Adds the specified processor to the output pipe
50  /// </summary>
51  /// <param name="processor">Processor to receive data from this pipe</param>
52  public void PipeTo(IDataProcessor processor)
53  {
54  _processors.Add(processor);
55  }
56 
57  /// <summary>
58  /// Invoked for each piece of data from the source file
59  /// </summary>
60  /// <param name="data">The data to be processed</param>
61  public void Process(IBaseData data)
62  {
63  foreach (var processor in _processors)
64  {
65  processor.Process(data);
66  }
67  }
68 
69  /// <summary>
70  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
71  /// </summary>
72  public void Dispose()
73  {
74  foreach (var processor in _processors)
75  {
76  processor.Dispose();
77  }
78  }
79  }
80 }