Jamba  3.2.0
RTJmbOutParameter.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 
19 #pragma once
20 
24 #include <pongasoft/VST/ParamDef.h>
25 
26 namespace pongasoft {
27 namespace VST {
28 namespace RT {
29 
30 using namespace Utils;
31 
36 {
37 public:
38  // Constructor
39  explicit IRTJmbOutParameter(std::shared_ptr<IJmbParamDef> iParamDef) : fParamDef{std::move(iParamDef)} {}
40 
41  // getParamDef
42  inline IJmbParamDef const *getParamDef() const { return fParamDef.get(); }
43 
44  // getParamID
45  ParamID getParamID() const { return fParamDef->fParamID; }
46 
47  // destructor
48  virtual ~IRTJmbOutParameter() = default;
49 
50  // hasUpdate
51  virtual bool hasUpdate() const = 0;
52 
53  // writeToMessage
54  virtual tresult writeToMessage(Message &oMessage) = 0;
55 
56  // writeToStream
57  virtual void writeToStream(std::ostream &oStream) const = 0;
58 
59 protected:
60  std::shared_ptr<IJmbParamDef> fParamDef;
61 };
62 
70 template<typename T>
72 {
73 public:
74  using ParamType = T;
75 
76  explicit RTJmbOutParameter(std::shared_ptr<JmbParamDef<T>> iParamDef) :
77  IRTJmbOutParameter(iParamDef),
78  fUpdateQueue{std::make_unique<T>(iParamDef->fDefaultValue), true}
79  {}
80 
81  // getParamDef
82  inline JmbParamDef<T> const *getParamDefT() const
83  {
84  return static_cast<JmbParamDef<T> const *>(getParamDef());
85  }
86 
92  inline void broadcastValue(ParamType const &iValue)
93  {
94  fUpdateQueue.push(iValue);
95  }
96 
102  template<class ElementModifier>
103  void broadcast(ElementModifier const &iElementModifier)
104  {
105  fUpdateQueue.updateAndPush(iElementModifier);
106  }
107 
114  template<class ElementModifier>
115  bool broadcastIf(ElementModifier const &iElementModifier)
116  {
117  return fUpdateQueue.updateAndPushIf(iElementModifier);
118  }
119 
120  // hasUpdate
121  bool hasUpdate() const override { return !fUpdateQueue.isEmpty(); }
122 
123  // writeToMessage - called to package and add the value to the message
124  tresult writeToMessage(Message &oMessage) override;
125 
126  // writeToStream
127  void writeToStream(std::ostream &oStream) const override;
128 
129 private:
131 };
132 
133 //------------------------------------------------------------------------
134 // RTJmbOutParameter::writeToMessage
135 //------------------------------------------------------------------------
136 template<typename T>
138 {
139  auto update = fUpdateQueue.pop();
140  if(update)
141  {
142  tresult res = getParamDefT()->writeToMessage(*update, oMessage);
143 
144  // Implementation note: this method is called from the UI thread so releasing resources is OK!
145  auto disposable = Cast<Disposable *>::dynamic(update);
146  if(disposable)
147  disposable->dispose();
148 
149  return res;
150  }
151 
152  return kResultFalse;
153 }
154 
155 //------------------------------------------------------------------------
156 // RTJmbInParameter::writeToStream
157 //------------------------------------------------------------------------
158 template<typename T>
159 void RTJmbOutParameter<T>::writeToStream(std::ostream &oStream) const
160 {
161  getParamDefT()->writeToStream(*fUpdateQueue.last(), oStream);
162 }
163 
164 //------------------------------------------------------------------------
165 // RTJmbOutParam - wrapper to make writing the code much simpler and natural
166 //------------------------------------------------------------------------
171 template<typename T>
173 {
174 public:
175  RTJmbOutParam(RTJmbOutParameter<T> *iPtr) : fPtr{iPtr} {} // NOLINT (not marked explicit on purpose)
176 
177  // getParamID
178  inline ParamID getParamID() const { return fPtr->getParamID(); }
179 
184  inline void broadcast(T const &iValue) { fPtr->broadcastValue(iValue); }
185 
190  template<class ElementModifier>
191  void broadcast(ElementModifier const &iElementModifier) { fPtr->broadcast(iElementModifier); }
192 
198  template<class ElementModifier>
199  bool broadcastIf(ElementModifier const &iElementModifier) { return fPtr->broadcastIf(iElementModifier); }
200 
201 private:
203 };
204 
205 }
206 }
207 }
void broadcast(T const &iValue)
Definition: RTJmbOutParameter.h:184
Definition: RTJmbOutParameter.h:172
Definition: RTJmbOutParameter.h:71
Definition: RTJmbOutParameter.h:35
Definition: ParamDef.h:199
ParamID getParamID() const
Definition: RTJmbOutParameter.h:45
Definition: Messaging.h:43
RTJmbOutParameter(std::shared_ptr< JmbParamDef< T >> iParamDef)
Definition: RTJmbOutParameter.h:76
void writeToStream(std::ostream &oStream) const override
Definition: RTJmbOutParameter.h:159
Definition: Clock.h:22
ParamID getParamID() const
Definition: RTJmbOutParameter.h:178
T ParamType
Definition: RTJmbOutParameter.h:74
void broadcast(ElementModifier const &iElementModifier)
Definition: RTJmbOutParameter.h:191
bool broadcastIf(ElementModifier const &iElementModifier)
Definition: RTJmbOutParameter.h:199
IJmbParamDef const * getParamDef() const
Definition: RTJmbOutParameter.h:42
bool hasUpdate() const override
Definition: RTJmbOutParameter.h:121
void broadcast(ElementModifier const &iElementModifier)
Definition: RTJmbOutParameter.h:103
IRTJmbOutParameter(std::shared_ptr< IJmbParamDef > iParamDef)
Definition: RTJmbOutParameter.h:39
tresult writeToMessage(Message &oMessage) override
Definition: RTJmbOutParameter.h:137
RTJmbOutParam(RTJmbOutParameter< T > *iPtr)
Definition: RTJmbOutParameter.h:175
RTJmbOutParameter< T > * fPtr
Definition: RTJmbOutParameter.h:202
static U dynamic(T *iPtr, typename std::enable_if<!std::is_polymorphic< T >::value >::type *=nullptr)
Definition: Metaprogramming.h:35
Definition: ParamDef.h:225
void broadcastValue(ParamType const &iValue)
Definition: RTJmbOutParameter.h:92
bool broadcastIf(ElementModifier const &iElementModifier)
Definition: RTJmbOutParameter.h:115
std::shared_ptr< IJmbParamDef > fParamDef
Definition: RTJmbOutParameter.h:60
JmbParamDef< T > const * getParamDefT() const
Definition: RTJmbOutParameter.h:82