Jamba  3.2.0
GUIVstParameter.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_PARAMETER_H__
19 #define __PONGASOFT_VST_GUI_PARAMETER_H__
20 
21 #include "GUIRawVstParameter.h"
23 #include <pongasoft/VST/ParamDef.h>
24 
25 namespace pongasoft {
26 namespace VST {
27 namespace GUI {
28 namespace Params {
29 
33 template<typename T>
35 {
36 public:
37  using ParamType = T;
38 
39 public:
53  class Editor
54  {
55  public:
56  inline explicit Editor(std::unique_ptr<GUIRawVstParameter::Editor> iRawEditor,
57  std::shared_ptr<VstParamDef<T>> iVstParamDef) :
58  fRawEditor{std::move(iRawEditor)},
59  fVstParamDef{std::move(iVstParamDef)}
60  {
61  }
62 
63  // disabling copy
64  Editor(Editor const &) = delete;
65  Editor& operator=(Editor const &) = delete;
66 
70  inline tresult setValue(ParamType const &iValue)
71  {
72  return fRawEditor->setValue(fVstParamDef->normalize(iValue));
73  }
74 
75  /*
76  * Call when you are done with the modifications.
77  * This has no effect if rollback() has already been called
78  */
79  inline tresult commit()
80  {
81  return fRawEditor->commit();
82  }
83 
84  /*
85  * Shortcut to set the value prior to commit
86  * Call when you are done with the modifications.
87  * This has no effect if rollback() has already been called
88  */
89  inline tresult commit(ParamType const &iValue)
90  {
91  setValue(iValue);
92  return commit();
93  }
94 
99  inline tresult rollback()
100  {
101  return fRawEditor->rollback();
102  }
103 
104  private:
105  std::unique_ptr<GUIRawVstParameter::Editor> fRawEditor;
106  std::shared_ptr<VstParamDef<T>> fVstParamDef;
107  };
108 
109 public:
110  // Constructor
111  GUIVstParameter(std::shared_ptr<GUIRawVstParameter> iRawParameter,
112  std::shared_ptr<VstParamDef<T>> iVstParamDef) :
113  fRawParameter{std::move(iRawParameter)},
114  fVstParamDef{std::move(iVstParamDef)}
115  {
116  DCHECK_NOTNULL_F(fRawParameter.get());
117  // DLOG_F(INFO, "VSTParameter::VSTParameter(%d)", fRawParameter->getParamID());
118  }
119 
120  // Destructor
122  {
123  // DLOG_F(INFO, "VSTParameter::~VSTParameter(%d)", fRawParameter->getParamID());
124  }
125 
126  // getParamID
127  ParamID getParamID() const
128  {
129  return fRawParameter->getParamID();
130  }
131 
136  {
137  return fVstParamDef->denormalize(fRawParameter->getValue());
138  }
139 
143  ParamValue getNormalizedValue() const
144  {
145  return fRawParameter->getValue();
146  }
147 
152  tresult setValue(ParamType const &iValue)
153  {
154  return fRawParameter->setValue(fVstParamDef->normalize(iValue));
155  }
156 
161  tresult setNormalizedValue(ParamValue const &iNormalizedValue)
162  {
163  return fRawParameter->setValue(iNormalizedValue);
164  }
165 
169  inline int32 getStepCount() const { return fVstParamDef->fStepCount; }
170 
174  void toString(String128 oString)
175  {
176  fRawParameter->toString(oString);
177  }
178 
182  String toString()
183  {
184  return fRawParameter->toString();
185  }
186 
190  std::unique_ptr<Editor> edit()
191  {
192  return std::make_unique<Editor>(fRawParameter->edit(), fVstParamDef);
193  }
194 
200  std::unique_ptr<Editor> edit(ParamType iValue)
201  {
202  auto editor = edit();
203  editor->setValue(iValue);
204  return editor;
205  }
206 
210  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const
211  {
212  return fRawParameter->connect(iChangeListener);
213  }
214 
218  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const
219  {
220  return fRawParameter->connect(std::move(iChangeCallback));
221  }
222 
223 private:
224  std::shared_ptr<GUIRawVstParameter> fRawParameter;
225  std::shared_ptr<VstParamDef<T>> fVstParamDef;
226 };
227 
228 //------------------------------------------------------------------------
229 // GUIVstParam - wrapper to make writing the code much simpler and natural
230 //------------------------------------------------------------------------
237 template<typename T>
239 {
240 public:
241  GUIVstParam() : fPtr{nullptr} {}
242 
243  // move constructor
244  explicit GUIVstParam(std::unique_ptr<GUIVstParameter<T>> &&iPtr) : fPtr{std::move(iPtr)} {}
245 
246  // delete copy constructor
247  GUIVstParam(GUIVstParam<T> &iPtr) = delete;
248 
249  // move copy constructor
250  GUIVstParam(GUIVstParam<T> &&iPtr) noexcept : fPtr{std::move(iPtr.fPtr)} {}
251 
252  // move assignment constructor
253  GUIVstParam<T> &operator=(GUIVstParam<T> &&iPtr) noexcept { fPtr = std::move(iPtr.fPtr); return *this; }
254 
255  // exists
256  inline bool exists() const { return (bool) fPtr; }
257 
258  // getParamID
259  inline ParamID getParamID() const { return fPtr->getParamID(); }
260 
264  inline T getValue() const { return fPtr->getValue(); }
265 
269  inline ParamValue getNormalizedValue() const { return fPtr->getNormalizedValue(); }
270 
275  tresult setValue(T const &iValue) { return fPtr->setValue(iValue); }
276 
281  tresult setNormalizedValue(ParamValue const &iNormalizedValue) { return fPtr->setNormalizedValue(iNormalizedValue); }
282 
287  tresult copyValueFrom(GUIVstParam<T> const &iParam) { return setNormalizedValue(iParam.getNormalizedValue()); }
288 
292  inline int32 getStepCount() const { return fPtr->getStepCount(); }
293 
297  void toString(String128 oString) { fPtr->toString(oString); }
298 
302  String toString() { return fPtr->toString(); }
303 
307  std::unique_ptr<typename GUIVstParameter<T>::Editor> edit() { return fPtr->edit(); }
308 
314  std::unique_ptr<typename GUIVstParameter<T>::Editor> edit(T const &iValue) { return fPtr->edit(iValue); }
315 
316  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
317  inline operator T() const { return fPtr->getValue(); } // NOLINT
318 
319  // allow to write param = 3 instead of param.setValue(3)
320  inline void operator=(T const &iValue) { fPtr->setValue(iValue); }
321 
322  // allow to write param1 == param2
323  inline bool operator==(const GUIVstParam<T> &rhs) const { return fPtr->getNormalizedValue() == rhs.fPtr->getNormalizedValue(); }
324 
325  // allow to write param1 != param2
326  inline bool operator!=(const GUIVstParam &rhs) const { return fPtr->getNormalizedValue() != rhs.fPtr->getNormalizedValue(); }
327 
331  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { return fPtr->connect(iChangeListener); }
332 
336  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { return fPtr->connect(std::move(iChangeCallback)); }
337 
338 private:
339  std::unique_ptr<GUIVstParameter<T>> fPtr;
340 };
341 
342 //------------------------------------------------------------------------
343 // shortcut notations
344 //------------------------------------------------------------------------
345 template<typename T>
346 using GUIVstParamEditor = std::unique_ptr<typename GUIVstParameter<T>::Editor>;
347 
350 
351 }
352 }
353 }
354 }
355 
356 #endif // __PONGASOFT_VST_GUI_PARAMETER_H__
T getValue() const
Definition: GUIVstParameter.h:264
std::shared_ptr< VstParamDef< T > > fVstParamDef
Definition: GUIVstParameter.h:106
tresult setValue(ParamType const &iValue)
Definition: GUIVstParameter.h:70
int32 getStepCount() const
Definition: GUIVstParameter.h:292
std::shared_ptr< VstParamDef< T > > fVstParamDef
Definition: GUIVstParameter.h:225
bool operator==(const GUIVstParam< T > &rhs) const
Definition: GUIVstParameter.h:323
GUIVstParam(GUIVstParam< T > &&iPtr) noexcept
Definition: GUIVstParameter.h:250
tresult commit()
Definition: GUIVstParameter.h:79
GUIVstParam(std::unique_ptr< GUIVstParameter< T >> &&iPtr)
Definition: GUIVstParameter.h:244
std::unique_ptr< GUIRawVstParameter::Editor > fRawEditor
Definition: GUIVstParameter.h:105
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:143
Definition: Clock.h:22
std::unique_ptr< GUIVstParameter< T > > fPtr
Definition: GUIVstParameter.h:339
Editor(std::unique_ptr< GUIRawVstParameter::Editor > iRawEditor, std::shared_ptr< VstParamDef< T >> iVstParamDef)
Definition: GUIVstParameter.h:56
T ParamType
Definition: GUIVstParameter.h:37
std::unique_ptr< typename GUIVstParameter< T >::Editor > edit()
Definition: GUIVstParameter.h:307
String toString()
Definition: GUIVstParameter.h:182
int32 getStepCount() const
Definition: GUIVstParameter.h:169
Definition: GUIVstParameter.h:238
ParamType getValue() const
Definition: GUIVstParameter.h:135
tresult commit(ParamType const &iValue)
Definition: GUIVstParameter.h:89
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:269
ParamID getParamID() const
Definition: GUIVstParameter.h:259
GUIVstParam< T > & operator=(GUIVstParam< T > &&iPtr) noexcept
Definition: GUIVstParameter.h:253
GUIVstParameter(std::shared_ptr< GUIRawVstParameter > iRawParameter, std::shared_ptr< VstParamDef< T >> iVstParamDef)
Definition: GUIVstParameter.h:111
std::unique_ptr< typename GUIVstParameter< T >::Editor > GUIVstParamEditor
Definition: GUIVstParameter.h:346
tresult setValue(ParamType const &iValue)
Definition: GUIVstParameter.h:152
tresult setValue(T const &iValue)
Definition: GUIVstParameter.h:275
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIVstParameter.h:210
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:336
tresult rollback()
Definition: GUIVstParameter.h:99
tresult copyValueFrom(GUIVstParam< T > const &iParam)
Definition: GUIVstParameter.h:287
std::unique_ptr< Editor > edit(ParamType iValue)
Definition: GUIVstParameter.h:200
std::shared_ptr< GUIRawVstParameter > fRawParameter
Definition: GUIVstParameter.h:224
std::function< void()> ChangeCallback
Definition: Parameters.h:57
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:218
Definition: GUIVstParameter.h:34
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Definition: GUIVstParameter.h:161
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Definition: GUIVstParameter.h:281
bool exists() const
Definition: GUIVstParameter.h:256
std::unique_ptr< typename GUIVstParameter< T >::Editor > edit(T const &iValue)
Definition: GUIVstParameter.h:314
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIVstParameter.h:331
GUIVstParam()
Definition: GUIVstParameter.h:241
void toString(String128 oString)
Definition: GUIVstParameter.h:297
Definition: ParamDef.h:129
void operator=(T const &iValue)
Definition: GUIVstParameter.h:320
void toString(String128 oString)
Definition: GUIVstParameter.h:174
std::unique_ptr< Editor > edit()
Definition: GUIVstParameter.h:190
~GUIVstParameter()
Definition: GUIVstParameter.h:121
bool operator!=(const GUIVstParam &rhs) const
Definition: GUIVstParameter.h:326
String toString()
Definition: GUIVstParameter.h:302
ParamID getParamID() const
Definition: GUIVstParameter.h:127