Jamba  3.0.2
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 
157  // readFromStream
158  tresult readFromStream(IBStreamer &iStreamer) override
159  {
160  tresult res = getParamDefT()->readFromStream(iStreamer, fValue);
161  if(res == kResultOk)
162  changed();
163  return res;
164  }
165 
166  // writeToStream
167  tresult writeToStream(IBStreamer &oStreamer) const override
168  {
169  return getParamDefT()->writeToStream(fValue, oStreamer);
170  }
171 
172  // writeToStream
173  void writeToStream(std::ostream &oStream) const override
174  {
175  getParamDefT()->writeToStream(fValue, oStream);
176  }
177 
178  // readFromMessage
179  tresult readFromMessage(Message const &iMessage) override
180  {
181  tresult res = getParamDefT()->readFromMessage(iMessage, fValue);
182  if(res == kResultOk)
183  changed();
184  return res;
185  }
186 
187  // writeToMessage
188  tresult writeToMessage(Message &oMessage) const override
189  {
190  return getParamDefT()->writeToMessage(fValue, oMessage);
191  }
192 
193  // getValue
194  inline ParamType const &getValue() const { return fValue; }
195 
196  // getValue
197  inline ParamType &getValue() { return fValue; }
198 
202  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener)
203  {
204  return std::make_unique<GUIParamCx>(getParamID(), this, iChangeListener);
205  }
206 
210  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback)
211  {
212  return std::make_unique<FObjectCxCallback>(this, std::move(iChangeCallback));
213  }
214 
215 protected:
217 };
218 
219 
220 //------------------------------------------------------------------------
221 // GUIJmbParam - wrapper to make writing the code much simpler and natural
222 //------------------------------------------------------------------------
229 template<typename T>
231 {
232 public:
233  GUIJmbParam(GUIJmbParameter<T> *iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
234  fPtr{iPtr}
235  {}
236 
237  // exists
238  inline bool exists() const { return fPtr != nullptr; }
239 
240  // getParamID
241  inline ParamID getParamID() const { return fPtr->getParamID(); }
242 
247  inline bool update(T const &iNewValue) { return fPtr->update(iNewValue); }
248 
253  template<class ValueModifier>
254  inline bool updateIf(ValueModifier const &iValueModifier) { return fPtr->updateIf(iValueModifier); }
255 
259  inline void setValue(T const &iNewValue) { fPtr->setValue(iNewValue); }
260 
264  inline void setValue(T &&iNewValue) { fPtr->setValue(std::move(iNewValue)); }
265 
266  // getValue
267  inline T const &getValue() const { return fPtr->getValue(); }
268 
269  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
270  inline operator T const &() const { return fPtr->getValue(); } // NOLINT
271 
272  // allow writing param->xxx to access the underlying type directly (if not a primitive)
273  inline T const *operator->() const { return &fPtr->getValue(); }
274 
275  // broadcast
276  inline tresult broadcast() const { return fPtr->broadcast(); }
277 
278  // broadcast
279  inline void broadcast(T const &iValue)
280  {
281  setValue(iValue);
282  broadcast();
283  }
284 
285  // broadcast
286  inline void broadcast(T &&iValue)
287  {
288  setValue(std::move(iValue));
289  broadcast();
290  }
291 
292  // connect
293  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) { return fPtr->connect(iChangeListener); }
294 
295  // connect
296  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) { return fPtr->connect(std::move(iChangeCallback)); }
297 
298 private:
300 };
301 
302 }
303 }
304 }
305 }
T const & getValue() const
Definition: GUIJmbParameter.h:267
tresult readFromStream(IBStreamer &iStreamer) override
Definition: GUIJmbParameter.h:158
void broadcast(T &&iValue)
Definition: GUIJmbParameter.h:286
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:202
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:254
GUIJmbParameter< T > * fPtr
Definition: GUIJmbParameter.h:299
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:259
Definition: Clock.h:22
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener)
Definition: GUIJmbParameter.h:293
ParamType & getValue()
Definition: GUIJmbParameter.h:197
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:194
tresult writeToStream(IBStreamer &oStreamer) const override
Definition: GUIJmbParameter.h:167
IJmbParamDef const * getParamDef() const
Definition: GUIJmbParameter.h:47
T const * operator->() const
Definition: GUIJmbParameter.h:273
void setValue(T &&iNewValue)
Definition: GUIJmbParameter.h:264
virtual tresult writeToMessage(Message &oMessage) const =0
virtual tresult writeToStream(IBStreamer &oStreamer) const =0
bool exists() const
Definition: GUIJmbParameter.h:238
Definition: GUIJmbParameter.h:230
GUIJmbParameter(std::shared_ptr< JmbParamDef< T >> iParamDef)
Definition: GUIJmbParameter.h:94
Definition: GUIJmbParameter.h:86
ParamType fValue
Definition: GUIJmbParameter.h:216
ParamID getParamID() const
Definition: GUIJmbParameter.h:241
std::function< void()> ChangeCallback
Definition: Parameters.h:57
tresult readFromMessage(Message const &iMessage) override
Definition: GUIJmbParameter.h:179
tresult broadcast() const
Definition: GUIJmbParameter.h:276
GUIJmbParam(GUIJmbParameter< T > *iPtr=nullptr)
Definition: GUIJmbParameter.h:233
tresult writeToMessage(Message &oMessage) const override
Definition: GUIJmbParameter.h:188
void setMessageProducer(IMessageProducer *iMessageProducer)
Definition: GUIJmbParameter.h:74
IGUIJmbParameter(std::shared_ptr< IJmbParamDef > iParamDef)
Definition: GUIJmbParameter.h:44
virtual tresult readFromMessage(Message const &iMessage)=0
void writeToStream(std::ostream &oStream) const override
Definition: GUIJmbParameter.h:173
Definition: ParamDef.h:225
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback)
Definition: GUIJmbParameter.h:296
JmbParamDef< T > const * getParamDefT() const
Definition: GUIJmbParameter.h:101
void broadcast(T const &iValue)
Definition: GUIJmbParameter.h:279
bool update(T const &iNewValue)
Definition: GUIJmbParameter.h:247
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback)
Definition: GUIJmbParameter.h:210