Jamba  3.0.2
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  void toString(String128 oString)
136  {
137  auto parameter = fVstParameters->getParameterObject(fParamID);
138  if(parameter)
139  parameter->toString(getValue(), oString);
140  }
141 
145  String toString()
146  {
147  String128 s;
148  toString(s);
149  return String(s);
150  }
151 
156  inline tresult setValue(ParamValue iValue)
157  {
158  Editor editor(fParamID, fVstParameters);
159  editor.setValue(iValue);
160  return editor.commit();
161  }
162 
166  std::unique_ptr<Editor> edit()
167  {
168  return std::make_unique<Editor>(fParamID, fVstParameters);
169  }
170 
176  std::unique_ptr<Editor> edit(ParamValue iValue)
177  {
178  auto editor = edit();
179  editor->setValue(iValue);
180  return editor;
181  }
182 
186  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const
187  {
188  return fVstParameters->connect(fParamID, iChangeListener);
189  }
190 
194  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const
195  {
196  return fVstParameters->connect(fParamID, std::move(iChangeCallback));
197  }
198 
199 private:
200  ParamID fParamID;
202 };
203 
204 //-------------------------------------------------------------------------------
205 // GUIRawVstParam - wrapper to make writing the code much simpler and natural
206 //-------------------------------------------------------------------------------
212 {
213 public:
214  GUIRawVstParam() : fPtr{nullptr} {}
215 
216  // move constructor
217  explicit GUIRawVstParam(std::unique_ptr<GUIRawVstParameter> &&iPtr) : fPtr{std::move(iPtr)} {}
218 
219  // delete copy constructor
220  GUIRawVstParam(GUIRawVstParam &iPtr) = delete;
221 
222  // move copy constructor
223  GUIRawVstParam(GUIRawVstParam &&iPtr) noexcept : fPtr{std::move(iPtr.fPtr)} {}
224 
225  // move assignment constructor
226  GUIRawVstParam &operator=(GUIRawVstParam &&iPtr) noexcept { fPtr = std::move(iPtr.fPtr); return *this; }
227 
228  // exists
229  inline bool exists() const { return (bool) fPtr; }
230 
231  // getParamID
232  inline ParamID getParamID() const { return fPtr->getParamID(); }
233 
237  inline ParamValue getValue() const { return fPtr->getValue(); }
238 
243  tresult setValue(ParamValue const &iValue) { return fPtr->setValue(iValue); }
244 
248  tresult copyValueFrom(GUIRawVstParam const &iParam) { return setValue(iParam.getValue()); }
249 
253  void toString(String128 oString) { fPtr->toString(oString); }
254 
258  String toString() { return fPtr->toString(); }
259 
263  std::unique_ptr<GUIRawVstParameter::Editor> edit() { return fPtr->edit(); }
264 
270  std::unique_ptr<GUIRawVstParameter::Editor> edit(ParamValue const &iValue) { return fPtr->edit(iValue); }
271 
272  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
273  inline operator ParamValue() const { return fPtr->getValue(); } // NOLINT
274 
275  // allow to write param = 3 instead of param.setValue(3)
276  inline void operator=(ParamValue const &iValue) { fPtr->setValue(iValue); }
277 
278  // allow to write param1 == param2
279  inline bool operator==(const GUIRawVstParam &rhs) const { return fPtr->getValue() == rhs.fPtr->getValue(); }
280 
281  // allow to write param1 != param2
282  inline bool operator!=(const GUIRawVstParam &rhs) const { return fPtr->getValue() != rhs.fPtr->getValue(); }
283 
284 private:
285  std::unique_ptr<GUIRawVstParameter> fPtr;
286 };
287 
288 //------------------------------------------------------------------------
289 // shortcut notations
290 //------------------------------------------------------------------------
291 using GUIRawVstParamEditor = std::unique_ptr<GUIRawVstParameter::Editor>;
292 
293 }
294 }
295 }
296 }
297 
298 #endif //__PONGASOFT_VST_GUI_RAW_PARAMETER_H__
bool operator==(const GUIRawVstParam &rhs) const
Definition: GUIRawVstParameter.h:279
std::unique_ptr< GUIRawVstParameter::Editor > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:291
tresult setValue(ParamValue const &iValue)
Definition: GUIRawVstParameter.h:243
Definition: GUIRawVstParameter.h:211
std::unique_ptr< GUIRawVstParameter > fPtr
Definition: GUIRawVstParameter.h:285
tresult rollback()
Definition: GUIRawVstParameter.cpp:70
bool operator!=(const GUIRawVstParam &rhs) const
Definition: GUIRawVstParameter.h:282
Definition: Clock.h:22
GUIRawVstParameter(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:85
String toString()
Definition: GUIRawVstParameter.h:145
ParamID fParamID
Definition: GUIRawVstParameter.h:101
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIRawVstParameter.h:194
void toString(String128 oString)
Definition: GUIRawVstParameter.h:135
GUIRawVstParam()
Definition: GUIRawVstParameter.h:214
tresult setValue(ParamValue iValue)
Definition: GUIRawVstParameter.cpp:41
tresult commit()
Definition: GUIRawVstParameter.cpp:56
tresult setValue(ParamValue iValue)
Definition: GUIRawVstParameter.h:156
tresult copyValueFrom(GUIRawVstParam const &iParam)
Definition: GUIRawVstParameter.h:248
void toString(String128 oString)
Definition: GUIRawVstParameter.h:253
bool fIsEditing
Definition: GUIRawVstParameter.h:105
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:102
std::unique_ptr< GUIRawVstParameter::Editor > edit(ParamValue const &iValue)
Definition: GUIRawVstParameter.h:270
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:201
~Editor()
Definition: GUIRawVstParameter.h:94
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIRawVstParameter.h:186
std::function< void()> ChangeCallback
Definition: Parameters.h:57
GUIRawVstParam(GUIRawVstParam &&iPtr) noexcept
Definition: GUIRawVstParameter.h:223
GUIRawVstParam & operator=(GUIRawVstParam &&iPtr) noexcept
Definition: GUIRawVstParameter.h:226
Editor(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:28
std::unique_ptr< Editor > edit(ParamValue iValue)
Definition: GUIRawVstParameter.h:176
std::unique_ptr< Editor > edit()
Definition: GUIRawVstParameter.h:166
bool exists() const
Definition: GUIRawVstParameter.h:229
ParamValue getValue() const
Definition: GUIRawVstParameter.h:237
ParamValue fInitialParamValue
Definition: GUIRawVstParameter.h:104
ParamID fParamID
Definition: GUIRawVstParameter.h:200
ParamValue getValue() const
Definition: GUIRawVstParameter.h:127
String toString()
Definition: GUIRawVstParameter.h:258
tresult commit(ParamValue iValue)
Definition: GUIRawVstParameter.h:79
ParamID getParamID() const
Definition: GUIRawVstParameter.h:232
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:87
GUIRawVstParam(std::unique_ptr< GUIRawVstParameter > &&iPtr)
Definition: GUIRawVstParameter.h:217
Definition: GUIRawVstParameter.h:38
ParamID getParamID() const
Definition: GUIRawVstParameter.h:119
std::unique_ptr< GUIRawVstParameter::Editor > edit()
Definition: GUIRawVstParameter.h:263
void operator=(ParamValue const &iValue)
Definition: GUIRawVstParameter.h:276