Jamba C++ API  5.0.0
GUIValParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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  // Since this parameter is used by GUIOptionalParam parameter it should satisfy the same rules
39  static_assert(std::is_default_constructible_v<T>, "T must have a default/empty constructor: T()");
40  static_assert(std::is_copy_constructible_v<T>, "T must have a copy constructor: T(T const &)");
41  static_assert(std::is_copy_assignable_v<T>, "T must be copy assignable: T& operator=(T const &)");
42 
43 public:
44  using ParamType = T;
46 
47  using FObject::update; // fixes overload hiding warning
48 
49 public:
50  // Constructor
51  explicit GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue) :
52  fParamID{iParamID},
53  fDefaultValue{iDefaultValue},
54  fValue(iDefaultValue)
55  {
56 // DLOG_F(INFO, "GUIValParameter(%p)", this);
57  };
58 
59  // Destructor
60  ~GUIValParameter() override
61  {
62 // DLOG_F(INFO, "~GUIValParameter(%p)", this);
63  }
64 
65  // getParamID
66  ParamID getParamID() const override { return fParamID; }
67  void setParamID(ParamID iParamID) { fParamID = iParamID; }
68 
69  inline int32 getStepCount() const override { return 0; }
70 
76  bool update(ParamType const &iValue) override
77  {
78  // Implementation note: because this method is declared virtual the compiler must instantiate it
79  // even if never called and will generate an error if the ParamType does not define operator!=, so we
80  // need to account for this case
81  if constexpr(Utils::is_operator_not_eq_defined<ParamType>)
82  {
83  if(fValue != iValue)
84  {
85  if(setValue(iValue) == kResultOk)
86  return true;
87  }
88  }
89  else
90  {
91  if(setValue(iValue) == kResultOk)
92  return true;
93  }
94  return false;
95  }
96 
100  std::unique_ptr<EditorType> edit() override
101  {
102  return std::make_unique<DefaultEditorImpl<T>>(this, getValue());
103  }
104 
110  template<class ValueModifier>
111  bool updateIf(ValueModifier const &iValueModifier)
112  {
113  if(iValueModifier(&fValue))
114  {
115  changed();
116  return true;
117  }
118  return false;
119  }
120 
125  tresult setValue(ParamType const &iValue) override
126  {
127  fValue = iValue;
128  changed();
129  return kResultOk;
130  }
131 
134  tresult resetToDefault() override
135  {
136  return update(fDefaultValue);
137  }
138 
139  // accessValue
140  tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
141  {
142  iGetter(fValue);
143  return kResultOk;
144  }
145 
146  // getValue
147  inline ParamType const &getValue() const { return fValue; }
148 
149  // getValue
150  inline ParamType &getValue() { return fValue; }
151 
152  // toUTF8String
153  std::string toUTF8String(int32 iPrecision) const override
154  {
155  return VstUtils::toUTF8String(getValue(), iPrecision);
156  }
157 
161  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
162  {
163  return std::make_unique<GUIParamCx>(getParamID(), const_cast<GUIValParameter *>(this), iChangeListener);
164  }
165 
169  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
170  {
171  return std::make_unique<FObjectCxCallback>(const_cast<GUIValParameter *>(this), iChangeCallback);
172  }
173 
174  // asDiscreteParameter
175  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
176 
177 protected:
178  ParamID fParamID;
181 };
182 
194 {
195 public:
196  GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount) :
197  GUIValParameter(iParamID, iDefaultValue),
198  fStepCount(iStepCount)
199  {
200  DCHECK_F(fStepCount > 0);
201  }
202 
203  // getStepCount
204  int32 getStepCount() const override { return fStepCount; }
205 
206 protected:
207  int32 fStepCount;
208 };
209 
210 //------------------------------------------------------------------------
211 // GUIValParameter::asDiscreteParameter
212 //------------------------------------------------------------------------
213 template<typename T>
214 std::shared_ptr<GUIDiscreteParameter> GUIValParameter<T>::asDiscreteParameter(int32 iStepCount)
215 {
216  if(iStepCount > 0)
217  {
218  if constexpr(Utils::is_static_cast_defined<int32, T> && Utils::is_static_cast_defined<T, int32>)
219  {
220  return VstUtils::make_sfo<GUIDiscreteValParameter>(fParamID, static_cast<int32>(fValue), iStepCount);
221  }
222  }
223  return nullptr;
224 }
225 
226 }
GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount)
Definition: GUIValParameter.h:196
~GUIValParameter() override
Definition: GUIValParameter.h:60
GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue)
Definition: GUIValParameter.h:51
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:180
int32 ParamType
Definition: GUIValParameter.h:44
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
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:182
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIValParameter.h:169
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition: GUIValParameter.h:214
Definition: GUIState.h:37
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:69
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:153
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIValParameter.h:66
std::unique_ptr< EditorType > edit() override
Definition: GUIValParameter.h:100
T fDefaultValue
Definition: GUIValParameter.h:179
int32 fStepCount
Definition: GUIValParameter.h:207
tresult setValue(ParamType const &iValue) override
Sets the value.
Definition: GUIValParameter.h:125
ParamType const & getValue() const
Definition: GUIValParameter.h:147
typename ITGUIParameter< int32 >::ITEditor EditorType
Definition: GUIValParameter.h:45
bool updateIf(ValueModifier const &iValueModifier)
Use this flavor of update if you want to modify the value itself.
Definition: GUIValParameter.h:111
tresult resetToDefault() override
Resets the param to its default value.
Definition: GUIValParameter.h:134
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:161
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:236
Simple extension class to treat a Val parameter as a discrete one.
Definition: GUIValParameter.h:193
ParamType & getValue()
Definition: GUIValParameter.h:150
void setParamID(ParamID iParamID)
Definition: GUIValParameter.h:67
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition: GUIValParameter.h:76
ParamID fParamID
Definition: GUIValParameter.h:178
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:204
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition: GUIValParameter.h:140