Jamba C++ API  5.0.0
RTJmbInParameter.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 
22 #include <pongasoft/VST/ParamDef.h>
24 
25 namespace pongasoft::VST::RT {
26 
27 using namespace Utils;
28 
33 {
34 public:
35  // Constructor
36  explicit IRTJmbInParameter(std::shared_ptr<IJmbParamDef> iParamDef) : fParamDef{std::move(iParamDef)} {}
37 
38  // getParamDef
39  inline IJmbParamDef const *getParamDef() const { return fParamDef.get(); }
40 
41  // getParamID
42  ParamID getParamID() const { return fParamDef->fParamID; }
43 
44  // destructor
45  ~IRTJmbInParameter() override = default;
46 
47  // hasUpdate
48  virtual bool hasUpdate() const = 0;
49 
50  // readFromMessage
51  virtual tresult readFromMessage(Message const &iMessage) = 0;
52 
53  // handleMessage
54  tresult handleMessage(Message const &iMessage) override { return readFromMessage(iMessage); }
55 
56  // writeToStream
57  virtual void writeToStream(std::ostream &oStream) const = 0;
58 
59 protected:
60  std::shared_ptr<IJmbParamDef> fParamDef;
61 };
62 
69 template<typename T>
71 {
72 public:
73  using ParamType = T;
74 
75  explicit RTJmbInParameter(std::shared_ptr<JmbParamDef<T>> iParamDef) :
76  IRTJmbInParameter(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 
86  // pop
87  inline ParamType *pop() { return fUpdateQueue.pop(); }
88 
89  // last
90  inline ParamType const *last() const { return fUpdateQueue.last(); }
91 
92  // popOrLast
93  inline ParamType const *popOrLast() { return fUpdateQueue.popOrLast(); }
94 
95  // hasUpdate
96  bool hasUpdate() const override { return !fUpdateQueue.isEmpty(); }
97 
98  // readFromMessage - called to extract the value from the message
99  tresult readFromMessage(Message const &iMessage) override;
100 
101  // writeToStream
102  void writeToStream(std::ostream &oStream) const override;
103 
104 private:
106 };
107 
108 //------------------------------------------------------------------------
109 // RTJmbInParameter::readFromMessage
110 //------------------------------------------------------------------------
111 template<typename T>
113 {
114  bool res = fUpdateQueue.updateAndPushIf([this, &iMessage](auto oUpdate) -> bool {
115  return getParamDefT()->readFromMessage(iMessage, *oUpdate) == kResultOk;
116  });
117 
118  return res ? kResultOk : kResultFalse;
119 }
120 
121 //------------------------------------------------------------------------
122 // RTJmbInParameter::writeToStream
123 //------------------------------------------------------------------------
124 template<typename T>
125 void RTJmbInParameter<T>::writeToStream(std::ostream &oStream) const
126 {
127  getParamDefT()->writeToStream(*last(), oStream);
128 }
129 
130 //------------------------------------------------------------------------
131 // RTJmbInParam - wrapper to make writing the code much simpler and natural
132 //------------------------------------------------------------------------
139 template<typename T>
141 {
142 public:
143  RTJmbInParam(RTJmbInParameter<T> *iPtr) : fPtr{iPtr} // NOLINT (not marked explicit on purpose)
144  {
145  DCHECK_F(fPtr != nullptr);
146  }
147 
148  // getParamID
149  inline ParamID getParamID() const { return fPtr->getParamID(); }
150 
151  // pop - nullptr if no update
152  inline T *pop() { return fPtr->pop(); }
153 
154  // last - last value popped (does not pop!)
155  inline T const *last() const { return fPtr->last(); }
156 
157  // popOrLast - call pop and no new value then call last
158  inline T const *popOrLast() { return fPtr->popOrLast(); }
159 
160  // hasUpdate
161  bool hasUpdate() const { return fPtr->hasUpdate(); }
162 
163  // getValue
164  inline T const &getValue() const { return *fPtr->last(); }
165 
166  // value - synonym
167  inline T const &value() const { return *fPtr->last(); }
168 
169  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
170  inline operator T const &() const { return *fPtr->last(); } // NOLINT
171 
172  // allow writing param->xxx to access the underlying type directly (if not a primitive)
173  inline T const *operator->() const { return fPtr->last(); }
174 
175 private:
177 };
178 
179 }
ParamType const * last() const
Definition: RTJmbInParameter.h:90
Simple wrapper class with better api.
Definition: Messaging.h:44
ParamID getParamID() const
Definition: RTJmbInParameter.h:42
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTJmbInParameter.h:140
T * pop()
Definition: RTJmbInParameter.h:152
RTJmbInParameter< T > * fPtr
Definition: RTJmbInParameter.h:176
Interface defining a message handler.
Definition: MessageHandler.h:30
RTJmbInParameter(std::shared_ptr< JmbParamDef< T >> iParamDef)
Definition: RTJmbInParameter.h:75
ParamID getParamID() const
Definition: RTJmbInParameter.h:149
T const * last() const
Definition: RTJmbInParameter.h:155
T const * popOrLast()
Definition: RTJmbInParameter.h:158
T const * operator->() const
Definition: RTJmbInParameter.h:173
Definition: RTJmbInParameter.h:25
void writeToStream(std::ostream &oStream) const override
Definition: RTJmbInParameter.h:125
ParamType const * popOrLast()
Definition: RTJmbInParameter.h:93
IJmbParamDef const * getParamDef() const
Definition: RTJmbInParameter.h:39
Templated class for RT Jamba Inbound parameter.
Definition: RTJmbInParameter.h:70
bool hasUpdate() const override
Definition: RTJmbInParameter.h:96
T const & getValue() const
Definition: RTJmbInParameter.h:164
bool hasUpdate() const
Definition: RTJmbInParameter.h:161
Base (non templated) class for RT Jamba (Inbound) parameters.
Definition: RTJmbInParameter.h:32
JmbParamDef< T > const * getParamDefT() const
Definition: RTJmbInParameter.h:81
tresult handleMessage(Message const &iMessage) override
Definition: RTJmbInParameter.h:54
T const & value() const
Definition: RTJmbInParameter.h:167
tresult readFromMessage(Message const &iMessage) override
Definition: RTJmbInParameter.h:112
IRTJmbInParameter(std::shared_ptr< IJmbParamDef > iParamDef)
Definition: RTJmbInParameter.h:36
T ParamType
Definition: RTJmbInParameter.h:73
RTJmbInParam(RTJmbInParameter< T > *iPtr)
Definition: RTJmbInParameter.h:143
std::shared_ptr< IJmbParamDef > fParamDef
Definition: RTJmbInParameter.h:60
This is the lock free version of the SingleElementQueue.
Definition: Concurrent.h:148
ParamType * pop()
Definition: RTJmbInParameter.h:87