Jamba C++ API  4.1.0
GUIValParameter.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 "GUIParamCx.h"
24 
26 
27 using namespace Steinberg::Vst;
28 
35 template<typename T>
36 class GUIValParameter: public ITGUIParameter<T>, public FObject
37 {
38 public:
39  using ParamType = T;
41 
42  using FObject::update; // fixes overload hiding warning
43 
44 public:
45  // Constructor
46  explicit GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue) :
47  fParamID{iParamID},
48  fValue(iDefaultValue)
49  {
50 // DLOG_F(INFO, "GUIValParameter(%p)", this);
51  };
52 
53  // Constructor
54  explicit GUIValParameter(ParamID iParamID, ParamType &&iDefaultValue) :
55  fParamID{iParamID},
56  fValue(std::move(iDefaultValue))
57  {
58 // DLOG_F(INFO, "GUIValParameter(%p)", this);
59  };
60 
61  // Destructor
62  ~GUIValParameter() override
63  {
64 // DLOG_F(INFO, "~GUIValParameter(%p)", this);
65  }
66 
67  // getParamID
68  ParamID getParamID() const override { return fParamID; }
69  void setParamID(ParamID iParamID) { fParamID = iParamID; }
70 
71  inline int32 getStepCount() const override { return 0; }
72 
78  bool update(ParamType const &iValue) override
79  {
80  // Implementation note: because this method is declared virtual the compiler must instantiate it
81  // even if never called and will generate an error if the ParamType does not define operator!=, so we
82  // need to account for this case
83  if constexpr(Utils::is_operator_not_eq_defined<ParamType>)
84  {
85  if(fValue != iValue)
86  {
87  if(setValue(iValue) == kResultOk)
88  return true;
89  }
90  }
91  else
92  {
93  if(setValue(iValue) == kResultOk)
94  return true;
95  }
96  return false;
97  }
98 
102  std::unique_ptr<EditorType> edit() override
103  {
104  return std::make_unique<DefaultEditorImpl<T>>(this, getValue());
105  }
106 
112  template<class ValueModifier>
113  bool updateIf(ValueModifier const &iValueModifier)
114  {
115  if(iValueModifier(&fValue))
116  {
117  changed();
118  return true;
119  }
120  return false;
121  }
122 
127  tresult setValue(ParamType const &iValue) override
128  {
129  fValue = iValue;
130  changed();
131  return kResultOk;
132  }
133 
138  tresult setValue(ParamType &&iValue)
139  {
140  fValue = std::move(iValue);
141  changed();
142  return kResultOk;
143  }
144 
145  // accessValue
146  tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
147  {
148  iGetter(fValue);
149  return kResultOk;
150  }
151 
152  // getValue
153  inline ParamType const &getValue() const { return fValue; }
154 
155  // getValue
156  inline ParamType &getValue() { return fValue; }
157 
158  // toUTF8String
159  std::string toUTF8String(int32 iPrecision) const override
160  {
161  return VstUtils::toUTF8String(getValue(), iPrecision);
162  }
163 
167  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
168  {
169  return std::make_unique<GUIParamCx>(getParamID(), const_cast<GUIValParameter *>(this), iChangeListener);
170  }
171 
175  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
176  {
177  return std::make_unique<FObjectCxCallback>(const_cast<GUIValParameter *>(this), iChangeCallback);
178  }
179 
180  // asDiscreteParameter
181  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
182 
183 protected:
184  ParamID fParamID;
186 };
187 
199 {
200 public:
201  GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount) :
202  GUIValParameter(iParamID, iDefaultValue),
203  fStepCount(iStepCount)
204  {
205  DCHECK_F(fStepCount > 0);
206  }
207 
208  // getStepCount
209  int32 getStepCount() const override { return fStepCount; }
210 
211 protected:
212  int32 fStepCount;
213 };
214 
215 //------------------------------------------------------------------------
216 // GUIValParameter::asDiscreteParameter
217 //------------------------------------------------------------------------
218 template<typename T>
219 std::shared_ptr<GUIDiscreteParameter> GUIValParameter<T>::asDiscreteParameter(int32 iStepCount)
220 {
221  if(iStepCount > 0)
222  {
223  if constexpr(Utils::is_static_cast_defined<T, int32>)
224  {
225  return VstUtils::make_sfo<GUIDiscreteValParameter>(fParamID, static_cast<int32>(fValue), iStepCount);
226  }
227  }
228  return nullptr;
229 }
230 
231 }
GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount)
Definition: GUIValParameter.h:201
~GUIValParameter() override
Definition: GUIValParameter.h:62
GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue)
Definition: GUIValParameter.h:46
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
T fValue
Definition: GUIValParameter.h:185
int32 ParamType
Definition: GUIValParameter.h:39
std::string toUTF8String(T const &iValue, Steinberg::int32 iPrecision)
This generic function will determine (at compilation time) whether T can be written to an ostream and...
Definition: Utils.h:49
GUIValParameter(ParamID iParamID, ParamType &&iDefaultValue)
Definition: GUIValParameter.h:54
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:178
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIValParameter.h:175
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition: GUIValParameter.h:219
Definition: GUIState.h:36
int32 getStepCount() const override
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition: GUIValParameter.h:71
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIValParameter.h:159
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIValParameter.h:68
std::unique_ptr< EditorType > edit() override
Definition: GUIValParameter.h:102
int32 fStepCount
Definition: GUIValParameter.h:212
tresult setValue(ParamType const &iValue) override
Sets the value.
Definition: GUIValParameter.h:127
ParamType const & getValue() const
Definition: GUIValParameter.h:153
typename ITGUIParameter< int32 >::ITEditor EditorType
Definition: GUIValParameter.h:40
bool updateIf(ValueModifier const &iValueModifier)
Use this flavor of update if you want to modify the value itself.
Definition: GUIValParameter.h:113
This parameter is not tied to any parameter definition/registration and is primarily used by the opti...
Definition: GUIValParameter.h:36
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIValParameter.h:167
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:232
Simple extension class to treat a Val parameter as a discrete one.
Definition: GUIValParameter.h:198
ParamType & getValue()
Definition: GUIValParameter.h:156
void setParamID(ParamID iParamID)
Definition: GUIValParameter.h:69
tresult setValue(ParamType &&iValue)
Sets the value.
Definition: GUIValParameter.h:138
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition: GUIValParameter.h:78
ParamID fParamID
Definition: GUIValParameter.h:184
int32 getStepCount() const override
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition: GUIValParameter.h:209
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition: GUIValParameter.h:146