Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
RTJmbOutParameter.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2023 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19
20#pragma once
21
26
27namespace pongasoft::VST::RT {
28
29using namespace Utils;
30
35{
36public:
37 // Constructor
38 explicit IRTJmbOutParameter(std::shared_ptr<IJmbParamDef> iParamDef) : fParamDef{std::move(iParamDef)} {}
39
40 // getParamDef
41 inline IJmbParamDef const *getParamDef() const { return fParamDef.get(); }
42
43 // getParamID
44 ParamID getParamID() const { return fParamDef->fParamID; }
45
46 // destructor
47 virtual ~IRTJmbOutParameter() = default;
48
49 // hasUpdate
50 virtual bool hasUpdate() const = 0;
51
52 // writeToMessage
53 virtual tresult writeToMessage(Message &oMessage) = 0;
54
55 // writeToStream
56 virtual void writeToStream(std::ostream &oStream) const = 0;
57
58protected:
59 std::shared_ptr<IJmbParamDef> fParamDef;
60};
61
69template<typename T>
71{
72public:
73 using ParamType = T;
74
75 explicit RTJmbOutParameter(std::shared_ptr<JmbParamDef<T>> iParamDef) :
76 IRTJmbOutParameter(iParamDef),
77 fUpdateQueue{std::make_unique<T>(iParamDef->fDefaultValue), true}
78 {}
79
80 // getParamDef
81 inline JmbParamDef<T> const *getParamDefT() const
82 {
83 return static_cast<JmbParamDef<T> const *>(getParamDef());
84 }
85
91 inline void broadcastValue(ParamType const &iValue)
92 {
93 fUpdateQueue.push(iValue);
94 }
95
101 template<class ElementModifier>
102 void broadcast(ElementModifier const &iElementModifier)
103 {
104 fUpdateQueue.updateAndPush(iElementModifier);
105 }
106
113 template<class ElementModifier>
114 bool broadcastIf(ElementModifier const &iElementModifier)
115 {
116 return fUpdateQueue.updateAndPushIf(iElementModifier);
117 }
118
119 // hasUpdate
120 bool hasUpdate() const override { return !fUpdateQueue.isEmpty(); }
121
122 // writeToMessage - called to package and add the value to the message
123 tresult writeToMessage(Message &oMessage) override;
124
125 // writeToStream
126 void writeToStream(std::ostream &oStream) const override;
127
128private:
130};
131
132//------------------------------------------------------------------------
133// RTJmbOutParameter::writeToMessage
134//------------------------------------------------------------------------
135template<typename T>
137{
138 auto update = fUpdateQueue.pop();
139 if(update)
140 {
141 tresult res = getParamDefT()->writeToMessage(*update, oMessage);
142
143 // Implementation note: this method is called from the UI thread so releasing resources is OK!
144 auto disposable = Cast<Disposable *>::dynamic(update);
145 if(disposable)
146 disposable->dispose();
147
148 return res;
149 }
150
151 return kResultFalse;
152}
153
154//------------------------------------------------------------------------
155// RTJmbInParameter::writeToStream
156//------------------------------------------------------------------------
157template<typename T>
158void RTJmbOutParameter<T>::writeToStream(std::ostream &oStream) const
159{
160 getParamDefT()->writeToStream(*fUpdateQueue.last(), oStream);
161}
162
163//------------------------------------------------------------------------
164// RTJmbOutParam - wrapper to make writing the code much simpler and natural
165//------------------------------------------------------------------------
170template<typename T>
172{
173public:
174 RTJmbOutParam(RTJmbOutParameter<T> *iPtr) : fPtr{iPtr} // NOLINT (not marked explicit on purpose)
175 {
176 DCHECK_F(fPtr != nullptr);
177 }
178
179 // getParamID
180 inline ParamID getParamID() const { return fPtr->getParamID(); }
181
186 inline void broadcast(T const &iValue) { fPtr->broadcastValue(iValue); }
187
192 template<class ElementModifier>
193 void broadcast(ElementModifier const &iElementModifier) { fPtr->broadcast(iElementModifier); }
194
200 template<class ElementModifier>
201 bool broadcastIf(ElementModifier const &iElementModifier) { return fPtr->broadcastIf(iElementModifier); }
202
203private:
205};
206
207template<typename T, size_t N>
208using RTJmbOutParams = std::array<RTJmbOutParam<T>, N>;
209
210}
static U dynamic(T *iPtr)
Definition Metaprogramming.h:38
This is the lock free version of the SingleElementQueue.
Definition Concurrent.h:150
Base class for jamba parameters (non templated).
Definition ParamDef.h:281
Base class for all non vst parameters (need to provide serialization/deserialization).
Definition ParamDef.h:318
Simple wrapper class with better api.
Definition Messaging.h:46
IJmbParamDef const * getParamDef() const
Definition RTJmbOutParameter.h:41
virtual void writeToStream(std::ostream &oStream) const =0
ParamID getParamID() const
Definition RTJmbOutParameter.h:44
std::shared_ptr< IJmbParamDef > fParamDef
Definition RTJmbOutParameter.h:59
virtual bool hasUpdate() const =0
IRTJmbOutParameter(std::shared_ptr< IJmbParamDef > iParamDef)
Definition RTJmbOutParameter.h:38
virtual tresult writeToMessage(Message &oMessage)=0
RTJmbOutParameter< T > * fPtr
Definition RTJmbOutParameter.h:204
ParamID getParamID() const
Definition RTJmbOutParameter.h:180
void broadcast(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:193
bool broadcastIf(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:201
void broadcast(T const &iValue)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:186
RTJmbOutParam(RTJmbOutParameter< T > *iPtr)
Definition RTJmbOutParameter.h:174
Templated class for RT Jamba parameter.
Definition RTJmbOutParameter.h:71
tresult writeToMessage(Message &oMessage) override
Definition RTJmbOutParameter.h:136
bool hasUpdate() const override
Definition RTJmbOutParameter.h:120
void broadcastValue(ParamType const &iValue)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:91
Concurrent::LockFree::SingleElementQueue< T > fUpdateQueue
Definition RTJmbOutParameter.h:129
RTJmbOutParameter(std::shared_ptr< JmbParamDef< T > > iParamDef)
Definition RTJmbOutParameter.h:75
JmbParamDef< T > const * getParamDefT() const
Definition RTJmbOutParameter.h:81
void broadcast(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:102
T ParamType
Definition RTJmbOutParameter.h:73
bool broadcastIf(ElementModifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:114
void writeToStream(std::ostream &oStream) const override
Definition RTJmbOutParameter.h:158
Definition RTJmbInParameter.h:26
std::array< RTJmbOutParam< T >, N > RTJmbOutParams
Definition RTJmbOutParameter.h:208