Jamba C++ API  4.0.0
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>
22 #include "IGUIParameter.h"
23 #include "GUIVstParameter.h"
24 #include "GUIJmbParameter.h"
25 #include "GUIValParameter.h"
26 
28 
29 using namespace Steinberg::Vst;
30 
46 template<typename T>
48 {
49  static_assert(std::is_default_constructible_v<T>, "T must have a default/empty constructor: T()");
50  static_assert(std::is_copy_constructible_v<T>, "T must have a copy constructor: T(T const &)");
51  static_assert(std::is_copy_assignable_v<T>, "T must be copy assignable: T& operator=(T const &)");
52 
53 public:
55  using ParamType = T;
57 
58 public:
59  // Constructor
61  fParameter{VstUtils::make_sfo<GUIValParameter<T>>(UNDEFINED_PARAM_ID, T{})} {}
62 
63  // Constructor
64  explicit GUIOptionalParam(T const &iDefaultValue) :
65  fParameter{VstUtils::make_sfo<GUIValParameter<T>>(UNDEFINED_PARAM_ID, iDefaultValue)} {}
66 
67  // Constructor
68  explicit GUIOptionalParam(std::shared_ptr<ITGUIParameter<T>> iParameter) : fParameter{std::move(iParameter) } {
69  DCHECK_F(fParameter != nullptr);
70  }
71 
74  inline bool exists() const { return true; }
75 
78  inline ParamID getParamID() const { return fParameter->getParamID(); }
79 
83  inline ParamType getValue() const
84  {
85  ParamType res;
86  fParameter->accessValue([&res](auto const &iValue) { res = iValue; });
87  return res;
88  }
89 
92  inline bool update(ParamType const &iValue)
93  {
94  return fParameter->update(iValue);
95  }
96 
99  inline tresult setValue(ParamType const &iValue)
100  {
101  return fParameter->setValue(iValue);
102  }
103 
106  inline std::unique_ptr<EditorType> edit()
107  {
108  return fParameter->edit();
109  }
110 
113  inline std::unique_ptr<EditorType> edit(ParamType const &iValue)
114  {
115  return fParameter->edit(iValue);
116  }
117 
120  inline int32 getStepCount() const { return fParameter->getStepCount(); }
121 
127  inline operator T() const { return getValue(); } // NOLINT
128 
131  inline GUIOptionalParam &operator=(T const &iValue) { update(iValue); return *this; }
132 
135  inline bool operator==(const class_type &rhs) const { return getValue() == rhs.getValue(); }
136 
139  inline bool operator!=(const class_type &rhs) const { return getValue() != rhs.getValue(); }
140 
143  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const
144  {
145  return fParameter->connect(iChangeListener);
146  }
147 
150  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const
151  {
152  return fParameter->connect(iChangeCallback);
153  }
154 
155 private:
156  std::shared_ptr<ITGUIParameter<T>> fParameter;
157 };
158 
159 
160 template<typename T>
161 using GUIOptionalParamEditor = std::unique_ptr<typename GUIOptionalParam<T>::EditorType>;
162 
165 
166 }
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:150
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:161
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:143
std::shared_ptr< ITGUIParameter< T > > fParameter
Definition: GUIOptionalParam.h:156
Definition: GUIState.h:36
GUIOptionalParam()
Definition: GUIOptionalParam.h:60
bool operator==(const class_type &rhs) const
Allow to write param1 == param2 to compare the underlying values.
Definition: GUIOptionalParam.h:135
ParamID getParamID() const
Each parameter has a unique ID returned by this method.
Definition: GUIOptionalParam.h:78
tresult setValue(ParamType const &iValue)
Unconditionaly sets the value of the parameter to the value provided.
Definition: GUIOptionalParam.h:99
GUIOptionalParam(T const &iDefaultValue)
Definition: GUIOptionalParam.h:64
GUIOptionalParamEditor< ParamValue > GUIRawOptionalParamEditor
Definition: GUIOptionalParam.h:164
bool operator!=(const class_type &rhs) const
Allow to write param1 != param2 to compare the underlying values.
Definition: GUIOptionalParam.h:139
typename ITGUIParameter< ParamValue >::ITEditor EditorType
Definition: GUIOptionalParam.h:56
std::unique_ptr< EditorType > edit()
Creates an editor to modify the parameter in a transactional fashion.
Definition: GUIOptionalParam.h:106
int32 getStepCount() const
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition: GUIOptionalParam.h:120
GUIOptionalParam & operator=(T const &iValue)
Allow to write param = 3 instead of param.update(3) for example.
Definition: GUIOptionalParam.h:131
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:92
GUIOptionalParam(std::shared_ptr< ITGUIParameter< T >> iParameter)
Definition: GUIOptionalParam.h:68
ParamType getValue() const
Definition: GUIOptionalParam.h:83
ParamValue ParamType
Definition: GUIOptionalParam.h:55
bool exists() const
Always return true because by definition an optional parameter always exist.
Definition: GUIOptionalParam.h:74
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:232
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:113
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
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:47