Jamba  3.1.0
GUIJmbParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 <base/source/fobject.h>
22 #include <pongasoft/VST/ParamDef.h>
27 #include "GUIParamCx.h"
28 
29 namespace pongasoft {
30 namespace VST {
31 namespace GUI {
32 namespace Params {
33 
41 {
42 public:
43  // Constructor
44  explicit IGUIJmbParameter(std::shared_ptr<IJmbParamDef> iParamDef) : fParamDef{std::move(iParamDef)} {}
45 
46  // getParamDef
47  inline IJmbParamDef const *getParamDef() const { return fParamDef.get(); }
48 
49  // getParamID
50  ParamID getParamID() const { return fParamDef->fParamID; }
51 
52  // readFromStream
53  virtual tresult readFromStream(IBStreamer &iStreamer) = 0;
54 
55  // writeToStream
56  virtual tresult writeToStream(IBStreamer &oStreamer) const = 0;
57 
58  // readFromMessage
59  virtual tresult readFromMessage(Message const &iMessage) = 0;
60 
61  // writeToMessage
62  virtual tresult writeToMessage(Message &oMessage) const = 0;
63 
64  // writeToStream
65  virtual void writeToStream(std::ostream &oStream) const = 0;
66 
67  // handleMessage
68  tresult handleMessage(Message const &iMessage) override { return readFromMessage(iMessage); }
69 
70  // broadcast
71  tresult broadcast() const;
72 
73  // setMessageProducer
74  void setMessageProducer(IMessageProducer *iMessageProducer) { fMessageProducer = iMessageProducer; }
75 
76 protected:
77  std::shared_ptr<IJmbParamDef> fParamDef;
79 };
80 
85 template<typename T>
86 class GUIJmbParameter : public IGUIJmbParameter, public FObject
87 {
88 public:
89  using ParamType = T;
90 
91  using FObject::update; // fixes overload hiding warning
92 
93  // Constructor
94  explicit GUIJmbParameter(std::shared_ptr<JmbParamDef<T>> iParamDef) :
95  IGUIJmbParameter(iParamDef),
96  FObject(),
97  fValue{iParamDef->fDefaultValue}
98  {}
99 
100  // getParamDef
101  inline JmbParamDef<T> const *getParamDefT() const
102  {
103  return static_cast<JmbParamDef<T> const *>(getParamDef());
104  }
105 
111  bool update(ParamType const &iValue)
112  {
113  if(fValue != iValue)
114  {
115  fValue = iValue;
116  changed();
117  return true;
118  }
119  return false;
120  }
121 
126  template<class ValueModifier>
127  bool updateIf(ValueModifier const &iValueModifier)
128  {
129  if(iValueModifier(&fValue))
130  {
131  changed();
132  return true;
133  }
134  return false;
135  }
136 
141  void setValue(ParamType const &iValue)
142  {
143  fValue = iValue;
144  changed();
145  }
146 
151  void setValue(ParamType &&iValue)
152  {
153  fValue = std::move(iValue);
154  changed();
155  }
156 
160  {
161  setValue(getParamDefT()->fDefaultValue);
162  }
163 
164  // readFromStream
165  tresult readFromStream(IBStreamer &iStreamer) override
166  {
167  tresult res = getParamDefT()->readFromStream(iStreamer, fValue);
168  if(res == kResultOk)
169  changed();
170  return res;
171  }
172 
173  // writeToStream
174  tresult writeToStream(IBStreamer &oStreamer) const override
175  {
176  return getParamDefT()->writeToStream(fValue, oStreamer);
177  }
178 
179  // writeToStream
180  void writeToStream(std::ostream &oStream) const override
181  {
182  getParamDefT()->writeToStream(fValue, oStream);
183  }
184 
185  // readFromMessage
186  tresult readFromMessage(Message const &iMessage) override
187  {
188  tresult res = getParamDefT()->readFromMessage(iMessage, fValue);
189  if(res == kResultOk)
190  changed();
191  return res;
192  }
193 
194  // writeToMessage
195  tresult writeToMessage(Message &oMessage) const override
196  {
197  return getParamDefT()->writeToMessage(fValue, oMessage);
198  }
199 
200  // getValue
201  inline ParamType const &getValue() const { return fValue; }
202 
203  // getValue
204  inline ParamType &getValue() { return fValue; }
205 
209  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener)
210  {
211  return std::make_unique<GUIParamCx>(getParamID(), this, iChangeListener);
212  }
213 
217  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback)
218  {
219  return std::make_unique<FObjectCxCallback>(this, std::move(iChangeCallback));
220  }
221 
222 protected:
224 };
225 
226 
227 //------------------------------------------------------------------------
228 // GUIJmbParam - wrapper to make writing the code much simpler and natural
229 //------------------------------------------------------------------------
236 template<typename T>
238 {
239 public:
240  GUIJmbParam(GUIJmbParameter<T> *iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
241  fPtr{iPtr}
242  {}
243 
244  // exists
245  inline bool exists() const { return fPtr != nullptr; }
246 
247  // getParamID
248  inline ParamID getParamID() const { return fPtr->getParamID(); }
249 
254  inline bool update(T const &iNewValue) { return fPtr->update(iNewValue); }
255 
260  template<class ValueModifier>
261  inline bool updateIf(ValueModifier const &iValueModifier) { return fPtr->updateIf(iValueModifier); }
262 
266  inline void setValue(T const &iNewValue) { fPtr->setValue(iNewValue); }
267 
271  inline void setValue(T &&iNewValue) { fPtr->setValue(std::move(iNewValue)); }
272 
275  inline void resetToDefault() { fPtr->resetToDefault(); }
276 
277  // getValue
278  inline T const &getValue() const { return fPtr->getValue(); }
279 
280  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
281  inline operator T const &() const { return fPtr->getValue(); } // NOLINT
282 
283  // allow writing param->xxx to access the underlying type directly (if not a primitive)
284  inline T const *operator->() const { return &fPtr->getValue(); }
285 
286  // broadcast
287  inline tresult broadcast() const { return fPtr->broadcast(); }
288 
289  // broadcast
290  inline void broadcast(T const &iValue)
291  {
292  setValue(iValue);
293  broadcast();
294  }
295 
296  // broadcast
297  inline void broadcast(T &&iValue)
298  {
299  setValue(std::move(iValue));
300  broadcast();
301  }
302 
303  // connect
304  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) { return fPtr->connect(iChangeListener); }
305 
306  // connect
307  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) { return fPtr->connect(std::move(iChangeCallback)); }
308 
309 private:
311 };
312 
313 }
314 }
315 }
316 }
T const & getValue() const
Definition: GUIJmbParameter.h:278
tresult readFromStream(IBStreamer &iStreamer) override
Definition: GUIJmbParameter.h:165
void broadcast(T &&iValue)
Definition: GUIJmbParameter.h:297
UTF8String ParamType
Definition: GUIJmbParameter.h:89
tresult handleMessage(Message const &iMessage) override
Definition: GUIJmbParameter.h:68
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener)
Definition: GUIJmbParameter.h:209
bool update(ParamType const &iValue)
Definition: GUIJmbParameter.h:111
virtual tresult readFromStream(IBStreamer &iStreamer)=0
tresult broadcast() const
Definition: GUIJmbParameter.cpp:29
Definition: ParamDef.h:199
bool updateIf(ValueModifier const &iValueModifier)
Definition: GUIJmbParameter.h:127
bool updateIf(ValueModifier const &iValueModifier)
Definition: GUIJmbParameter.h:261
GUIJmbParameter< T > * fPtr
Definition: GUIJmbParameter.h:310
Definition: GUIJmbParameter.h:40
Definition: MessageProducer.h:33
Definition: Messaging.h:43
IMessageProducer * fMessageProducer
Definition: GUIJmbParameter.h:78
void setValue(T const &iNewValue)
Definition: GUIJmbParameter.h:266
Definition: Clock.h:22
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener)
Definition: GUIJmbParameter.h:304
ParamType & getValue()
Definition: GUIJmbParameter.h:204
Definition: MessageHandler.h:30
void setValue(ParamType const &iValue)
Definition: GUIJmbParameter.h:141
void setValue(ParamType &&iValue)
Definition: GUIJmbParameter.h:151
ParamID getParamID() const
Definition: GUIJmbParameter.h:50
std::shared_ptr< IJmbParamDef > fParamDef
Definition: GUIJmbParameter.h:77
ParamType const & getValue() const
Definition: GUIJmbParameter.h:201
tresult writeToStream(IBStreamer &oStreamer) const override
Definition: GUIJmbParameter.h:174
IJmbParamDef const * getParamDef() const
Definition: GUIJmbParameter.h:47
T const * operator->() const
Definition: GUIJmbParameter.h:284
void setValue(T &&iNewValue)
Definition: GUIJmbParameter.h:271
virtual tresult writeToMessage(Message &oMessage) const =0
virtual tresult writeToStream(IBStreamer &oStreamer) const =0
bool exists() const
Definition: GUIJmbParameter.h:245
Definition: GUIJmbParameter.h:237
GUIJmbParameter(std::shared_ptr< JmbParamDef< T >> iParamDef)
Definition: GUIJmbParameter.h:94
Definition: GUIJmbParameter.h:86
ParamType fValue
Definition: GUIJmbParameter.h:223
ParamID getParamID() const
Definition: GUIJmbParameter.h:248
std::function< void()> ChangeCallback
Definition: Parameters.h:57
tresult readFromMessage(Message const &iMessage) override
Definition: GUIJmbParameter.h:186
tresult broadcast() const
Definition: GUIJmbParameter.h:287
GUIJmbParam(GUIJmbParameter< T > *iPtr=nullptr)
Definition: GUIJmbParameter.h:240
tresult writeToMessage(Message &oMessage) const override
Definition: GUIJmbParameter.h:195
void setMessageProducer(IMessageProducer *iMessageProducer)
Definition: GUIJmbParameter.h:74
IGUIJmbParameter(std::shared_ptr< IJmbParamDef > iParamDef)
Definition: GUIJmbParameter.h:44
void resetToDefault()
Definition: GUIJmbParameter.h:159
virtual tresult readFromMessage(Message const &iMessage)=0
void writeToStream(std::ostream &oStream) const override
Definition: GUIJmbParameter.h:180
Definition: ParamDef.h:225
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback)
Definition: GUIJmbParameter.h:307
JmbParamDef< T > const * getParamDefT() const
Definition: GUIJmbParameter.h:101
void broadcast(T const &iValue)
Definition: GUIJmbParameter.h:290
void resetToDefault()
Definition: GUIJmbParameter.h:275
bool update(T const &iNewValue)
Definition: GUIJmbParameter.h:254
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback)
Definition: GUIJmbParameter.h:217