Jamba C++ API  4.5.0
GUIVstParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 #pragma once
19 
20 #include "IGUIParameter.h"
21 #include "GUIRawVstParameter.h"
23 #include <pongasoft/VST/ParamDef.h>
25 
27 
31 template<typename T>
32 class GUIVstParameter : public ITGUIParameter<T>
33 {
34 public:
35  using ParamType = T;
37 
38 public:
52  class Editor : public EditorType
53  {
54  public:
55  inline explicit Editor(GUIRawVstParamEditor iRawEditor,
56  std::shared_ptr<IParamConverter<T>> iConverter) :
57  fRawEditor{std::move(iRawEditor)},
58  fConverter{std::move(iConverter)}
59  {
60  }
61 
62  ~Editor() override { rollback(); }
63 
64  // disabling copy
65  Editor(Editor const &) = delete;
66  Editor& operator=(Editor const &) = delete;
67 
71  tresult setValue(ParamType const &iValue) override
72  {
73  return fRawEditor->setValue(fConverter->normalize(iValue));
74  }
75 
80  bool updateValue(ParamType const &iValue) override
81  {
82  return fRawEditor->updateValue(fConverter->normalize(iValue));
83  }
84 
85  /*
86  * Call when you are done with the modifications.
87  * This has no effect if rollback() has already been called
88  */
89  tresult commit() override
90  {
91  return fRawEditor->commit();
92  }
93 
94  // importing superclass commit methods
95  using EditorType::commit;
96 
101  tresult rollback() override
102  {
103  return fRawEditor->rollback();
104  }
105 
106  private:
108  std::shared_ptr<IParamConverter<T>> fConverter;
109  };
110 
111 public:
112  // Constructor
113  GUIVstParameter(std::shared_ptr<GUIRawVstParameter> iRawParameter,
114  std::shared_ptr<IParamConverter<T>> iConverter) :
115  fRawParameter{std::move(iRawParameter)},
116  fConverter{std::move(iConverter)}
117  {
118  DCHECK_NOTNULL_F(fRawParameter.get());
119  DCHECK_NOTNULL_F(fConverter.get());
120  // DLOG_F(INFO, "VSTParameter::VSTParameter(%d)", fRawParameter->getParamID());
121  }
122 
123  // Destructor
125  {
126  // DLOG_F(INFO, "VSTParameter::~VSTParameter(%d)", fRawParameter->getParamID());
127  }
128 
129  // getParamID
130  ParamID getParamID() const override
131  {
132  return fRawParameter->getParamID();
133  }
134 
135  // accessValue
136  tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
137  {
138  iGetter(getValue());
139  return kResultOk;
140  }
141 
146  {
147  return fConverter->denormalize(fRawParameter->getValue());
148  }
149 
153  ParamValue getNormalizedValue() const
154  {
155  return fRawParameter->getValue();
156  }
157 
163  bool update(ParamType const &iValue) override
164  {
165  // Implementation note: because ParamType may not define `operator!=` we use the normalized value instead
166  auto currentNormalizedValue = getNormalizedValue();
167  auto newNormalizedValued = fConverter->normalize(iValue);
168  if(currentNormalizedValue != newNormalizedValued)
169  {
170  if(setNormalizedValue(newNormalizedValued) == kResultOk)
171  return true;
172  }
173  return false;
174  }
175 
180  tresult setValue(ParamType const &iValue) override
181  {
182  return fRawParameter->setValue(fConverter->normalize(iValue));
183  }
184 
189  tresult setNormalizedValue(ParamValue const &iNormalizedValue)
190  {
191  return fRawParameter->setValue(iNormalizedValue);
192  }
193 
196  tresult resetToDefault() override
197  {
198  return fRawParameter->resetToDefault();
199  }
200 
204  inline int32 getStepCount() const override { return fConverter->getStepCount(); }
205 
209  void toString(String128 oString)
210  {
211  fRawParameter->toString(oString);
212  }
213 
217  String toString()
218  {
219  return fRawParameter->toString();
220  }
221 
222  // toUTF8String
223  std::string toUTF8String(int32 iPrecision) const override
224  {
225  return fConverter->toString(getValue(), iPrecision);
226  }
227 
231  std::unique_ptr<EditorType> edit() override
232  {
233  return std::make_unique<Editor>(fRawParameter->edit(), fConverter);
234  }
235 
240 
244  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
245  {
246  return fRawParameter->connect(iChangeListener);
247  }
248 
252  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
253  {
254  return fRawParameter->connect(std::move(iChangeCallback));
255  }
256 
257  // asDiscreteParameter
258  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override
259  {
260  return fRawParameter->asDiscreteParameter(iStepCount);
261  }
262 
263 private:
264  std::shared_ptr<GUIRawVstParameter> fRawParameter;
265  std::shared_ptr<IParamConverter<T>> fConverter;
266 };
267 
268 //------------------------------------------------------------------------
269 // GUIVstParam - wrapper to make writing the code much simpler and natural
270 //------------------------------------------------------------------------
277 template<typename T>
278 class GUIVstParam: public Utils::Operators::Dereferenceable<GUIVstParam<T>>
279 {
280 public:
281  using ParamType = T;
283  using Editor = std::unique_ptr<EditorType>;
284 
285 public:
290  class Value {
291  public:
292  constexpr T const *operator ->() const { return &fValue; }
293  friend class GUIVstParam<T>;
294  private:
295  explicit Value(T const &iValue) : fValue{iValue} {}
297  };
298 
299 public:
300  // Constructor
301  GUIVstParam(std::shared_ptr<GUIVstParameter<T>> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
302  fPtr{std::move(iPtr)}
303  {}
304 
306  GUIVstParam<T> &operator=(GUIVstParam<T> const &iOther) = default;
307 
308  // exists
309  inline bool exists() const { return (bool) fPtr; }
310 
311  // getParamID
312  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
313 
317  inline T getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
318 
320  inline T value() const { DCHECK_F(exists()); return fPtr->getValue(); }
321 
325  inline ParamValue getNormalizedValue() const { DCHECK_F(exists()); return fPtr->getNormalizedValue(); }
326 
332  bool update(T const &iValue) { DCHECK_F(exists()); return fPtr->update(iValue); }
333 
338  tresult setValue(T const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
339 
344  tresult setNormalizedValue(ParamValue const &iNormalizedValue) { DCHECK_F(exists()); return fPtr->setNormalizedValue(iNormalizedValue); }
345 
348  inline tresult resetToDefault() { DCHECK_F(exists()); return fPtr->resetToDefault(); }
349 
354  template<typename V>
355  tresult copyValueFrom(GUIVstParam<V> const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getNormalizedValue()); }
356 
360  tresult copyValueFrom(GUIRawVstParam const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getValue()); }
361 
365  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
366 
370  void toString(String128 oString) { DCHECK_F(exists()); fPtr->toString(oString); }
371 
375  String toString() { DCHECK_F(exists()); return fPtr->toString(); }
376 
379  inline std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
380 
384  Editor edit() { DCHECK_F(exists()); return fPtr->edit(); }
385 
391  Editor edit(T const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
392 
394  constexpr T operator *() const { DCHECK_F(exists()); return fPtr->getValue(); }
395 
397  constexpr Value operator ->() const { DCHECK_F(exists()); return Value{fPtr->getValue()}; }
398 
400  [[deprecated("Since 4.1.0 - use operator* or .value() instead (ex: if(*param) {...} or if(param.value()) {...}")]]
401  inline operator T() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
402 
404  inline GUIVstParam<T> &operator=(T const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
405 
407 // friend constexpr bool operator==(GUIVstParam<T> const &lhs, GUIVstParam<T> const &rhs) { return lhs.fPtr->getNormalizedValue() == rhs.fPtr->getNormalizedValue(); }
408 
412  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
413 
417  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
418 
419 private:
420  std::shared_ptr<GUIVstParameter<T>> fPtr;
421 };
422 
423 
424 //------------------------------------------------------------------------
425 // GUIRawVstParameter::asVstParameter
426 //------------------------------------------------------------------------
427 template<typename T>
428 std::shared_ptr<GUIVstParameter<T>> GUIRawVstParameter::asVstParameter()
429 {
430  auto vstParamDef = std::dynamic_pointer_cast<VstParamDef<T>>(fParamDef);
431  if(vstParamDef && vstParamDef->fConverter)
432  return std::make_shared<GUIVstParameter<T>>(std::dynamic_pointer_cast<GUIRawVstParameter>(shared_from_this()),
433  vstParamDef->fConverter);
434  else
435  return nullptr;
436 }
437 
438 //------------------------------------------------------------------------
439 // shortcut notations
440 //------------------------------------------------------------------------
441 template<typename T>
442 using GUIVstParamEditor = std::unique_ptr<typename GUIVstParameter<T>::EditorType>;
443 
446 
447 }
typename ITGUIParameter< T >::ITEditor EditorType
Definition: GUIVstParameter.h:36
T getValue() const
Definition: GUIVstParameter.h:317
T value() const
Synonym to getValue()
Definition: GUIVstParameter.h:320
tresult rollback() override
Call this if you want to revert to the original value of the parameter (when the editor is created).
Definition: GUIVstParameter.h:101
tresult setValue(ParamType const &iValue) override
Sets the value of this parameter.
Definition: GUIVstParameter.h:180
ParamType getValue() const
Definition: GUIVstParameter.h:145
T ParamType
Definition: GUIVstParameter.h:281
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition: GUIVstParameter.h:163
int32 getStepCount() const
Definition: GUIVstParameter.h:365
std::shared_ptr< IParamConverter< T > > fConverter
Definition: GUIVstParameter.h:265
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIRawVstParameter.h:265
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:153
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition: Parameters.h:55
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition: IGUIParameter.h:33
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition: GUIVstParameter.h:344
Editor edit(T const &iValue)
Shortcut to create an editor and set the value to it.
Definition: GUIVstParameter.h:391
~Editor() override
Definition: GUIVstParameter.h:62
bool exists() const
Definition: GUIVstParameter.h:309
std::unique_ptr< EditorType > edit() override
Definition: GUIVstParameter.h:231
Value(T const &iValue)
Definition: GUIVstParameter.h:295
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIVstParameter.h:370
std::shared_ptr< GUIVstParameter< T > > fPtr
Definition: GUIVstParameter.h:420
GUIVstParam< T > & operator=(T const &iValue)
Allow to write param = 3.0.
Definition: GUIVstParameter.h:404
tresult copyValueFrom(GUIVstParam< V > const &iParam)
Shortcut to copy the value from another param to this one.
Definition: GUIVstParameter.h:355
std::shared_ptr< RawVstParamDef > fParamDef
Definition: GUIRawVstParameter.h:255
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition: Operators.h:54
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:182
tresult setValue(T const &iValue)
Sets the value of this parameter.
Definition: GUIVstParameter.h:338
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIVstParameter.h:278
bool update(T const &iValue)
Update the parameter with a value.
Definition: GUIVstParameter.h:332
std::unique_ptr< typename GUIVstParameter< T >::EditorType > GUIVstParamEditor
Definition: GUIVstParameter.h:442
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Allow to write param1 == param2.
Definition: GUIVstParameter.h:412
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition: GUIVstParameter.h:258
Definition: GUIState.h:37
tresult setValue(ParamType const &iValue) override
Change the value of the parameter.
Definition: GUIVstParameter.h:71
bool updateValue(ParamType const &iValue) override
Change the value of the parameter.
Definition: GUIVstParameter.h:80
~GUIVstParameter()
Definition: GUIVstParameter.h:124
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIVstParameter.h:209
tresult resetToDefault() override
Resets the parameter to its default value.
Definition: GUIVstParameter.h:196
The purpose of this class is to copy the value so that it can be accessed via -> thus allowing to wri...
Definition: GUIVstParameter.h:290
GUIVstParam(std::shared_ptr< GUIVstParameter< T >> iPtr=nullptr)
Definition: GUIVstParameter.h:301
Editor(GUIRawVstParamEditor iRawEditor, std::shared_ptr< IParamConverter< T >> iConverter)
Definition: GUIVstParameter.h:55
GUIRawVstParamEditor fRawEditor
Definition: GUIVstParameter.h:107
std::shared_ptr< GUIRawVstParameter > fRawParameter
Definition: GUIVstParameter.h:264
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIVstParameter.h:244
ParamValue getValue() const
Definition: GUIRawVstParameter.h:290
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition: GUIVstParameter.h:136
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIVstParameter.h:252
Editor edit()
Definition: GUIVstParameter.h:384
std::string toUTF8String(int32 iPrecision) const
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIVstParameter.h:379
constexpr Value operator ->() const
allow writing param->x to access the underlying value when T is a struct or class
Definition: GUIVstParameter.h:397
std::unique_ptr< EditorType > Editor
Definition: GUIVstParameter.h:283
This class wraps a GUIRawVstParameter to deal with any type T.
Definition: GUIRawVstParameter.h:31
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIVstParameter.h:223
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition: GUIVstParameter.h:189
typename GUIVstParameter< T >::ITEditor EditorType
Definition: GUIVstParameter.h:282
GUIVstParameter(std::shared_ptr< GUIRawVstParameter > iRawParameter, std::shared_ptr< IParamConverter< T >> iConverter)
Definition: GUIVstParameter.h:113
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:375
GUIVstParam< T > & operator=(GUIVstParam< T > const &iOther)=default
Assignment operator: fMyParam = registerParam(...);
String toString()
Returns a string representation of this parameter.
Definition: GUIVstParameter.h:217
ParamID getParamID() const
Definition: GUIVstParameter.h:312
Typed parameter definition.
Definition: ParamDef.h:176
tresult copyValueFrom(GUIRawVstParam const &iParam)
Shortcut to copy the value from another param to this one (raw value)
Definition: GUIVstParameter.h:360
T ParamType
Definition: GUIVstParameter.h:35
tresult resetToDefault()
Resets the param to its default value.
Definition: GUIVstParameter.h:348
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:236
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:325
Wrapper to edit a single parameter.
Definition: GUIVstParameter.h:52
constexpr T const * operator ->() const
Definition: GUIVstParameter.h:292
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition: GUIVstParameter.h:428
T fValue
Definition: GUIVstParameter.h:296
tresult commit() override
Definition: GUIVstParameter.h:89
std::shared_ptr< IParamConverter< T > > fConverter
Definition: GUIVstParameter.h:108
String toString()
Returns a string representation of this parameter.
Definition: GUIVstParameter.h:375
int32 getStepCount() const override
Definition: GUIVstParameter.h:204
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIVstParameter.h:130
constexpr T operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: GUIVstParameter.h:394
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:417
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition: ParamConverters.h:53