~marcustomlinson/dspatch/overall_cleanup

« back to all changes in this revision

Viewing changes to include/dspatch/DspComponent.h

  • Committer: Marcus Tomlinson
  • Date: 2014-06-16 15:55:44 UTC
  • mfrom: (18.2.41 component_params)
  • Revision ID: marcus.tomlinson@canonical.com-20140616155544-o0ykqd90jtpx660o
Merged component_params

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/************************************************************************
2
2
DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
3
 
Copyright (c) 2012-2013 Marcus Tomlinson
 
3
Copyright (c) 2012-2014 Marcus Tomlinson
4
4
 
5
5
This file is part of DSPatch.
6
6
 
30
30
#include <dspatch/DspSignalBus.h>
31
31
#include <dspatch/DspWireBus.h>
32
32
#include <dspatch/DspComponentThread.h>
 
33
#include <dspatch/DspParameter.h>
 
34
 
 
35
#include <map>
33
36
 
34
37
class DspCircuit;
35
38
 
37
40
/// Abstract base class for all DSPatch components
38
41
 
39
42
/**
40
 
Classes derived from DspComponent can be added to an DspCircuit and routed to and from other
41
 
DspComponents. On construction, derived classes must configure the component's IO buses by calling
42
 
AddInput_() and AddOutput_() respectively. Derived classes must also implement the virtual method:
43
 
Process_(). The Process_() method is a callback from the DSPatch engine that occurs when a new set
44
 
of input signals is ready for processing. The Process_() method has 2 parameters: the input bus and
45
 
the output bus. This method's purpose is to pull its required inputs out of the input bus, process
46
 
these inputs, and populate the output bus with the results (see DspSignalBus).
 
43
Classes derived from DspComponent can be added to a DspCircuit and routed to and from other
 
44
DspComponents.
 
45
 
 
46
On construction, derived classes must configure the component's IO buses by calling AddInput_() and
 
47
AddOutput_() respectively, as well as populate the component's parameter map via AddParameter_()
 
48
(see DspParameter).
 
49
 
 
50
Derived classes must also implement the virtual method: Process_(). The Process_() method is a
 
51
callback from the DSPatch engine that occurs when a new set of input signals is ready for
 
52
processing. The Process_() method has 2 arguments: the input bus, and the output bus. This
 
53
method's purpose is to pull its required inputs out of the input bus, process these inputs, and
 
54
populate the output bus with the results (see DspSignalBus).
 
55
 
 
56
Derived classes that expose parameters will also need to implement the virtual ParameterUpdating_()
 
57
method. The ParameterUpdating_() method is a callback from the DSPatch engine that occurs when an
 
58
update to a component parameter has been requested via the public SetParameter() method.
 
59
ParameterUpdating_() has 2 arguments: the parameter name, and the new parameter value to be set.
 
60
This method's purpose is to: 1. validate that the new value is legal, 2. make the necessary
 
61
internal changes associated with that parameter change, and 3. update the target parameter itself
 
62
by calling the protected SetParameter_() method. If the new parameter value is legal and the update
 
63
was successful, ParameterUpdating_() should return true, otherwise, it should return false.
47
64
 
48
65
In order for a component to do any work it must be ticked over. This is performed by repeatedly
49
66
calling the Tick() and Reset() methods. The Tick() method is responsible for acquiring the next set
54
71
method. The Reset() method then informs the component that the last circuit traversal has completed
55
72
and hence can execute the next Tick() request. A component's Tick() and Reset() methods can be
56
73
called in a loop from the main application thread, or alternatively, by calling StartAutoTick(), a
57
 
seperate thread will spawn, automatically calling Tick() and Reset() methods continuously (This is
 
74
separate thread will spawn, automatically calling Tick() and Reset() methods continuously (This is
58
75
most commonly used to tick over an instance of DspCircuit).
59
76
*/
60
77
 
