Jamba C++ API  4.5.0
GUIParamCxMgr.hpp
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 
21 
22 //------------------------------------------------------------------------
23 // GUIParamCxMgr::registerOptionalParam
24 //------------------------------------------------------------------------
25 template<typename T>
27  Parameters::IChangeListener *iChangeListener)
28 {
29  auto param = fGUIState->findParam(iParamID);
30  std::shared_ptr<ITGUIParameter<T>> optionalParam = nullptr;
31 
32  if(param)
33  {
34  auto typedParam = param->cast<T>();
35  if(typedParam)
36  {
37  optionalParam = std::move(typedParam);
38  }
39  else
40  {
41  DLOG_F(WARNING, "param [%d] is not of the requested type [%s]", iParamID, Utils::typeString<T>().c_str());
42  }
43  }
44 
45  // no vst or jmb parameter match => using default
46  if(!optionalParam)
47  {
48  optionalParam = VstUtils::make_sfo<GUIValParameter<T>>(iParamID, T{});
49 
50 #ifndef NDEBUG
51  if(iParamID != UNDEFINED_PARAM_ID)
52  DLOG_F(WARNING, "could not find any parameter (vst or jmb) with id [%d]... reverting to default", iParamID);
53 #endif
54  }
55 
56  return __registerListener(GUIOptionalParam<T>(std::move(optionalParam)), iChangeListener);
57 }
58 
59 //------------------------------------------------------------------------
60 // GUIParamCxMgr::registerVstParam
61 //------------------------------------------------------------------------
62 template<typename T>
64 {
65  auto param = fGUIState->getGUIVstParameter<T>(iParamID);
66 
67  return __registerListener(GUIVstParam<T>(std::move(param)), iChangeListener);
68 }
69 
70 //------------------------------------------------------------------------
71 // GUIParamCxMgr::registerJmbParam
72 //------------------------------------------------------------------------
73 template<typename T>
75 {
76  auto param = fGUIState->getJmbParameter(iParamID);
77 
78  if(!param)
79  {
80  DLOG_F(WARNING, "jmb param [%d] not found", iParamID);
81  return GUIJmbParam<T>{};
82  }
83 
84  auto res = std::dynamic_pointer_cast<GUIJmbParameter<T>>(param);
85  if(!res)
86  {
87  DLOG_F(WARNING, "jmb param [%d] is not of the requested type [%s]", iParamID, typeid(T).name());
88  }
89 
90  return __registerListener(GUIJmbParam<T>(std::move(res)), iChangeListener);
91 }
92 
93 //------------------------------------------------------------------------
94 // GUIParamCxMgr::__registerCallback
95 //------------------------------------------------------------------------
96 template<typename TParam>
97 inline TParam GUIParamCxMgr::__registerCallback(TParam iParam, Parameters::ChangeCallback iCallback, bool iInvokeCallback)
98 {
99  if(!iCallback)
100  {
101  DLOG_F(WARNING, "Callback is empty... unlikely what you want to do");
102  return iParam;
103  }
104 
105  if(iParam.exists())
106  {
107  if(iInvokeCallback)
108  iCallback();
109 
110  fParamCxs.emplace_back(iParam.connect(std::move(iCallback)));
111  }
112 
113  return iParam;
114 }
115 
116 //------------------------------------------------------------------------
117 // GUIParamCxMgr::__registerCallback1
118 //------------------------------------------------------------------------
119 template<typename TParam>
120 inline TParam GUIParamCxMgr::__registerCallback1(TParam iParam,
122  bool iInvokeCallback)
123 {
124  if(!iCallback)
125  {
126  DLOG_F(WARNING, "Callback is empty... unlikely what you want to do");
127  return iParam;
128  }
129 
130  if(iParam.exists())
131  {
132  // Implementation note: iParam is copied on purpose as we do not know its lifespan (and iParam are simple
133  // wrappers around a shared pointer so cheap/quick to copy)
134  auto callback = [iParam, cb2 = std::move(iCallback)] () mutable { cb2(iParam); };
135 
136  if(iInvokeCallback)
137  callback();
138 
139  fParamCxs.emplace_back(iParam.connect(std::move(callback)));
140  }
141 
142  return iParam;
143 }
144 
145 }
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition: Parameters.h:55
std::vector< std::unique_ptr< FObjectCx > > fParamCxs
Definition: GUIParamCxMgr.h:233
std::shared_ptr< IGUIJmbParameter > getJmbParameter(ParamID iParamID) const
Definition: GUIState.cpp:284
TParam __registerCallback1(TParam iParam, Parameters::ChangeCallback1< TParam > iCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.hpp:120
GUIState * fGUIState
Definition: GUIParamCxMgr.h:230
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition: GUIState.h:356
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIVstParameter.h:278
TParam __registerListener(TParam iParam, Parameters::IChangeListener *iChangeListener)
Definition: GUIParamCxMgr.h:215
Definition: GUIState.h:37
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIJmbParameter.h:495
GUIVstParam< T > registerVstParam(ParamID iParamID, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.hpp:63
std::shared_ptr< IGUIParameter > findParam(ParamID iParamID) const
Generic call which returns a param with the given id or nullptr if there isn't one.
Definition: GUIState.cpp:35
This is the templated version providing serializer methods, very similar to the GUIVstParameter conce...
Definition: GUIJmbParameter.h:86
GUIOptionalParam< T > registerOptionalParam(ParamID iParamID, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.hpp:26
std::function< void(Param &)> ChangeCallback1
A callback which will be invoked for changes with the param as an argument.
Definition: Parameters.h:61
GUIJmbParam< T > registerJmbParam(ParamID iParamID, Parameters::IChangeListener *iChangeListener=nullptr)
Definition: GUIParamCxMgr.hpp:74
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
TParam __registerCallback(TParam iParam, Parameters::ChangeCallback iCallback, bool iInvokeCallback)
Definition: GUIParamCxMgr.hpp:97
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:48