Jamba C++ API 8.0.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 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
82 {
83 return static_cast<JmbParamDef<T> const *>(getParamDef());
84 }
85
91 void broadcastValue(ParamType const &iValue)
92 {
93 fUpdateQueue.push(iValue);
94 }
95
101 template<ElementModifier<T> Modifier>
102 void broadcast(Modifier const &iElementModifier)
103 {
104 fUpdateQueue.updateAndPush(iElementModifier);
105 }
106
113 template<ElementPredicate<T> Modifier>
114 bool broadcastIf(Modifier 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 if(auto disposable = safe_dynamic_cast<Disposable *>(update))
145 disposable->dispose();
146
147 return res;
148 }
149
150 return kResultFalse;
151}
152
153//------------------------------------------------------------------------
154// RTJmbInParameter::writeToStream
155//------------------------------------------------------------------------
156template<typename T>
157void 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//------------------------------------------------------------------------
169template<typename T>
171{
172public:
173 RTJmbOutParam(RTJmbOutParameter<T> *iPtr) : fPtr{iPtr} // NOLINT (not marked explicit on purpose)
174 {
175 DCHECK_F(fPtr != nullptr);
176 }
177
178 // getParamID
179 ParamID getParamID() const { return fPtr->getParamID(); }
180
185 void broadcast(T const &iValue) { fPtr->broadcastValue(iValue); }
186
191 template<ElementModifier<T> Modifier>
192 void broadcast(Modifier const &iElementModifier) { fPtr->broadcast(iElementModifier); }
193
199 template<ElementPredicate<T> Modifier>
200 bool broadcastIf(Modifier const &iElementModifier) { return fPtr->broadcastIf(iElementModifier); }
201
202private:
204};
205
207template<typename T, size_t N>
208using RTJmbOutParams = std::array<RTJmbOutParam<T>, N>;
209
210}
This is the lock free version of the SingleElementQueue.
Definition Concurrent.h:162
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:43
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
bool broadcastIf(Modifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:200
RTJmbOutParameter< T > * fPtr
Definition RTJmbOutParameter.h:203
ParamID getParamID() const
Definition RTJmbOutParameter.h:179
void broadcast(Modifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:192
void broadcast(T const &iValue)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:185
RTJmbOutParam(RTJmbOutParameter< T > *iPtr)
Definition RTJmbOutParameter.h:173
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
bool broadcastIf(Modifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:114
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
T ParamType
Definition RTJmbOutParameter.h:73
void broadcast(Modifier const &iElementModifier)
Enqueues the value to be delivered to the GUI (or whoever is listening to messages).
Definition RTJmbOutParameter.h:102
void writeToStream(std::ostream &oStream) const override
Definition RTJmbOutParameter.h:157
constexpr To safe_dynamic_cast(From *p) noexcept
dynamic_cast<U *>(x) does not compile if x is not polymorphic.
Definition Metaprogramming.h:41
Definition RTJmbInParameter.h:26
std::array< RTJmbOutParam< T >, N > RTJmbOutParams
An array of RTJmbOutParam.
Definition RTJmbOutParameter.h:208