61
78
class DLLEXPORT DspComponent
62
79
{
63
 
  friend class DspCircuit;
64
 
  friend class DspCircuitThread;
65
 
 
66
80
public:
 
81
  enum CallbackType
 
82
  {
 
83
    InputAdded, InputRemoved,
 
84
    OutputAdded, OutputRemoved,
 
85
    ParameterAdded, ParameterRemoved, ParameterUpdated
 
86
  };
 
87
  typedef void( *Callback_t )( DspComponent const* component, CallbackType const& callbackType, int index, void* userData );
 
88
 
67
89
  DspComponent();
68
90
  virtual ~DspComponent();
69
91
 
70
 
  void SetComponentName( std::string componentName );
 
92
  void SetCallback( Callback_t const& callback, void* userData = NULL );
 
93
 
 
94
  void SetComponentName( std::string const& componentName );
71
95
  std::string GetComponentName() const;
72
96
 
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 );
 
97
  template< class FromOutputId, class ToInputId >
 
98
  bool ConnectInput( DspComponent* fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput );
 
99
 
 
100
  template< class FromOutputId, class ToInputId >
 
101
  bool ConnectInput( DspComponent& fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput );
 
102
 
 
103
  template< class FromOutputId, class ToInputId >
 
104
  void DisconnectInput( DspComponent const* fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput );
 
105
 
 
106
  template< class FromOutputId, class ToInputId >
 
107
  void DisconnectInput( DspComponent const& fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput );
84
108
 
85
109
  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;
 
110
  void DisconnectInput( std::string const& inputName );
 
111
  void DisconnectInput( DspComponent const* inputComponent );
 
112
  void DisconnectAllInputs();
 
113
 
 
114
  unsigned short GetInputCount();
 
115
  unsigned short GetOutputCount();
 
116
  unsigned short GetParameterCount();
 
117
 
 
118
  std::string GetInputName( unsigned short index );
 
119
  std::string GetOutputName( unsigned short index );
 
120
  std::string GetParameterName( unsigned short index );
 
121
 
 
122
  bool GetParameter( std::string const& paramName, DspParameter& param );
 
123
  bool SetParameter( std::string const& paramName, DspParameter const& param );
92
124
 
93
125
  void Tick();
94
126
  void Reset();
99
131
  virtual void ResumeAutoTick();
100
132
 
101
133
protected:
102
 
  virtual void Process_( DspSignalBus& inputs, DspSignalBus& outputs ) {};
103
 
 
104
 
  bool AddInput_( std::string inputName = "" );
105
 
  bool AddOutput_( std::string outputName = "" );
106
 
 
107
 
  void RemoveInputs_();
108
 
  void RemoveOutputs_();
 
134
  virtual void Process_( DspSignalBus& inputs, DspSignalBus& outputs ) {}
 
135
  virtual bool ParameterUpdating_( std::string const& name, DspParameter const& param ) { return false; }
 
136
 
 
137
  bool AddInput_( std::string const& inputName = "" );
 
138
  bool AddOutput_( std::string const& outputName = "" );
 
139
  bool AddParameter_( std::string const& paramName, DspParameter const& param );
 
140
 
 
141
  bool RemoveInput_();
 
142
  bool RemoveOutput_();
 
143
  bool RemoveParameter_();
 
144
 
 
145
  void RemoveAllInputs_();
 
146
  void RemoveAllOutputs_();
 
147
  void RemoveAllParameters_();
 
148
 
 
149
  unsigned short GetInputCount_();
 
150
  unsigned short GetOutputCount_();
 
151
  unsigned short GetParameterCount_();
 
152
 
 
153
  DspParameter const* GetParameter_( std::string const& paramName ) const;
 
154
  bool SetParameter_( std::string const& paramName, DspParameter const& param );
109
155
 
110
156
private:
111
157
  void _SetParentCircuit( DspCircuit* parentCircuit );
112
158
  DspCircuit* _GetParentCircuit();
113
159
 
114
 
  bool _FindInput( std::string signalName, unsigned short& returnIndex ) const;
 
160
  bool _FindInput( std::string const& signalName, unsigned short& returnIndex ) const;
115
161
  bool _FindInput( unsigned short signalIndex, unsigned short& returnIndex ) const;
116
 
  bool _FindOutput( std::string signalName, unsigned short& returnIndex ) const;
 
162
  bool _FindOutput( std::string const& signalName, unsigned short& returnIndex ) const;
