Jamba C++ API  4.3.0
GUIRawVstParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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"
23 #include "VstParameters.h"
24 #include "GUIParamCx.h"
25 
26 #include <string>
27 
29 
30 template<typename T>
32 
39 class GUIRawVstParameter : public ITGUIParameter<ParamValue>
40 {
41 public:
42  using ParamType = ParamValue;
44 
45 public:
59  class Editor : public EditorType
60  {
61  public:
62  Editor(ParamID iParamID, VstParametersSPtr iVstParameters);
63 
64  ~Editor() override { rollback(); }
65 
66  // disabling copy
67  Editor(Editor const &) = delete;
68  Editor& operator=(Editor const &) = delete;
69 
73  tresult setValue(ParamValue const &iValue) override;
74 
79  bool updateValue(ParamValue const &iValue) override;
80 
81  /*
82  * Call when you are done with the modifications.
83  * This has no effect if rollback() has already been called
84  */
85  tresult commit() override;
86 
87  // importing superclass commit methods
88  using EditorType::commit;
89 
94  tresult rollback() override;
95 
96  private:
97  ParamID fParamID;
99 
100  ParamValue fInitialParamValue;
102  };
103 
104 public:
105  // Constructor
106  GUIRawVstParameter(ParamID iParamID,
107  VstParametersSPtr iVstParameters,
108  std::shared_ptr<RawVstParamDef> iParamDef);
109 
110  // Destructor
111  ~GUIRawVstParameter() = default;
112 // {
113 // DLOG_F(INFO, "RawParameter::~RawParameter(%d)", fParamID);
114 // }
115 
116  // getParamID
117  inline ParamID getParamID() const override
118  {
119  return fParamID;
120  }
121 
122  // accessValue
123  tresult accessValue(ValueAccessor const &iGetter) const override
124  {
125  iGetter(getValue());
126  return kResultOk;
127  }
128 
132  inline ParamValue getValue() const
133  {
134  return fVstParameters->getParamNormalized(fParamID);
135  }
136 
140  inline int32 getStepCount() const override
141  {
142  auto parameterInfo = fVstParameters->getParameterInfo(fParamID);
143  if(parameterInfo)
144  return parameterInfo->stepCount;
145  else
146  return 0;
147  }
148 
152  void toString(String128 oString)
153  {
154  fParamDef->toString(getValue(), oString);
155  }
156 
160  String toString()
161  {
162  String128 s;
163  toString(s);
164  return String(s);
165  }
166 
167  // toUTF8String
168  std::string toUTF8String(int32 iPrecision) const override
169  {
170  return fParamDef->toUTF8String(getValue(), iPrecision);
171  }
172 
178  bool update(ParamValue const &iValue) override
179  {
180  auto const previousValue = getValue();
181  if(previousValue != iValue)
182  {
183  setValue(iValue);
184  return true;
185  }
186  return false;
187  }
188 
193  inline tresult setValue(ParamValue const &iValue) override
194  {
195  Editor editor(fParamID, fVstParameters);
196  editor.setValue(iValue);
197  return editor.commit();
198  }
199 
203  std::unique_ptr<EditorType> edit() override
204  {
205  return std::make_unique<Editor>(fParamID, fVstParameters);
206  }
207 
212 
216  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
217  {
218  return fVstParameters->connect(fParamID, iChangeListener);
219  }
220 
224  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
225  {
226  return fVstParameters->connect(fParamID, std::move(iChangeCallback));
227  }
228 
233  template<typename T>
234  std::shared_ptr<GUIVstParameter<T>> asVstParameter();
235 
243  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
244 
245 private:
246  ParamID fParamID;
248  std::shared_ptr<RawVstParamDef> fParamDef;
249 };
250 
251 //-------------------------------------------------------------------------------
252 // GUIRawVstParam - wrapper to make writing the code much simpler and natural
253 //-------------------------------------------------------------------------------
258 class GUIRawVstParam : public Utils::Operators::Dereferenceable<GUIRawVstParam>
259 {
260 public:
261  // Constructor
262  GUIRawVstParam(std::shared_ptr<GUIRawVstParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
263  fPtr{std::move(iPtr)}
264  {}
265 
267  GUIRawVstParam &operator=(GUIRawVstParam const &iOther) = default;
268 
269  // exists
270  inline bool exists() const { return (bool) fPtr; }
271 
272  // getParamID
273  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
274 
278  inline ParamValue getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
279 
281  inline ParamValue value() const { DCHECK_F(exists()); return fPtr->getValue(); }
282 
288  bool update(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->update(iValue); }
289 
294  tresult setValue(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
295 
299  tresult copyValueFrom(GUIRawVstParam const &iParam) { DCHECK_F(exists()); return setValue(iParam.getValue()); }
300 
304  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
305 
309  void toString(String128 oString) { fPtr->toString(oString); }
310 
314  String toString() { DCHECK_F(exists()); return fPtr->toString(); }
315 
319  std::unique_ptr<GUIRawVstParameter::EditorType> edit() { DCHECK_F(exists()); return fPtr->edit(); }
320 
326  std::unique_ptr<GUIRawVstParameter::EditorType> edit(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
327 
329  inline ParamValue operator *() const { DCHECK_F(exists()); return fPtr->getValue(); }
330 
332  [[deprecated("Since 4.1.0 - use operator* or .value() instead (ex: if(*param) {...} or if(param.value()) {...}")]]
333  inline operator ParamValue() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
334 
336  inline GUIRawVstParam &operator=(ParamValue const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
337 
341  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
342 
346  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
347 
348 private:
349  std::shared_ptr<GUIRawVstParameter> fPtr;
350 };
351 
352 //------------------------------------------------------------------------
353 // shortcut notations
354 //------------------------------------------------------------------------
355 using GUIRawVstParamEditor = std::unique_ptr<GUIRawVstParameter::EditorType>;
356 
357 }
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
This implementation will adapt this parameter to be interpreted as a discrete parameter.
Definition: GUIRawVstParameter.cpp:144
~Editor() override
Definition: GUIRawVstParameter.h:64
ParamValue value() const
Synonym to getValue()
Definition: GUIRawVstParameter.h:281
GUIRawVstParam & operator=(ParamValue const &iValue)
Allow to write param = 0.5.
Definition: GUIRawVstParameter.h:336
GUIRawVstParam(std::shared_ptr< GUIRawVstParameter > iPtr=nullptr)
Definition: GUIRawVstParameter.h:262
ParamID fParamID
Definition: GUIRawVstParameter.h:97
bool exists() const
Definition: GUIRawVstParameter.h:270
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIRawVstParameter.h:258
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
std::unique_ptr< GUIRawVstParameter::EditorType > edit(ParamValue const &iValue)
Shortcut to create an editor and set the value to it.
Definition: GUIRawVstParameter.h:326
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIRawVstParameter.h:346
bool updateValue(ParamValue const &iValue) override
Change the value of the parameter.
Definition: GUIRawVstParameter.cpp:54
ParamValue operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: GUIRawVstParameter.h:329
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIRawVstParameter.h:117
std::shared_ptr< RawVstParamDef > fParamDef
Definition: GUIRawVstParameter.h:248
GUIRawVstParameter(ParamID iParamID, VstParametersSPtr iVstParameters, std::shared_ptr< RawVstParamDef > iParamDef)
Definition: GUIRawVstParameter.cpp:97
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition: Operators.h:54
ParamValue fInitialParamValue
Definition: GUIRawVstParameter.h:100
std::function< void(ParamValue const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:178
ParamID fParamID
Definition: GUIRawVstParameter.h:246
tresult setValue(ParamValue const &iValue)
Sets the value of this parameter.
Definition: GUIRawVstParameter.h:294
Editor(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:26
bool fIsEditing
Definition: GUIRawVstParameter.h:101
ParamValue getValue() const
Definition: GUIRawVstParameter.h:132
String toString()
Returns a string representation of this parameter.
Definition: GUIRawVstParameter.h:314
Definition: GUIState.h:36
tresult rollback() override
Call this if you want to revert to the original value of the parameter (when the editor is created).
Definition: GUIRawVstParameter.cpp:82
tresult setValue(ParamValue const &iValue) override
Sets the value of this parameter.
Definition: GUIRawVstParameter.h:193
bool update(ParamValue const &iValue) override
Update the parameter with a value.
Definition: GUIRawVstParameter.h:178
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:98
bool update(ParamValue const &iValue)
Update the parameter with a value.
Definition: GUIRawVstParameter.h:288
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:247
GUIRawVstParam & operator=(GUIRawVstParam const &iOther)=default
Assignment operator: fMyParam = registerParam(...);
ITGUIParameter< ParamValue >::ITEditor EditorType
Definition: GUIRawVstParameter.h:43
ParamValue getValue() const
Definition: GUIRawVstParameter.h:278
std::unique_ptr< GUIRawVstParameter::EditorType > edit()
Definition: GUIRawVstParameter.h:319
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIRawVstParameter.h:152
This class wraps a GUIRawVstParameter to deal with any type T.
Definition: GUIRawVstParameter.h:31
ParamID getParamID() const
Definition: GUIRawVstParameter.h:273
int32 getStepCount() const override
Definition: GUIRawVstParameter.h:140
String toString()
Returns a string representation of this parameter.
Definition: GUIRawVstParameter.h:160
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:355
tresult commit() override
Definition: GUIRawVstParameter.cpp:68
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:95
ParamValue ParamType
Definition: GUIRawVstParameter.h:42
std::shared_ptr< GUIRawVstParameter > fPtr
Definition: GUIRawVstParameter.h:349
tresult accessValue(ValueAccessor const &iGetter) const override
API to access the underlying value.
Definition: GUIRawVstParameter.h:123
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIRawVstParameter.h:309
tresult setValue(ParamValue const &iValue) override
Change the value of the parameter.
Definition: GUIRawVstParameter.cpp:39
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition: GUIVstParameter.h:409
tresult copyValueFrom(GUIRawVstParam const &iParam)
Shortcut to copy the value from another param to this one.
Definition: GUIRawVstParameter.h:299
Wrapper to edit a single parameter.
Definition: GUIRawVstParameter.h:59
Encapsulates a vst parameter and how to access it (read/write) as well as how to "connect" to it in o...
Definition: GUIRawVstParameter.h:39
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIRawVstParameter.h:341
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIRawVstParameter.h:168
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIRawVstParameter.h:216
std::unique_ptr< EditorType > edit() override
Definition: GUIRawVstParameter.h:203
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIRawVstParameter.h:224
int32 getStepCount() const
Definition: GUIRawVstParameter.h:304