Jamba C++ API  5.1.1
GUIOptionalParam.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 <pluginterfaces/vst/vsttypes.h>
21 #include <pongasoft/logging/logging.h>
23 #include "IGUIParameter.h"
24 #include "GUIVstParameter.h"
25 #include "GUIJmbParameter.h"
26 #include "GUIValParameter.h"
27 
29 
30 using namespace Steinberg::Vst;
31 
47 template<typename T>
48 class GUIOptionalParam: public Utils::Operators::Dereferenceable<GUIOptionalParam<T>>
49 {
50  static_assert(std::is_default_constructible_v<T>, "T must have a default/empty constructor: T()");
51  static_assert(std::is_copy_constructible_v<T>, "T must have a copy constructor: T(T const &)");
52  static_assert(std::is_copy_assignable_v<T>, "T must be copy assignable: T& operator=(T const &)");
53 
54 public:
56  using ParamType = T;
58  using Editor = std::unique_ptr<EditorType>;
59 
60 public:
65  class Value {
66  public:
67  constexpr T const *operator ->() const { return &fValue; }
68  friend class GUIOptionalParam<T>;
69  private:
70  explicit Value(T const &iValue) : fValue{iValue} {}
71  T fValue;
72  };
73 
74 public:
75  // Constructor
77  fParameter{VstUtils::make_sfo<GUIValParameter<T>>(UNDEFINED_PARAM_ID, T{})} {}
78 
79  // Constructor
80  explicit GUIOptionalParam(T const &iDefaultValue) :
81  fParameter{VstUtils::make_sfo<GUIValParameter<T>>(UNDEFINED_PARAM_ID, iDefaultValue)} {}
82 
83  // Constructor
84  explicit GUIOptionalParam(std::shared_ptr<ITGUIParameter<T>> iParameter) : fParameter{std::move(iParameter) } {
85  DCHECK_F(fParameter != nullptr);
86  }
87 
90  inline bool exists() const { return true; }
91 
94  inline ParamID getParamID() const { return fParameter->getParamID(); }
95 
99  inline ParamType getValue() const
100  {
101  ParamType res;
102  fParameter->accessValue([&res](auto const &iValue) { res = iValue; });
103  return res;
104  }
105 
107  inline ParamType value() const { return getValue(); }
108 
111  inline bool update(ParamType const &iValue)
112  {
113  return fParameter->update(iValue);
114  }
115 
118  inline tresult setValue(ParamType const &iValue)
119  {
120  return fParameter->setValue(iValue);
121  }
122 
125  inline tresult resetToDefault() { fParameter->resetToDefault(); }
126 
129  inline std::unique_ptr<EditorType> edit()
130  {
131  return fParameter->edit();
132  }
133 
136  inline std::unique_ptr<EditorType> edit(ParamType const &iValue)
137  {
138  return fParameter->edit(iValue);
139  }
140 
143  inline int32 getStepCount() const { return fParameter->getStepCount(); }
144 
146  constexpr ParamType operator *() const { return getValue(); }
147 
149  constexpr Value operator ->() const { return Value{getValue()}; }
150 
156  [[deprecated("Since 4.1.0 - use operator* or value() instead (ex: if(*param) {...} or if(param.value() {...})")]]
157  inline operator ParamType() const { return getValue(); } // NOLINT
158 
161  inline GUIOptionalParam &operator=(T const &iValue) { update(iValue); return *this; }
162 
165  inline std::string toUTF8String(int32 iPrecision) const { return fParameter->toUTF8String(iPrecision); }
166 
169  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const
170  {
171  return fParameter->connect(iChangeListener);
172  }
173 
176  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const
177  {
178  return fParameter->connect(iChangeCallback);
179  }
180 
181 private:
182  std::shared_ptr<ITGUIParameter<ParamType>> fParameter;
183 };
184 
185 
186 template<typename T>
187 using GUIOptionalParamEditor = std::unique_ptr<typename GUIOptionalParam<T>::EditorType>;
188 
191 
192 }
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Creates a connection between this parameter and the callback: whenever the parameter changes,...
Definition: GUIOptionalParam.h:176
ParamType value() const
Synonym to getValue()
Definition: GUIOptionalParam.h:107
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< typename GUIOptionalParam< T >::EditorType > GUIOptionalParamEditor
Definition: GUIOptionalParam.h:187
T fValue
Definition: GUIOptionalParam.h:71
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Creates a connection between this parameter and the change listener: whenever the parameter changes,...
Definition: GUIOptionalParam.h:169
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition: Operators.h:54
tresult resetToDefault()
Resets the param to its default value.
Definition: GUIOptionalParam.h:125
std::unique_ptr< EditorType > Editor
Definition: GUIOptionalParam.h:58
constexpr ParamType operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: GUIOptionalParam.h:146
Definition: GUIState.h:37
GUIOptionalParam()
Definition: GUIOptionalParam.h:76
ParamID getParamID() const
Each parameter has a unique ID returned by this method.
Definition: GUIOptionalParam.h:94
tresult setValue(ParamType const &iValue)
Unconditionally sets the value of the parameter to the value provided.
Definition: GUIOptionalParam.h:118
GUIOptionalParam(T const &iDefaultValue)
Definition: GUIOptionalParam.h:80
GUIOptionalParamEditor< ParamValue > GUIRawOptionalParamEditor
Definition: GUIOptionalParam.h:190
typename ITGUIParameter< ParamValue >::ITEditor EditorType
Definition: GUIOptionalParam.h:57
std::unique_ptr< EditorType > edit()
Creates an editor to modify the parameter in a transactional fashion.
Definition: GUIOptionalParam.h:129
int32 getStepCount() const
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition: GUIOptionalParam.h:143
GUIOptionalParam & operator=(T const &iValue)
Allow to write param = 3 instead of param.update(3) for example.
Definition: GUIOptionalParam.h:161
bool update(ParamType const &iValue)
First check if the value provided (iValue) is different from the current value and if that is the cas...
Definition: GUIOptionalParam.h:111
GUIOptionalParam(std::shared_ptr< ITGUIParameter< T >> iParameter)
Definition: GUIOptionalParam.h:84
ParamType getValue() const
Definition: GUIOptionalParam.h:99
ParamValue ParamType
Definition: GUIOptionalParam.h:56
std::string toUTF8String(int32 iPrecision) const
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIOptionalParam.h:165
bool exists() const
Always return true because by definition an optional parameter always exist.
Definition: GUIOptionalParam.h:90
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:236
std::shared_ptr< ITGUIParameter< ParamType > > fParameter
Definition: GUIOptionalParam.h:182
constexpr T const * operator ->() const
Definition: GUIOptionalParam.h:67
The purpose of this class is to copy the value so that it can be accessed via -> thus allowing to wri...
Definition: GUIOptionalParam.h:65
std::unique_ptr< EditorType > edit(ParamType const &iValue)
Shortcut api which creates an editor followed by ITEditor::setValue(ParamType const &) to set the par...
Definition: GUIOptionalParam.h:136
constexpr Value operator ->() const
allow writing param->x to access the underlying value when T is a struct or class
Definition: GUIOptionalParam.h:149
constexpr ParamID UNDEFINED_PARAM_ID
Constant used throughout the code to test whether the ParamID represents a valid id or an undefined o...
Definition: Types.h:47
Value(T const &iValue)
Definition: GUIOptionalParam.h:70
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
Represents an optional parameter (Jmb, Vst or no param at all).
Definition: GUIOptionalParam.h:48