Jamba  3.2.0
GUIParamCxMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #ifndef __PONGASOFT_VST_GUI_PARAM_CX_MGR_H__
19 #define __PONGASOFT_VST_GUI_PARAM_CX_MGR_H__
20 
22 #include <unordered_map>
23 
24 namespace pongasoft {
25 namespace VST {
26 namespace GUI {
27 namespace Params {
28 
30 {
31 public:
35  inline bool existsVst(ParamID iParamID) const { return fGUIState->existsVst(iParamID); }
36 
40  inline bool existsJmb(ParamID iParamID) const { return fGUIState->existsJmb(iParamID); }
41 
47  bool unregisterParam(ParamID iParamID);
48 
51  void unregisterAll();
52 
56  inline GUIRawVstParam registerRawVstParam(ParamID iParamID,
57  Parameters::IChangeListener *iChangeListener = nullptr)
58  {
59  return __registerRawVstParam(iParamID, iChangeListener);
60  }
61 
65  inline GUIRawVstParam registerRawVstCallback(ParamID iParamID,
66  Parameters::ChangeCallback iChangeCallback,
67  bool iInvokeCallback)
68  {
69  return maybeInvokeCallback(__registerRawVstParam(iParamID, iChangeCallback),
70  iChangeCallback,
71  iInvokeCallback);
72  }
73 
78  template<typename T>
79  inline GUIVstParam<T> registerVstParam(ParamID iParamID,
80  Parameters::IChangeListener *iChangeListener = nullptr)
81  {
82  return __registerVstParam<T>(iParamID, iChangeListener);
83  }
84 
89  template<typename T>
90  inline GUIVstParam<T> registerVstCallback(ParamID iParamID,
91  Parameters::ChangeCallback iChangeCallback,
92  bool iInvokeCallback)
93  {
94  return maybeInvokeCallback(__registerVstParam<T>(iParamID, iChangeCallback),
95  iChangeCallback,
96  iInvokeCallback);
97  }
98 
103  template<typename T>
104  inline GUIVstParam<T> registerVstParam(VstParam<T> const &iParamDef,
105  Parameters::IChangeListener *iChangeListener = nullptr)
106  {
107  return __registerVstParam(iParamDef, iChangeListener);
108  }
109 
114  template<typename T>
116  Parameters::ChangeCallback iChangeCallback,
117  bool iInvokeCallback)
118  {
119  return maybeInvokeCallback(__registerVstParam(iParamDef, iChangeCallback),
120  iChangeCallback,
121  iInvokeCallback);
122  }
123 
129  template<typename T>
130  bool registerVstCallback(VstParam<T> iParamDef,
132  bool iInvokeCallback);
133 
141  template<typename T>
143  {
144  DCHECK_F(iChangeListener != nullptr);
145  fParamCxs[iParamDef.getParamID()] = std::move(iParamDef.connect(iChangeListener));
146  return iParamDef;
147  }
148 
154  template<typename T>
156  Parameters::ChangeCallback iChangeCallback,
157  bool iInvokeCallback)
158  {
159  DCHECK_F((bool) iChangeCallback);
160  fParamCxs[iParamDef.getParamID()] = std::move(iParamDef.connect(iChangeCallback));
161  if(iInvokeCallback)
162  iChangeCallback();
163  return iParamDef;
164  }
165 
169  template<typename T>
172  bool iInvokeCallback)
173  {
174  DCHECK_F((bool) iChangeCallback);
175  auto callback = [&iParamDef, cb2 = std::move(iChangeCallback)] () { cb2(iParamDef); };
176  registerJmbCallback(iParamDef, callback, iInvokeCallback);
177  }
178 
179 
185  template<typename T>
187  Parameters::IChangeListener *iChangeListener = nullptr)
188  {
189  return __registerJmbParam<T>(iParamID, iChangeListener);
190  }
191 
197  template<typename T>
199  Parameters::ChangeCallback iChangeCallback,
200  bool iInvokeCallback)
201  {
202  return maybeInvokeCallback(__registerJmbParam<T>(iParamID, iChangeCallback),
203  iChangeCallback,
204  iInvokeCallback);
205  }
206 
207  // getGUIState
208  GUIState *getGUIState() const { return fGUIState; };
209 
212  void invokeAll();
213 
214  friend class GUI::GUIState;
215 
216 protected:
217  explicit GUIParamCxMgr(GUIState *iGUIState) : fGUIState{iGUIState}
218  {
219  DCHECK_F(fGUIState != nullptr);
220  }
221 
224  template<typename Listener>
225  std::unique_ptr<GUIRawVstParameter> __registerRawVstParameter(ParamID iParamID, Listener iListener);
226 
227  // __registerRawVstParam
228  template<typename Listener>
229  GUIRawVstParam __registerRawVstParam(ParamID iParamID, Listener iListener);
230 
231  // __registerVstParam
232  template<typename T, typename Listener>
233  GUIVstParam<T> __registerVstParam(ParamID iParamID, Listener iListener);
234 
235  // __registerVstParam
236  template<typename T, typename Listener>
237  GUIVstParam<T> __registerVstParam(VstParam<T> const &iParamDef, Listener iListener);
238 
239  // __registerJmbParam
240  template<typename T, typename Listener>
241  GUIJmbParam<T> __registerJmbParam(ParamID iParamID, Listener iListener);
242 
243  // maybeInvokeCallback
244  template<typename TParam>
245  inline TParam &&maybeInvokeCallback(TParam &&iParam, Parameters::ChangeCallback &iCallback, bool iInvokeCallback)
246  {
247  if(iInvokeCallback && iParam.exists() && iCallback)
248  iCallback();
249  return std::forward<TParam>(iParam);
250  }
251 
252 private:
253  // the gui state
255 
256  // Maintains the connections for the listeners... will be automatically discarded in the destructor
257  std::unordered_map<ParamID, std::unique_ptr<FObjectCx>> fParamCxs;
258 };
259 
260 //------------------------------------------------------------------------
261 // GUIParamCxMgr::__registerRawVstParameter
262 //------------------------------------------------------------------------
263 template<typename Listener>
264 std::unique_ptr<GUIRawVstParameter> GUIParamCxMgr::__registerRawVstParameter(ParamID iParamID, Listener iListener)
265 {
266  auto param = fGUIState->getRawVstParameter(iParamID);
267 
268  if(!param)
269  {
270  DLOG_F(WARNING, "vst param [%d] not found", iParamID);
271  return nullptr;
272  }
273 
274  if(iListener)
275  {
276  fParamCxs[iParamID] = param->connect(iListener);
277  }
278  else
279  {
280  unregisterParam(iParamID);
281  }
282 
283  return param;
284 }
285 
286 //------------------------------------------------------------------------
287 // GUIParamCxMgr::__registerRawVstParam
288 //------------------------------------------------------------------------
289 template<typename Listener>
290 GUIRawVstParam GUIParamCxMgr::__registerRawVstParam(ParamID iParamID, Listener iListener)
291 {
292  auto param = __registerRawVstParameter(iParamID, iListener);
293 
294  if(!param)
295  {
296  return GUIRawVstParam{};
297  }
298 
299  return GUIRawVstParam{std::move(param)};
300 }
301 
302 
303 //------------------------------------------------------------------------
304 // GUIParamCxMgr::__registerVstParam
305 //------------------------------------------------------------------------
306 template<typename T, typename Listener>
307 GUIVstParam<T> GUIParamCxMgr::__registerVstParam(VstParam<T> const &iParamDef, Listener iListener)
308 {
309  auto param = __registerRawVstParameter(iParamDef->fParamID, iListener);
310 
311  if(param)
312  return GUIVstParam<T>{std::make_unique<GUIVstParameter<T>>(std::move(param), iParamDef)};
313  else
314  return GUIVstParam<T>{};
315 }
316 
317 //------------------------------------------------------------------------
318 // GUIParamCxMgr::__registerVstParam
319 //------------------------------------------------------------------------
320 template<typename T, typename Listener>
321 GUIVstParam<T> GUIParamCxMgr::__registerVstParam(ParamID iParamID, Listener iListener)
322 {
323  if(iListener)
324  {
325  auto param = fGUIState->getGUIVstParameter<T>(iParamID);
326 
327  if(param)
328  {
329  fParamCxs[iParamID] = param->connect(iListener);
330  return GUIVstParam<T>{std::move(param)};
331  }
332  }
333 
334  unregisterParam(iParamID);
335  return GUIVstParam<T>{};
336 }
337 
338 //------------------------------------------------------------------------
339 // GUIParamCxMgr::__registerJmbParam
340 //------------------------------------------------------------------------
341 template<typename T, typename Listener>
342 GUIJmbParam<T> GUIParamCxMgr::__registerJmbParam(ParamID iParamID, Listener iListener)
343 {
344  auto param = fGUIState->getJmbParameter(iParamID);
345 
346  if(!param)
347  {
348  DLOG_F(WARNING, "jmb param [%d] not found", iParamID);
349  return nullptr;
350  }
351 
352  auto res = dynamic_cast<GUIJmbParameter<T> *>(param);
353  if(res)
354  {
355  if(iListener)
356  fParamCxs[iParamID] = std::move(res->connect(iListener));
357  else
358  unregisterParam(iParamID);
359  }
360  else
361  {
362  DLOG_F(WARNING, "jmb param [%d] is not of the requested type", iParamID);
363  unregisterParam(iParamID);
364  }
365 
366  return res;
367 }
368 
369 //------------------------------------------------------------------------
370 // GUIParamCxMgr::registerVstCallback
371 //------------------------------------------------------------------------
372 template<typename T>
375  bool iInvokeCallback)
376 {
377  auto paramID = iParamDef->fParamID;
378 
379  if(iChangeCallback)
380  {
381  auto param = fGUIState->getGUIVstParameter<T>(paramID);
382 
383  if(param)
384  {
385  // Implementation note:
386  // 1) using std::make_unique results in an error on std::move(callback) because unique_ptr
387  // cannot be copied...
388  // 2) we need to access the ptr afterwards to call connect
389  auto ptr = std::make_shared<GUIVstParam<T>>(std::move(param));
390 
391  auto callback = [ptr, cb1 = std::move(iChangeCallback)] () {
392  cb1(*ptr);
393  };
394 
395  fParamCxs[paramID] = ptr->connect(callback);
396 
397  if(iInvokeCallback)
398  callback();
399 
400  return true;
401  }
402  }
403 
404  unregisterParam(paramID);
405  return false;
406 }
407 
408 }
409 }
410 }
411 }
412 
413 #endif //__PONGASOFT_VST_GUI_PARAM_CX_MGR_H__
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:358
Definition: GUIState.h:39
Definition: GUIRawVstParameter.h:216
GUIRawVstParam __registerRawVstParam(ParamID iParamID, Listener iListener)
Definition: GUIParamCxMgr.h:290
GUIState * fGUIState
Definition: GUIParamCxMgr.h:254
IGUIJmbParameter * getJmbParameter(ParamID iParamID) const
Definition: GUIState.cpp:188
Definition: Clock.h:22
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener)
Definition: GUIJmbParameter.h:304
GUIRawVstParam registerRawVstCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:65
GUIVstParam< T > registerVstParam(ParamID iParamID, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.h:79
std::unique_ptr< GUIRawVstParameter > __registerRawVstParameter(ParamID iParamID, Listener iListener)
Definition: GUIParamCxMgr.h:264
bool existsJmb(ParamID iParamID) const
Definition: GUIState.h:66
GUIJmbParam< T > registerJmbParam(ParamID iParamID, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.h:186
GUIJmbParam< T > registerJmbCallback(GUIJmbParam< T > &iParamDef, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:155
Definition: GUIVstParameter.h:238
std::unique_ptr< GUIRawVstParameter > getRawVstParameter(ParamID iParamID) const
Definition: GUIState.h:71
std::unordered_map< ParamID, std::unique_ptr< FObjectCx > > fParamCxs
Definition: GUIParamCxMgr.h:257
TParam && maybeInvokeCallback(TParam &&iParam, Parameters::ChangeCallback &iCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:245
GUIState * getGUIState() const
Definition: GUIParamCxMgr.h:208
std::unique_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition: GUIState.h:311
void unregisterAll()
Definition: GUIParamCxMgr.cpp:47
GUIRawVstParam registerRawVstParam(ParamID iParamID, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.h:56
Definition: GUIJmbParameter.h:237
bool existsJmb(ParamID iParamID) const
Definition: GUIParamCxMgr.h:40
void invokeAll()
Definition: GUIParamCxMgr.cpp:28
Definition: GUIJmbParameter.h:86
GUIVstParam< T > __registerVstParam(ParamID iParamID, Listener iListener)
Definition: GUIParamCxMgr.h:321
ParamID getParamID() const
Definition: GUIJmbParameter.h:248
std::function< void()> ChangeCallback
Definition: Parameters.h:57
bool existsVst(ParamID iParamID) const
Definition: GUIState.h:61
GUIJmbParam< T > registerJmbParam(GUIJmbParam< T > &iParamDef, Parameters::IChangeListener *iChangeListener)
Definition: GUIParamCxMgr.h:142
bool existsVst(ParamID iParamID) const
Definition: GUIParamCxMgr.h:35
Definition: GUIParamCxMgr.h:29
GUIVstParam< T > registerVstCallback(VstParam< T > const &iParamDef, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:115
GUIVstParam< T > registerVstCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:90
bool unregisterParam(ParamID iParamID)
Definition: GUIParamCxMgr.cpp:39
std::function< void(Param &)> ChangeCallback1
Definition: Parameters.h:63
void registerJmbCallback(GUIJmbParam< T > &iParamDef, Parameters::ChangeCallback1< GUIJmbParam< T >> iChangeCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:170
GUIVstParam< T > registerVstParam(VstParam< T > const &iParamDef, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.h:104
GUIParamCxMgr(GUIState *iGUIState)
Definition: GUIParamCxMgr.h:217
GUIJmbParam< T > registerJmbCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.h:198
GUIJmbParam< T > __registerJmbParam(ParamID iParamID, Listener iListener)
Definition: GUIParamCxMgr.h:342