Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <pluginterfaces/vst/vsttypes.h>
22#include <pongasoft/logging/logging.h>
24#include "GUIParamCx.h"
25
27
28using namespace Steinberg::Vst;
29
36template<typename T>
37class GUIValParameter: public ITGUIParameter<T>, public FObject
38{
39 // Since this parameter is used by GUIOptionalParam parameter it should satisfy the same rules
40 static_assert(std::is_default_constructible_v<T>, "T must have a default/empty constructor: T()");
41 static_assert(std::is_copy_constructible_v<T>, "T must have a copy constructor: T(T const &)");
42 static_assert(std::is_copy_assignable_v<T>, "T must be copy assignable: T& operator=(T const &)");
43
44public:
45 using ParamType = T;
47
48 using FObject::update; // fixes overload hiding warning
49
50public:
51 // Constructor
52 explicit GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue) :
53 fParamID{iParamID},
54 fDefaultValue{iDefaultValue},
55 fValue(iDefaultValue)
56 {
57// DLOG_F(INFO, "GUIValParameter(%p)", this);
58 };
59
60 // Destructor
62 {
63// DLOG_F(INFO, "~GUIValParameter(%p)", this);
64 }
65
66 // getParamID
67 ParamID getParamID() const override { return fParamID; }
68 void setParamID(ParamID iParamID) { fParamID = iParamID; }
69
70 inline int32 getStepCount() const override { return 0; }
71
77 bool update(ParamType const &iValue) override
78 {
79 // Implementation note: because this method is declared virtual the compiler must instantiate it
80 // even if never called and will generate an error if the ParamType does not define operator!=, so we
81 // need to account for this case
83 {
84 if(fValue != iValue)
85 {
86 if(setValue(iValue) == kResultOk)
87 return true;
88 }
89 }
90 else
91 {
92 if(setValue(iValue) == kResultOk)
93 return true;
94 }
95 return false;
96 }
97
101 std::unique_ptr<EditorType> edit() override
102 {
103 return std::make_unique<DefaultEditorImpl<T>>(this, getValue());
104 }
105
111 template<class ValueModifier>
112 bool updateIf(ValueModifier const &iValueModifier)
113 {
114 if(iValueModifier(&fValue))
115 {
116 changed();
117 return true;
118 }
119 return false;
120 }
121
126 tresult setValue(ParamType const &iValue) override
127 {
128 fValue = iValue;
129 changed();
130 return kResultOk;
131 }
132
135 tresult resetToDefault() override
136 {
137 return update(fDefaultValue);
138 }
139
140 // accessValue
141 tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
142 {
143 iGetter(fValue);
144 return kResultOk;
145 }
146
147 // getValue
148 inline ParamType const &getValue() const { return fValue; }
149
150 // getValue
151 inline ParamType &getValue() { return fValue; }
152
153 // toUTF8String
154 std::string toUTF8String(int32 iPrecision) const override
155 {
156 return VstUtils::toUTF8String(getValue(), iPrecision);
157 }
158
162 std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
163 {
164 return std::make_unique<GUIParamCx>(getParamID(), const_cast<GUIValParameter *>(this), iChangeListener);
165 }
166
170 std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
171 {
172 return std::make_unique<FObjectCxCallback>(const_cast<GUIValParameter *>(this), iChangeCallback);
173 }
174
175 // asDiscreteParameter
176 std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
177
178protected:
179 ParamID fParamID;
182};
183
195{
196public:
197 GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount) :
198 GUIValParameter(iParamID, iDefaultValue),
199 fStepCount(iStepCount)
200 {
201 DCHECK_F(fStepCount > 0);
202 }
203
204 // getStepCount
205 int32 getStepCount() const override { return fStepCount; }
206
207protected:
209};
210
211//------------------------------------------------------------------------
212// GUIValParameter::asDiscreteParameter
213//------------------------------------------------------------------------
214template<typename T>
215std::shared_ptr<GUIDiscreteParameter> GUIValParameter<T>::asDiscreteParameter(int32 iStepCount)
216{
217 if(iStepCount > 0)
218 {
220 {
221 return VstUtils::make_sfo<GUIDiscreteValParameter>(fParamID, static_cast<int32>(fValue), iStepCount);
222 }
223 }
224 return nullptr;
225}
226
227}
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:205
int32 fStepCount
Definition GUIValParameter.h:208
GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount)
Definition GUIValParameter.h:197
T fDefaultValue
Definition GUIValParameter.h:180
bool updateIf(ValueModifier const &iValueModifier)
Use this flavor of update if you want to modify the value itself.
Definition GUIValParameter.h:112
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:154
ParamID fParamID
Definition GUIValParameter.h:179
ParamType & getValue()
Definition GUIValParameter.h:151
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:70
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition GUIValParameter.h:67
void setParamID(ParamID iParamID)
Definition GUIValParameter.h:68
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition GUIValParameter.h:215
tresult resetToDefault() override
Resets the param to its default value.
Definition GUIValParameter.h:135
tresult setValue(ParamType const &iValue) override
Sets the value.
Definition GUIValParameter.h:126
T fValue
Definition GUIValParameter.h:181
GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue)
Definition GUIValParameter.h:52
std::unique_ptr< EditorType > edit() override
Definition GUIValParameter.h:101
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition GUIValParameter.h:170
~GUIValParameter() override
Definition GUIValParameter.h:61
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition GUIValParameter.h:77
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition GUIValParameter.h:162
T ParamType
Definition GUIValParameter.h:45
typename ITGUIParameter< T >::ITEditor EditorType
Definition GUIValParameter.h:46
ParamType const & getValue() const
Definition GUIValParameter.h:148
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition GUIValParameter.h:141
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition IGUIParameter.h:238
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition IGUIParameter.h:172
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition IGUIParameter.h:183
Interface to implement to receive parameter changes.
Definition Parameters.h:45
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition Parameters.h:56
constexpr auto is_operator_not_eq_defined
Allows to detect whether a type defines operator!= at compile time.
Definition Metaprogramming.h:70
constexpr auto is_static_cast_defined
Allows to detect (at compilation time) whether the call static_cast<To>(from) (where from is of type ...
Definition Metaprogramming.h:94
Definition GUIState.h:38
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:47
std::shared_ptr< T > make_sfo(Args &&...iArgs)
The VST SDK uses the concept of FObject (which are self contained reference counted objects) but requ...
Definition Utils.h:86