Jamba C++ API 8.0.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 <concepts>
22#include <pluginterfaces/vst/vsttypes.h>
23#include <pongasoft/logging/logging.h>
25#include "GUIParamCx.h"
26
28
29using namespace Steinberg::Vst;
30
37template<std::semiregular T>
38class GUIValParameter: public ITGUIParameter<T>, public FObject
39{
40public:
41 using ParamType = T;
43
44 using FObject::update; // fixes overload hiding warning
45
46public:
47 // Constructor
48 explicit GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue) :
49 fParamID{iParamID},
50 fDefaultValue{iDefaultValue},
51 fValue(iDefaultValue)
52 {
53// DLOG_F(INFO, "GUIValParameter(%p)", this);
54 }
55
56 // Destructor
58 {
59// DLOG_F(INFO, "~GUIValParameter(%p)", this);
60 }
61
62 // getParamID
63 ParamID getParamID() const override { return fParamID; }
64 void setParamID(ParamID iParamID) { fParamID = iParamID; }
65
66 int32 getStepCount() const override { return 0; }
67
73 bool update(ParamType const &iValue) override
74 {
75 // Implementation note: because this method is declared virtual the compiler must instantiate it
76 // even if never called and will generate an error if the ParamType does not define operator!=, so we
77 // need to account for this case
79 {
80 if(fValue != iValue)
81 {
82 if(setValue(iValue) == kResultOk)
83 return true;
84 }
85 }
86 else
87 {
88 if(setValue(iValue) == kResultOk)
89 return true;
90 }
91 return false;
92 }
93
97 std::unique_ptr<EditorType> edit() override
98 {
99 return std::make_unique<DefaultEditorImpl<T>>(this, getValue());
100 }
101
107 template<class ValueModifier>
108 requires std::predicate<ValueModifier const &, T *>
109 bool updateIf(ValueModifier const &iValueModifier)
110 {
111 if(iValueModifier(&fValue))
112 {
113 changed();
114 return true;
115 }
116 return false;
117 }
118
123 tresult setValue(ParamType const &iValue) override
124 {
125 fValue = iValue;
126 changed();
127 return kResultOk;
128 }
129
132 tresult resetToDefault() override
133 {
134 return update(fDefaultValue);
135 }
136
137 // accessValue
138 tresult accessValue(ITGUIParameter<T>::ValueAccessor const &iGetter) const override
139 {
140 iGetter(fValue);
141 return kResultOk;
142 }
143
144 // getValue
145 ParamType const &getValue() const { return fValue; }
146
147 // getValue
148 ParamType &getValue() { return fValue; }
149
150 // toUTF8String
151 std::string toUTF8String(int32 iPrecision) const override
152 {
153 return VstUtils::toUTF8String(getValue(), iPrecision);
154 }
155
159 std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
160 {
161 return std::make_unique<GUIParamCx>(getParamID(), const_cast<GUIValParameter *>(this), iChangeListener);
162 }
163
167 std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
168 {
169 return std::make_unique<FObjectCxCallback>(const_cast<GUIValParameter *>(this), iChangeCallback);
170 }
171
172 // asDiscreteParameter
173 std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
174
175protected:
176 ParamID fParamID;
179};
180
192{
193public:
194 GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount) :
195 GUIValParameter(iParamID, iDefaultValue),
196 fStepCount(iStepCount)
197 {
198 DCHECK_F(fStepCount > 0);
199 }
200
201 // getStepCount
202 int32 getStepCount() const override { return fStepCount; }
203
204protected:
206};
207
208//------------------------------------------------------------------------
209// GUIValParameter::asDiscreteParameter
210//------------------------------------------------------------------------
211template<std::semiregular T>
212std::shared_ptr<GUIDiscreteParameter> GUIValParameter<T>::asDiscreteParameter(int32 iStepCount)
213{
214 if(iStepCount > 0)
215 {
217 {
218 return VstUtils::make_sfo<GUIDiscreteValParameter>(fParamID, static_cast<int32>(fValue), iStepCount);
219 }
220 }
221 return nullptr;
222}
223
224}
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:202
int32 fStepCount
Definition GUIValParameter.h:205
GUIDiscreteValParameter(ParamID iParamID, int32 iDefaultValue, int32 iStepCount)
Definition GUIValParameter.h:194
T fDefaultValue
Definition GUIValParameter.h:177
ITGUIParameter< T >::ITEditor EditorType
Definition GUIValParameter.h:42
bool updateIf(ValueModifier const &iValueModifier)
Use this flavor of update if you want to modify the value itself.
Definition GUIValParameter.h:109
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:151
ParamID fParamID
Definition GUIValParameter.h:176
ParamType & getValue()
Definition GUIValParameter.h:148
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:66
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition GUIValParameter.h:63
tresult accessValue(ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition GUIValParameter.h:138
void setParamID(ParamID iParamID)
Definition GUIValParameter.h:64
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition GUIValParameter.h:212
tresult resetToDefault() override
Resets the param to its default value.
Definition GUIValParameter.h:132
tresult setValue(ParamType const &iValue) override
Sets the value.
Definition GUIValParameter.h:123
T fValue
Definition GUIValParameter.h:178
GUIValParameter(ParamID iParamID, ParamType const &iDefaultValue)
Definition GUIValParameter.h:48
std::unique_ptr< EditorType > edit() override
Definition GUIValParameter.h:97
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition GUIValParameter.h:167
~GUIValParameter() override
Definition GUIValParameter.h:57
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition GUIValParameter.h:73
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition GUIValParameter.h:159
T ParamType
Definition GUIValParameter.h:41
ParamType const & getValue() const
Definition GUIValParameter.h:145
Defines the API for the editor, which can be obtained by calling ITGUIParameter::edit().
Definition IGUIParameter.h:241
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition IGUIParameter.h:175
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition IGUIParameter.h:186
Interface to implement to receive parameter changes.
Definition Parameters.h:47
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition Parameters.h:61
Allows to detect (at compilation time) whether the call static_cast<To>(from) (where from is of type ...
Definition Metaprogramming.h:84
Allows to detect whether a type defines operator!= at compile time.
Definition Metaprogramming.h:62
Definition GUIState.h:40
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