Jamba C++ API  5.1.1
RTJmbOutParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 
19 #pragma once
20 
24 #include <pongasoft/VST/ParamDef.h>
25 
26 namespace pongasoft::VST::RT {
27 
28 using namespace Utils;
29 
34 {
35 public:
36  // Constructor
37  explicit IRTJmbOutParameter(std::shared_ptr<IJmbParamDef> iParamDef) : fParamDef{std::move(iParamDef)} {}
38 
39  // getParamDef
40  inline IJmbParamDef const *getParamDef() const { return fParamDef.get(); }
41 
42  // getParamID
43  ParamID getParamID() const { return fParamDef->fParamID; }
44 
45  // destructor
46  virtual ~IRTJmbOutParameter() = default;
47 
48  // hasUpdate
49  virtual bool hasUpdate() const = 0;
50 
51  // writeToMessage
52  virtual tresult writeToMessage(Message &oMessage) = 0;
53 
54  // writeToStream
55  virtual void writeToStream(std::ostream &oStream) const = 0;
56 
57 protected:
58  std::shared_ptr<IJmbParamDef> fParamDef;
59 };
60 
68 template<typename T>
70 {
71 public:
72  using ParamType = T;
73 
74  explicit RTJmbOutParameter(std::shared_ptr<JmbParamDef<T>> iParamDef) :
75  IRTJmbOutParameter(iParamDef),
76  fUpdateQueue{std::make_unique<T>(iParamDef->fDefaultValue), true}
77  {}
78 
79  // getParamDef
80  inline JmbParamDef<T> const *getParamDefT() const
81  {
82  return static_cast<JmbParamDef<T> const *>(getParamDef());
83  }
84 
90  inline void broadcastValue(ParamType const &iValue)
91  {
92  fUpdateQueue.push(iValue);
93  }
94 
100  template<class ElementModifier>
101  void broadcast(ElementModifier const &iElementModifier)
102  {
103  fUpdateQueue.updateAndPush(iElementModifier);
104  }
105 
112  template<class ElementModifier>
113  bool broadcastIf(ElementModifier const &iElementModifier)
114  {
115  return fUpdateQueue.updateAndPushIf(iElementModifier);
116  }
117 
118  // hasUpdate
119  bool hasUpdate() const override { return !fUpdateQueue.isEmpty(); }
120 
121  // writeToMessage - called to package and add the value to the message
122  tresult writeToMessage(Message &oMessage) override;
123 
124  // writeToStream
125  void writeToStream(std::ostream &oStream) const override;
126 
127 private:
129 };
130 
131 //------------------------------------------------------------------------
132 // RTJmbOutParameter::writeToMessage
133 //------------------------------------------------------------------------
134 template<typename T>
136 {
137  auto update = fUpdateQueue.pop();
138  if(update)
139  {
140  tresult res = getParamDefT()->writeToMessage(*update, oMessage);
141 
142  // Implementation note: this method is called from the UI thread so releasing resources is OK!
143  auto disposable = Cast<Disposable *>::dynamic(update);
144  if(disposable)
145  disposable->dispose();
146 
147  return res;
148  }
149 
150  return kResultFalse;
151 }
152 
153 //------------------------------------------------------------------------
154 // RTJmbInParameter::writeToStream
155 //------------------------------------------------------------------------
156 template<typename T>
157 void RTJmbOutParameter<T>::writeToStream(std::ostream &oStream) const
158 {
159  getParamDefT()->writeToStream(*fUpdateQueue.last(), oStream);
160 }
161 
162 //------------------------------------------------------------------------
163 // RTJmbOutParam - wrapper to make writing the code much simpler and natural
164 //------------------------------------------------------------------------
169 template<typename T>
171 {
172 public:
173  RTJmbOutParam(RTJmbOutParameter<T> *iPtr) : fPtr{iPtr} // NOLINT (not marked explicit on purpose)
174  {
175  DCHECK_F(fPtr != nullptr);
176  }
177 
178  // getParamID
179  inline ParamID getParamID() const { return fPtr->getParamID(); }
180 
185  inline void broadcast(T const &iValue) { fPtr->broadcastValue(iValue); }
186 
191  template<class ElementModifier>
192  void broadcast(ElementModifier const &iElementModifier) { fPtr->broadcast(iElementModifier); }
193 
199  template<class ElementModifier>
200  bool broadcastIf(ElementModifier const &iElementModifier) { return fPtr->broadcastIf(iElementModifier); }
201 
202 private:
204 };
205 
206 }
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTJmbOutParameter.h:170
Templated class for RT Jamba parameter.
Definition: RTJmbOutParameter.h:69
void broadcast(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition: RTJmbOutParameter.h:192
Base (non templated) class for RT Jamba (Outbound) parameters.
Definition: RTJmbOutParameter.h:33
bool broadcastIf(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition: RTJmbOutParameter.h:113
Simple wrapper class with better api.
Definition: Messaging.h:44
RTJmbOutParam(RTJmbOutParameter< T > *iPtr)
Definition: RTJmbOutParameter.h:173
ParamID getParamID() const
Definition: RTJmbOutParameter.h:179
static U dynamic(T *iPtr)
Definition: Metaprogramming.h:37
void broadcast(T const &iValue)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition: RTJmbOutParameter.h:185
Definition: RTJmbInParameter.h:25
std::shared_ptr< IJmbParamDef > fParamDef
Definition: RTJmbOutParameter.h:58
bool hasUpdate() const override
Definition: RTJmbOutParameter.h:119
RTJmbOutParameter< T > * fPtr
Definition: RTJmbOutParameter.h:203
void broadcast(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition: RTJmbOutParameter.h:101
IJmbParamDef const * getParamDef() const
Definition: RTJmbOutParameter.h:40
tresult writeToMessage(Message &oMessage) override
Definition: RTJmbOutParameter.h:135
JmbParamDef< T > const * getParamDefT() const
Definition: RTJmbOutParameter.h:80
RTJmbOutParameter(std::shared_ptr< JmbParamDef< T >> iParamDef)
Definition: RTJmbOutParameter.h:74
void broadcastValue(ParamType const &iValue)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition: RTJmbOutParameter.h:90
ParamID getParamID() const
Definition: RTJmbOutParameter.h:43
void writeToStream(std::ostream &oStream) const override
Definition: RTJmbOutParameter.h:157
T ParamType
Definition: RTJmbOutParameter.h:72
bool broadcastIf(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition: RTJmbOutParameter.h:200
IRTJmbOutParameter(std::shared_ptr< IJmbParamDef > iParamDef)
Definition: RTJmbOutParameter.h:37
This is the lock free version of the SingleElementQueue.
Definition: Concurrent.h:148