Jamba  3.0.2
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  void toString(String128 oString)
170  {
171  fRawParameter->toString(oString);
172  }
173 
177  String toString()
178  {
179  return fRawParameter->toString();
180  }
181 
185  std::unique_ptr<Editor> edit()
186  {
187  return std::make_unique<Editor>(fRawParameter->edit(), fVstParamDef);
188  }
189 
195  std::unique_ptr<Editor> edit(ParamType iValue)
196  {
197  auto editor = edit();
198  editor->setValue(iValue);
199  return editor;
200  }
201 
205  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const
206  {
207  return fRawParameter->connect(iChangeListener);
208  }
209 
213  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const
214  {
215  return fRawParameter->connect(std::move(iChangeCallback));
216  }
217 
218 private:
219  std::shared_ptr<GUIRawVstParameter> fRawParameter;
220  std::shared_ptr<VstParamDef<T>> fVstParamDef;
221 };
222 
223 //------------------------------------------------------------------------
224 // GUIVstParam - wrapper to make writing the code much simpler and natural
225 //------------------------------------------------------------------------
232 template<typename T>
234 {
235 public:
236  GUIVstParam() : fPtr{nullptr} {}
237 
238  // move constructor
239  explicit GUIVstParam(std::unique_ptr<GUIVstParameter<T>> &&iPtr) : fPtr{std::move(iPtr)} {}
240 
241  // delete copy constructor
242  GUIVstParam(GUIVstParam<T> &iPtr) = delete;
243 
244  // move copy constructor
245  GUIVstParam(GUIVstParam<T> &&iPtr) noexcept : fPtr{std::move(iPtr.fPtr)} {}
246 
247  // move assignment constructor
248  GUIVstParam<T> &operator=(GUIVstParam<T> &&iPtr) noexcept { fPtr = std::move(iPtr.fPtr); return *this; }
249 
250  // exists
251  inline bool exists() const { return (bool) fPtr; }
252 
253  // getParamID
254  inline ParamID getParamID() const { return fPtr->getParamID(); }
255 
259  inline T getValue() const { return fPtr->getValue(); }
260 
264  inline ParamValue getNormalizedValue() const { return fPtr->getNormalizedValue(); }
265 
270  tresult setValue(T const &iValue) { return fPtr->setValue(iValue); }
271 
276  tresult setNormalizedValue(ParamValue const &iNormalizedValue) { return fPtr->setNormalizedValue(iNormalizedValue); }
277 
282  tresult copyValueFrom(GUIVstParam<T> const &iParam) { return setNormalizedValue(iParam.getNormalizedValue()); }
283 
287  void toString(String128 oString) { fPtr->toString(oString); }
288 
292  String toString() { return fPtr->toString(); }
293 
297  std::unique_ptr<typename GUIVstParameter<T>::Editor> edit() { return fPtr->edit(); }
298 
304  std::unique_ptr<typename GUIVstParameter<T>::Editor> edit(T const &iValue) { return fPtr->edit(iValue); }
305 
306  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
307  inline operator T() const { return fPtr->getValue(); } // NOLINT
308 
309  // allow to write param = 3 instead of param.setValue(3)
310  inline void operator=(T const &iValue) { fPtr->setValue(iValue); }
311 
312  // allow to write param1 == param2
313  inline bool operator==(const GUIVstParam<T> &rhs) const { return fPtr->getNormalizedValue() == rhs.fPtr->getNormalizedValue(); }
314 
315  // allow to write param1 != param2
316  inline bool operator!=(const GUIVstParam &rhs) const { return fPtr->getNormalizedValue() != rhs.fPtr->getNormalizedValue(); }
317 
321  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { return fPtr->connect(iChangeListener); }
322 
326  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { return fPtr->connect(std::move(iChangeCallback)); }
327 
328 private:
329  std::unique_ptr<GUIVstParameter<T>> fPtr;
330 };
331 
332 //------------------------------------------------------------------------
333 // shortcut notations
334 //------------------------------------------------------------------------
335 template<typename T>
336 using GUIVstParamEditor = std::unique_ptr<typename GUIVstParameter<T>::Editor>;
337 
340 
341 }
342 }
343 }
344 }
345 
346 #endif // __PONGASOFT_VST_GUI_PARAMETER_H__
T getValue() const
Definition: GUIVstParameter.h:259
std::shared_ptr< VstParamDef< T > > fVstParamDef
Definition: GUIVstParameter.h:106
tresult setValue(ParamType const &iValue)
Definition: GUIVstParameter.h:70
std::shared_ptr< VstParamDef< T > > fVstParamDef
Definition: GUIVstParameter.h:220
bool operator==(const GUIVstParam< T > &rhs) const
Definition: GUIVstParameter.h:313
GUIVstParam(GUIVstParam< T > &&iPtr) noexcept
Definition: GUIVstParameter.h:245
tresult commit()
Definition: GUIVstParameter.h:79
GUIVstParam(std::unique_ptr< GUIVstParameter< T >> &&iPtr)
Definition: GUIVstParameter.h:239
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:329
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:297
String toString()
Definition: GUIVstParameter.h:177
Definition: GUIVstParameter.h:233
ParamType getValue() const
Definition: GUIVstParameter.h:135
tresult commit(ParamType const &iValue)
Definition: GUIVstParameter.h:89
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:264
ParamID getParamID() const
Definition: GUIVstParameter.h:254
GUIVstParam< T > & operator=(GUIVstParam< T > &&iPtr) noexcept
Definition: GUIVstParameter.h:248
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:336
tresult setValue(ParamType const &iValue)
Definition: GUIVstParameter.h:152
tresult setValue(T const &iValue)
Definition: GUIVstParameter.h:270
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIVstParameter.h:205
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:326
tresult rollback()
Definition: GUIVstParameter.h:99
tresult copyValueFrom(GUIVstParam< T > const &iParam)
Definition: GUIVstParameter.h:282
std::unique_ptr< Editor > edit(ParamType iValue)
Definition: GUIVstParameter.h:195
std::shared_ptr< GUIRawVstParameter > fRawParameter
Definition: GUIVstParameter.h:219
std::function< void()> ChangeCallback
Definition: Parameters.h:57
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:213
Definition: GUIVstParameter.h:34
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Definition: GUIVstParameter.h:161
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Definition: GUIVstParameter.h:276
bool exists() const
Definition: GUIVstParameter.h:251
std::unique_ptr< typename GUIVstParameter< T >::Editor > edit(T const &iValue)
Definition: GUIVstParameter.h:304
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIVstParameter.h:321
GUIVstParam()
Definition: GUIVstParameter.h:236
void toString(String128 oString)
Definition: GUIVstParameter.h:287
Definition: ParamDef.h:129
void operator=(T const &iValue)
Definition: GUIVstParameter.h:310
void toString(String128 oString)
Definition: GUIVstParameter.h:169
std::unique_ptr< Editor > edit()
Definition: GUIVstParameter.h:185
~GUIVstParameter()
Definition: GUIVstParameter.h:121
bool operator!=(const GUIVstParam &rhs) const
Definition: GUIVstParameter.h:316
String toString()
Definition: GUIVstParameter.h:292
ParamID getParamID() const
Definition: GUIVstParameter.h:127