Jamba  3.1.0
GUIRawVstParameter.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_RAW_PARAMETER_H__
19 #define __PONGASOFT_VST_GUI_RAW_PARAMETER_H__
20 
22 #include "VstParameters.h"
23 #include "GUIParamCx.h"
24 
25 #include <string>
26 
27 namespace pongasoft {
28 namespace VST {
29 namespace GUI {
30 namespace Params {
31 
39 {
40 public:
54  class Editor
55  {
56  public:
57  Editor(ParamID iParamID, VstParametersSPtr iVstParameters);
58 
59  // disabling copy
60  Editor(Editor const &) = delete;
61  Editor& operator=(Editor const &) = delete;
62 
66  tresult setValue(ParamValue iValue);
67 
68  /*
69  * Call when you are done with the modifications.
70  * This has no effect if rollback() has already been called
71  */
72  tresult commit();
73 
74  /*
75  * Shortcut to set the value prior to commit
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(ParamValue iValue)
80  {
81  setValue(iValue);
82  return commit();
83  }
84 
89  tresult rollback();
90 
94  inline ~Editor()
95  {
96  // DLOG_F(INFO, "~RawParameter::Editor(%d)", fParamID);
97  rollback();
98  }
99 
100  private:
101  ParamID fParamID;
103 
104  ParamValue fInitialParamValue;
106  };
107 
108 public:
109  // Constructor
110  GUIRawVstParameter(ParamID iParamID, VstParametersSPtr iVstParameters);
111 
112  // Destructor
113  ~GUIRawVstParameter() = default;
114 // {
115 // DLOG_F(INFO, "RawParameter::~RawParameter(%d)", fParamID);
116 // }
117 
118  // getParamID
119  inline ParamID getParamID() const
120  {
121  return fParamID;
122  }
123 
127  inline ParamValue getValue() const
128  {
129  return fVstParameters->getParamNormalized(fParamID);
130  }
131 
135  inline int32 getStepCount() const { return fVstParameters->getParameterInfo(fParamID)->stepCount; }
136 
140  void toString(String128 oString)
141  {
142  auto parameter = fVstParameters->getParameterObject(fParamID);
143  if(parameter)
144  parameter->toString(getValue(), oString);
145  }
146 
150  String toString()
151  {
152  String128 s;
153  toString(s);
154  return String(s);
155  }
156 
161  inline tresult setValue(ParamValue iValue)
162  {
163  Editor editor(fParamID, fVstParameters);
164  editor.setValue(iValue);
165  return editor.commit();
166  }
167 
171  std::unique_ptr<Editor> edit()
172  {
173  return std::make_unique<Editor>(fParamID, fVstParameters);
174  }
175 
181  std::unique_ptr<Editor> edit(ParamValue iValue)
182  {
183  auto editor = edit();
184  editor->setValue(iValue);
185  return editor;
186  }
187 
191  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const
192  {
193  return fVstParameters->connect(fParamID, iChangeListener);
194  }
195 
199  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const
200  {
201  return fVstParameters->connect(fParamID, std::move(iChangeCallback));
202  }
203 
204 private:
205  ParamID fParamID;
207 };
208 
209 //-------------------------------------------------------------------------------
210 // GUIRawVstParam - wrapper to make writing the code much simpler and natural
211 //-------------------------------------------------------------------------------
217 {
218 public:
219  GUIRawVstParam() : fPtr{nullptr} {}
220 
221  // move constructor
222  explicit GUIRawVstParam(std::unique_ptr<GUIRawVstParameter> &&iPtr) : fPtr{std::move(iPtr)} {}
223 
224  // delete copy constructor
225  GUIRawVstParam(GUIRawVstParam &iPtr) = delete;
226 
227  // move copy constructor
228  GUIRawVstParam(GUIRawVstParam &&iPtr) noexcept : fPtr{std::move(iPtr.fPtr)} {}
229 
230  // move assignment constructor
231  GUIRawVstParam &operator=(GUIRawVstParam &&iPtr) noexcept { fPtr = std::move(iPtr.fPtr); return *this; }
232 
233  // exists
234  inline bool exists() const { return (bool) fPtr; }
235 
236  // getParamID
237  inline ParamID getParamID() const { return fPtr->getParamID(); }
238 
242  inline ParamValue getValue() const { return fPtr->getValue(); }
243 
248  tresult setValue(ParamValue const &iValue) { return fPtr->setValue(iValue); }
249 
253  tresult copyValueFrom(GUIRawVstParam const &iParam) { return setValue(iParam.getValue()); }
254 
258  inline int32 getStepCount() const { return fPtr->getStepCount(); }
259 
263  void toString(String128 oString) { fPtr->toString(oString); }
264 
268  String toString() { return fPtr->toString(); }
269 
273  std::unique_ptr<GUIRawVstParameter::Editor> edit() { return fPtr->edit(); }
274 
280  std::unique_ptr<GUIRawVstParameter::Editor> edit(ParamValue const &iValue) { return fPtr->edit(iValue); }
281 
282  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
283  inline operator ParamValue() const { return fPtr->getValue(); } // NOLINT
284 
285  // allow to write param = 3 instead of param.setValue(3)
286  inline void operator=(ParamValue const &iValue) { fPtr->setValue(iValue); }
287 
288  // allow to write param1 == param2
289  inline bool operator==(const GUIRawVstParam &rhs) const { return fPtr->getValue() == rhs.fPtr->getValue(); }
290 
291  // allow to write param1 != param2
292  inline bool operator!=(const GUIRawVstParam &rhs) const { return fPtr->getValue() != rhs.fPtr->getValue(); }
293 
294 private:
295  std::unique_ptr<GUIRawVstParameter> fPtr;
296 };
297 
298 //------------------------------------------------------------------------
299 // shortcut notations
300 //------------------------------------------------------------------------
301 using GUIRawVstParamEditor = std::unique_ptr<GUIRawVstParameter::Editor>;
302 
303 }
304 }
305 }
306 }
307 
308 #endif //__PONGASOFT_VST_GUI_RAW_PARAMETER_H__
bool operator==(const GUIRawVstParam &rhs) const
Definition: GUIRawVstParameter.h:289
std::unique_ptr< GUIRawVstParameter::Editor > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:301
tresult setValue(ParamValue const &iValue)
Definition: GUIRawVstParameter.h:248
Definition: GUIRawVstParameter.h:216
std::unique_ptr< GUIRawVstParameter > fPtr
Definition: GUIRawVstParameter.h:295
tresult rollback()
Definition: GUIRawVstParameter.cpp:70
bool operator!=(const GUIRawVstParam &rhs) const
Definition: GUIRawVstParameter.h:292
Definition: Clock.h:22
GUIRawVstParameter(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:85
String toString()
Definition: GUIRawVstParameter.h:150
ParamID fParamID
Definition: GUIRawVstParameter.h:101
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIRawVstParameter.h:199
void toString(String128 oString)
Definition: GUIRawVstParameter.h:140
GUIRawVstParam()
Definition: GUIRawVstParameter.h:219
tresult setValue(ParamValue iValue)
Definition: GUIRawVstParameter.cpp:41
tresult commit()
Definition: GUIRawVstParameter.cpp:56
tresult setValue(ParamValue iValue)
Definition: GUIRawVstParameter.h:161
tresult copyValueFrom(GUIRawVstParam const &iParam)
Definition: GUIRawVstParameter.h:253
int32 getStepCount() const
Definition: GUIRawVstParameter.h:258
void toString(String128 oString)
Definition: GUIRawVstParameter.h:263
bool fIsEditing
Definition: GUIRawVstParameter.h:105
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:102
std::unique_ptr< GUIRawVstParameter::Editor > edit(ParamValue const &iValue)
Definition: GUIRawVstParameter.h:280
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:206
~Editor()
Definition: GUIRawVstParameter.h:94
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIRawVstParameter.h:191
std::function< void()> ChangeCallback
Definition: Parameters.h:57
GUIRawVstParam(GUIRawVstParam &&iPtr) noexcept
Definition: GUIRawVstParameter.h:228
GUIRawVstParam & operator=(GUIRawVstParam &&iPtr) noexcept
Definition: GUIRawVstParameter.h:231
Editor(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:28
std::unique_ptr< Editor > edit(ParamValue iValue)
Definition: GUIRawVstParameter.h:181
std::unique_ptr< Editor > edit()
Definition: GUIRawVstParameter.h:171
bool exists() const
Definition: GUIRawVstParameter.h:234
ParamValue getValue() const
Definition: GUIRawVstParameter.h:242
ParamValue fInitialParamValue
Definition: GUIRawVstParameter.h:104
ParamID fParamID
Definition: GUIRawVstParameter.h:205
ParamValue getValue() const
Definition: GUIRawVstParameter.h:127
String toString()
Definition: GUIRawVstParameter.h:268
tresult commit(ParamValue iValue)
Definition: GUIRawVstParameter.h:79
ParamID getParamID() const
Definition: GUIRawVstParameter.h:237
int32 getStepCount() const
Definition: GUIRawVstParameter.h:135
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:95
GUIRawVstParam(std::unique_ptr< GUIRawVstParameter > &&iPtr)
Definition: GUIRawVstParameter.h:222
Definition: GUIRawVstParameter.h:38
ParamID getParamID() const
Definition: GUIRawVstParameter.h:119
std::unique_ptr< GUIRawVstParameter::Editor > edit()
Definition: GUIRawVstParameter.h:273
void operator=(ParamValue const &iValue)
Definition: GUIRawVstParameter.h:286