117
163
  bool _FindOutput( unsigned short signalIndex, unsigned short& returnIndex ) const;
118
164
 
119
165
  void _SetBufferCount( unsigned short bufferCount );
122
168
  void _ThreadTick( unsigned short threadNo );
123
169
  void _ThreadReset( unsigned short threadNo );
124
170
 
125
 
  bool _SetInputSignal( unsigned short inputIndex, const DspSignal* newSignal );
126
 
  bool _SetInputSignal( unsigned short inputIndex, unsigned short threadIndex, const DspSignal* newSignal );
 
171
  bool _SetInputSignal( unsigned short inputIndex, DspSignal const* newSignal );
 
172
  bool _SetInputSignal( unsigned short inputIndex, unsigned short threadIndex, DspSignal const* newSignal );
127
173
  DspSignal* _GetOutputSignal( unsigned short outputIndex );
128
174
  DspSignal* _GetOutputSignal( unsigned short outputIndex, unsigned short threadIndex );
129
175
 
131
177
  void _ReleaseThread( unsigned short threadNo );
132
178
 
133
179
private:
 
180
  friend class DspCircuit;
 
181
  friend class DspCircuitThread;
 
182
 
134
183
  DspCircuit* _parentCircuit;
135
184
 
136
185
  unsigned short _bufferCount;
141
190
  std::vector< DspSignalBus > _inputBuses;
142
191
  std::vector< DspSignalBus > _outputBuses;
143
192
 
 
193
  std::map< std::string, DspParameter > _parameters;
 
194
 
144
195
  std::string _componentName;
145
196
  bool _isAutoTickRunning;
146
197
  bool _isAutoTickPaused;
155
206
  std::vector< bool > _gotReleases; // bool pointers not used here as only 1 thread writes to this vector at a time
156
207
  std::vector< DspMutex > _releaseMutexes;
157
208
  std::vector< DspWaitCondition > _releaseCondts;
 
209
 
 
210
  Callback_t _callback;
 
211
  void* _userData;
158
212
};
159
213
 
160
214
//=================================================================================================
161
215
 
162
 
template< class FromOutputType, class ToInputType >
163
 
bool DspComponent::ConnectInput( DspComponent* fromComponent, FromOutputType fromOutput, ToInputType toInput )
 
216
template< class FromOutputId, class ToInputId >
 
217
bool DspComponent::ConnectInput( DspComponent* fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput )
164
218
{
165
219
  unsigned short fromOutputIndex;
166
220
  unsigned short toInputIndex;
180
234
 
181
235
//-------------------------------------------------------------------------------------------------
182
236
 
183
 
template< class FromOutputType, class ToInputType >
184
 
bool DspComponent::ConnectInput( DspComponent& fromComponent, FromOutputType fromOutput, ToInputType toInput )
 
237
template< class FromOutputId, class ToInputId >
 
238
bool DspComponent::ConnectInput( DspComponent& fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput )
185
239
{
186
240
  return ConnectInput( &fromComponent, fromOutput, toInput );
187
241
}
188
242
 
189
243
//-------------------------------------------------------------------------------------------------
190
244
 
191
 
template< class FromOutputType, class ToInputType >
192
 
void DspComponent::DisconnectInput( DspComponent* fromComponent, FromOutputType fromOutput, ToInputType toInput )
 
245
template< class FromOutputId, class ToInputId >
 
246
void DspComponent::DisconnectInput( DspComponent const* fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput )
193
247
{
194
248
  unsigned short fromOutputIndex;
195
249
  unsigned short toInputIndex;
207
261
 
208
262
//-------------------------------------------------------------------------------------------------
209
263
 
210
 
template< class FromOutputType, class ToInputType >
211
 
void DspComponent::DisconnectInput( DspComponent& fromComponent, FromOutputType fromOutput, ToInputType toInput )
 
264
template< class FromOutputId, class ToInputId >
 
265
void DspComponent::DisconnectInput( DspComponent const& fromComponent, FromOutputId const& fromOutput, ToInputId const& toInput )
212
266
{
213
267
  DisconnectInput( &fromComponent, fromOutput, toInput );
214
268
}