DSPatch  v.2.42
C++ Cross-Platform, Object-Oriented, Flow-Based Programming Library
 All Classes Pages
DspComponent.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 DSPCOMPONENT_H
26 #define DSPCOMPONENT_H
27 
28 //-------------------------------------------------------------------------------------------------
29 
30 #include "DspSignalBus.h"
31 #include "DspWireBus.h"
32 #include "DspComponentThread.h"
33 
34 class DspCircuit;
35 
36 //=================================================================================================
38 
61 class DLLEXPORT DspComponent
62 {
63 public:
64  DspComponent();
65  virtual ~DspComponent();
66 
67  void SetComponentName( std::string componentName );
68  std::string GetComponentName() const;
69 
70  void SetParentCircuit( DspCircuit* parentCircuit );
71  DspCircuit* GetParentCircuit();
72 
73  template< class FromOutputType, class ToInputType >
74  bool ConnectInput( DspComponent* fromComponent, FromOutputType fromOutput, ToInputType toInput );
75 
76  template< class FromOutputType, class ToInputType >
77  bool ConnectInput( DspComponent& fromComponent, FromOutputType fromOutput, ToInputType toInput );
78 
79  template< class FromOutputType, class ToInputType >
80  void DisconnectInput( DspComponent* fromComponent, FromOutputType fromOutput, ToInputType toInput );
81 
82  template< class FromOutputType, class ToInputType >
83  void DisconnectInput( DspComponent& fromComponent, FromOutputType fromOutput, ToInputType toInput );
84 
85  void DisconnectInput( unsigned short inputIndex );
86  void DisconnectInput( std::string inputName );
87  void DisconnectInput( DspComponent* inputComponent );
88  void DisconnectInputs();
89 
90  unsigned short GetInputCount() const;
91  unsigned short GetOutputCount() const;
92 
93  bool FindInput( std::string signalName, unsigned short& returnIndex ) const;
94  bool FindInput( unsigned short signalIndex, unsigned short& returnIndex ) const;
95  bool FindOutput( std::string signalName, unsigned short& returnIndex ) const;
96  bool FindOutput( unsigned short signalIndex, unsigned short& returnIndex ) const;
97 
98  void Tick();
99  void Reset();
100 
101  virtual void StartAutoTick();
102  virtual void StopAutoTick();
103  virtual void PauseAutoTick();
104  virtual void ResumeAutoTick();
105 
106  // Methods for DspCircuit processing
107  // =================================
108 
109  void SetBufferCount( unsigned short bufferCount );
110  unsigned short GetBufferCount() const;
111 
112  void ThreadTick( unsigned short threadNo );
113  void ThreadReset( unsigned short threadNo );
114 
115  bool SetInputSignal( unsigned short inputIndex, const DspSignal* newSignal );
116  bool SetInputSignal( unsigned short inputIndex, unsigned short threadIndex, const DspSignal* newSignal );
117  DspSignal* GetOutputSignal( unsigned short outputIndex );
118  DspSignal* GetOutputSignal( unsigned short outputIndex, unsigned short threadIndex );
119 
120 protected:
121  virtual void Process_( DspSignalBus& inputs, DspSignalBus& outputs ) {};
122 
123  bool AddInput_( std::string inputName = "" );
124  bool AddOutput_( std::string outputName = "" );
125 
126  void ClearInputs_();
127  void ClearOutputs_();
128 
129 private:
130  DspCircuit* _parentCircuit;
131 
132  unsigned short _bufferCount;
133 
134  DspSignalBus _inputBus;
135  DspSignalBus _outputBus;
136 
137  std::vector< DspSignalBus > _inputBuses;
138  std::vector< DspSignalBus > _outputBuses;
139 
140  std::string _componentName;
141  bool _isAutoTickRunning;
142  bool _isAutoTickPaused;
143 
144  DspWireBus _inputWires;
145 
146  bool _hasTicked;
147 
148  DspComponentThread _componentThread;
149 
150  std::vector< bool* > _hasTickeds; // bool pointers ensure that parallel threads will only read from this vector
151  std::vector< bool > _gotReleases; // bool pointers not used here as only 1 thread writes to this vector at a time
152  std::vector< DspMutex > _releaseMutexes;
153  std::vector< DspWaitCondition > _releaseCondts;
154 
155  void _WaitForRelease( unsigned short threadNo );
156  void _ReleaseThread( unsigned short threadNo );
157 };
158 
159 //=================================================================================================
160 
161 template< class FromOutputType, class ToInputType >
162 bool DspComponent::ConnectInput( DspComponent* fromComponent, FromOutputType fromOutput, ToInputType toInput )
163 {
164  unsigned short fromOutputIndex;
165  unsigned short toInputIndex;
166 
167  if( !fromComponent->_outputBus.FindSignal( fromOutput, fromOutputIndex ) ||
168  !_inputBus.FindSignal( toInput, toInputIndex ) )
169  {
170  return false;
171  }
172 
173  PauseAutoTick();
174  _inputWires.AddWire( fromComponent, fromOutputIndex, toInputIndex );
175  ResumeAutoTick();
176 
177  return true;
178 }
179 
180 //-------------------------------------------------------------------------------------------------
181 
182 template< class FromOutputType, class ToInputType >
183 bool DspComponent::ConnectInput( DspComponent& fromComponent, FromOutputType fromOutput, ToInputType toInput )
184 {
185  return ConnectInput( &fromComponent, fromOutput, toInput );
186 }
187 
188 //-------------------------------------------------------------------------------------------------
189 
190 template< class FromOutputType, class ToInputType >
191 void DspComponent::DisconnectInput( DspComponent* fromComponent, FromOutputType fromOutput, ToInputType toInput )
192 {
193  unsigned short fromOutputIndex;
194  unsigned short toInputIndex;
195 
196  if( !fromComponent->_outputBus.FindSignal( fromOutput, fromOutputIndex ) ||
197  !_inputBus.FindSignal( toInput, toInputIndex ) )
198  {
199  return;
200  }
201 
202  PauseAutoTick();
203  _inputWires.RemoveWire( fromComponent, fromOutputIndex, toInputIndex );
204  ResumeAutoTick();
205 }
206 
207 //-------------------------------------------------------------------------------------------------
208 
209 template< class FromOutputType, class ToInputType >
210 void DspComponent::DisconnectInput( DspComponent& fromComponent, FromOutputType fromOutput, ToInputType toInput )
211 {
212  DisconnectInput( &fromComponent, fromOutput, toInput );
213 }
214 
215 //=================================================================================================
216 
217 #endif // DSPCOMPONENT_H