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