DSPatch  v.2.42
C++ Cross-Platform, Object-Oriented, Flow-Based Programming Library
 All Classes Pages
DspCircuit.h
1 /************************************************************************
2 DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
3 Copyright (c) 2012-2013 Marcus Tomlinson
4 
5 This file is part of DSPatch.
6 
7 GNU Lesser General Public License Usage
8 This file may be used under the terms of the GNU Lesser General Public
9 License version 3.0 as published by the Free Software Foundation and
10 appearing in the file LGPLv3.txt included in the packaging of this
11 file. Please review the following information to ensure the GNU Lesser
12 General Public License version 3.0 requirements will be met:
13 http://www.gnu.org/copyleft/lgpl.html.
14 
15 Other Usage
16 Alternatively, this file may be used in accordance with the terms and
17 conditions contained in a signed written agreement between you and
18 Marcus Tomlinson.
19 
20 DSPatch is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 ************************************************************************/
24 
25 #ifndef DSPCIRCUIT_H
26 #define DSPCIRCUIT_H
27 
28 //-------------------------------------------------------------------------------------------------
29 
30 #include "DspComponent.h"
31 #include "DspWireBus.h"
32 #include "DspCircuitThread.h"
33 
34 //=================================================================================================
36 
62 class DLLEXPORT DspCircuit : public DspComponent
63 {
64 public:
65  DspCircuit( unsigned short threadCount = 0 );
66  ~DspCircuit();
67 
68  virtual void PauseAutoTick();
69 
70  void SetThreadCount( unsigned short threadCount );
71  unsigned short GetThreadCount() const;
72 
73  bool AddComponent( DspComponent* component, std::string componentName = "" );
74  bool AddComponent( DspComponent& component, std::string componentName = "" );
75 
76  void RemoveComponent( DspComponent* component );
77  void RemoveComponent( DspComponent& component );
78  void RemoveComponent( std::string componentName );
79 
80  void RemoveAllComponents();
81 
82  template< class ComponentType >
83  ComponentType* GetComponent( std::string componentName );
84 
85  unsigned short GetComponentCount() const;
86 
87  // component output to component input
88  template< class FromComponentType, class FromOutputType, class ToComponentType, class ToInputType >
89  bool ConnectOutToIn( FromComponentType& fromComponent, FromOutputType fromOutput, ToComponentType& toComponent, ToInputType toInput );
90 
91  // circuit input to component input
92  template< class FromInputType, class ToComponentType, class ToInputType >
93  bool ConnectInToIn( FromInputType fromInput, ToComponentType& toComponent, ToInputType toInput );
94 
95  // component output to circuit output
96  template< class FromComponentType, class FromOutputType, class ToOutputType >
97  bool ConnectOutToOut( FromComponentType& fromComponent, FromOutputType fromOutput, ToOutputType toOutput );
98 
99  // component output to component input
100  template< class FromComponentType, class FromOutputType, class ToComponentType, class ToInputType >
101  void DisconnectOutToIn( FromComponentType& fromComponent, FromOutputType fromOutput, ToComponentType& toComponent, ToInputType toInput );
102 
103  // circuit input to component input
104  template< class FromInputType, class ToComponentType, class ToInputType >
105  bool DisconnectInToIn( FromInputType fromInput, ToComponentType& toComponent, ToInputType toInput );
106 
107  // component output to circuit output
108  template< class FromComponentType, class FromOutputType, class ToOutputType >
109  bool DisconnectOutToOut( FromComponentType& fromComponent, FromOutputType fromOutput, ToOutputType toOutput );
110 
111  void DisconnectComponent( std::string component );
112 
113  bool AddInput( std::string inputName = "" );
114  bool AddOutput( std::string outputName = "" );
115 
116  void ClearInputs();
117  void ClearOutputs();
118 
119 protected:
120  virtual void Process_( DspSignalBus& inputs, DspSignalBus& outputs );
121 
122 private:
123  std::vector< DspComponent* > _components;
124 
125  std::vector< DspCircuitThread > _circuitThreads;
126  unsigned short _currentThreadIndex;
127 
128  DspWireBus _inToInWires;
129  DspWireBus _outToOutWires;
130 
131  bool _FindComponent( DspComponent* component, unsigned short& returnIndex ) const;
132  bool _FindComponent( DspComponent& component, unsigned short& returnIndex ) const;
133  bool _FindComponent( std::string componentName, unsigned short& returnIndex ) const;
134  bool _FindComponent( unsigned short componentIndex, unsigned short& returnIndex ) const;
135 
136  void _DisconnectComponent( unsigned short componentIndex );
137  void _RemoveComponent( unsigned short componentIndex );
138 };
139 
140 //=================================================================================================
141 
142 template< class ComponentType >
143 ComponentType* DspCircuit::GetComponent( std::string componentName )
144 {
145  unsigned short componentIndex;
146 
147  if( _FindComponent( componentName, componentIndex ) )
148  {
149  return static_cast< ComponentType* >( _components[componentIndex] );
150  }
151  else
152  {
153  return NULL;
154  }
155 }
156 
157 //-------------------------------------------------------------------------------------------------
158 
159 template< class FromComponentType, class FromOutputType, class ToComponentType, class ToInputType >
160 bool DspCircuit::ConnectOutToIn( FromComponentType& fromComponent, FromOutputType fromOutput, ToComponentType& toComponent, ToInputType toInput )
161 {
162  unsigned short fromComponentIndex;
163  unsigned short toComponentIndex;
164 
165  // only interconnect components that have been added to this system
166  if( !_FindComponent( fromComponent, fromComponentIndex ) ||
167  !_FindComponent( toComponent, toComponentIndex ) )
168  {
169  return false;
170  }
171 
172  PauseAutoTick();
173 
174  bool result = _components[toComponentIndex]->ConnectInput( _components[fromComponentIndex], fromOutput, toInput );
175 
176  ResumeAutoTick();
177 
178  return result;
179 }
180 
181 //-------------------------------------------------------------------------------------------------
182 
183 template< class FromInputType, class ToComponentType, class ToInputType >
184 bool DspCircuit::ConnectInToIn( FromInputType fromInput, ToComponentType& toComponent, ToInputType toInput )
185 {
186  unsigned short fromInputIndex;
187  unsigned short toComponentIndex;
188  unsigned short toInputIndex;
189 
190  // only interconnect components that have been added to this system
191  if( !FindInput( fromInput, fromInputIndex ) ||
192  !_FindComponent( toComponent, toComponentIndex ) ||
193  !_components[toComponentIndex]->FindInput( toInput, toInputIndex ) )
194  {
195  return false;
196  }
197 
198  PauseAutoTick();
199 
200  bool result = _inToInWires.AddWire( _components[toComponentIndex], fromInputIndex, toInputIndex );
201 
202  ResumeAutoTick();
203 
204  return result;
205 }
206 
207 //-------------------------------------------------------------------------------------------------
208 
209 template< class FromComponentType, class FromOutputType, class ToOutputType >
210 bool DspCircuit::ConnectOutToOut( FromComponentType& fromComponent, FromOutputType fromOutput, ToOutputType toOutput )
211 {
212  unsigned short fromComponentIndex;
213  unsigned short fromOutputIndex;
214  unsigned short toOutputIndex;
215 
216  // only interconnect components that have been added to this system
217  if( !_FindComponent( fromComponent, fromComponentIndex ) ||
218  !_components[fromComponentIndex]->FindOutput( fromOutput, fromOutputIndex ) ||
219  !FindOutput( toOutput, toOutputIndex ) )
220  {
221  return false;
222  }
223 
224  PauseAutoTick();
225 
226  bool result = _outToOutWires.AddWire( _components[fromComponentIndex], fromOutputIndex, toOutputIndex );
227 
228  ResumeAutoTick();
229 
230  return result;
231 }
232 
233 //-------------------------------------------------------------------------------------------------
234 
235 template< class FromComponentType, class FromOutputType, class ToComponentType, class ToInputType >
236 void DspCircuit::DisconnectOutToIn( FromComponentType& fromComponent, FromOutputType fromOutput, ToComponentType& toComponent, ToInputType toInput )
237 {
238  unsigned short fromComponentIndex;
239  unsigned short toComponentIndex;
240 
241  // only interconnect components that have been added to this system
242  if( !_FindComponent( fromComponent, fromComponentIndex ) ||
243  !_FindComponent( toComponent, toComponentIndex ) )
244  {
245  return;
246  }
247 
248  PauseAutoTick();
249 
250  _components[toComponentIndex]->DisconnectInput( _components[fromComponentIndex], fromOutput, toInput );
251 
252  ResumeAutoTick();
253 }
254 
255 //-------------------------------------------------------------------------------------------------
256 
257 template< class FromInputType, class ToComponentType, class ToInputType >
258 bool DspCircuit::DisconnectInToIn( FromInputType fromInput, ToComponentType& toComponent, ToInputType toInput )
259 {
260  unsigned short fromInputIndex;
261  unsigned short toComponentIndex;
262  unsigned short toInputIndex;
263 
264  // only interconnect components that have been added to this system
265  if( !FindInput( fromInput, fromInputIndex ) ||
266  !_FindComponent( toComponent, toComponentIndex ) ||
267  !_components[toComponentIndex]->FindInput( toInput, toInputIndex ) )
268  {
269  return false;
270  }
271 
272  PauseAutoTick();
273 
274  bool result = _inToInWires.RemoveWire( _components[toComponentIndex], fromInputIndex, toInputIndex );
275 
276  ResumeAutoTick();
277 
278  return result;
279 }
280 
281 //-------------------------------------------------------------------------------------------------
282 
283 template< class FromComponentType, class FromOutputType, class ToOutputType >
284 bool DspCircuit::DisconnectOutToOut( FromComponentType& fromComponent, FromOutputType fromOutput, ToOutputType toOutput )
285 {
286  unsigned short fromComponentIndex;
287  unsigned short fromOutputIndex;
288  unsigned short toOutputIndex;
289 
290  // only interconnect components that have been added to this system
291  if( !_FindComponent( fromComponent, fromComponentIndex ) ||
292  !_components[fromComponentIndex]->FindOutput( fromOutput, fromOutputIndex ) ||
293  !FindOutput( toOutput, toOutputIndex ) )
294  {
295  return false;
296  }
297 
298  PauseAutoTick();
299 
300  bool result = _outToOutWires.RemoveWire( _components[fromComponentIndex], fromOutputIndex, toOutputIndex );
301 
302  ResumeAutoTick();
303 
304  return result;
305 }
306 
307 //=================================================================================================
308 
309 #endif // DSPCIRCUIT_H