Jamba C++ API 8.0.0
Loading...
Searching...
No Matches
Messaging.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2019 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#pragma once
20
21#include <pluginterfaces/base/ftypes.h>
22#include <pluginterfaces/vst/ivstmessage.h>
23#include <algorithm>
24#include <public.sdk/source/vst/vstpresetfile.h>
27
28#include "ParamSerializers.h"
29
30namespace pongasoft::VST {
31
32using namespace Steinberg;
33using namespace Steinberg::Vst;
34
35static const auto ATTR_MSG_ID = "ATTR_MSG_ID";
36
37using MessageID = int;
38
43{
44public:
45 explicit Message(IMessage *message) : fMessage(message) {}
46
48 {
49 return static_cast<MessageID>(getInt(ATTR_MSG_ID, -1));
50 }
51
52 void setMessageID(MessageID messageID)
53 {
54 fMessage->getAttributes()->setInt(ATTR_MSG_ID, messageID);
55 }
56
57 int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
58 {
59 int64 value;
60 if(fMessage->getAttributes()->getInt(id, value) != kResultOk)
61 value = defaultValue;
62 return value;
63 }
64
65 void setInt(IAttributeList::AttrID id, int64 value)
66 {
67 fMessage->getAttributes()->setInt(id, value);
68 }
69
70 double getFloat(IAttributeList::AttrID id, double defaultValue) const
71 {
72 double value;
73 if(fMessage->getAttributes()->getFloat(id, value) != kResultOk)
74 value = defaultValue;
75 return value;
76 }
77
78 void setFloat(IAttributeList::AttrID id, double value)
79 {
80 fMessage->getAttributes()->setFloat(id, value);
81 }
82
90 template<typename T>
91 tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
92 {
93 return fMessage->getAttributes()->setBinary(id, iData, iSize * sizeof(T));
94 }
95
104 template<typename T>
105 int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const;
106
111 template<typename T>
112 tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer<T> const &iSerializer, T const &iValue);
113
118 template<typename T>
119 tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer<T> const &iSerializer, T &oValue) const;
120
121private:
122 IMessage *fMessage;
123};
124
125//------------------------------------------------------------------------
126// Message::getBinary
127//------------------------------------------------------------------------
128template<typename T>
129int32 Message::getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
130{
131 const void *data;
132 uint32 size;
133
134 if(fMessage->getAttributes()->getBinary(id, data, size) != kResultOk)
135 return -1;
136
137 uint32 oSize = size / sizeof(T);
138 oSize = std::min(iSize, oSize);
139
140 memcpy(iData, data, oSize * sizeof(T));
141
142 return oSize;
143}
144
145//------------------------------------------------------------------------
146// Message::setSerializableValue
147//------------------------------------------------------------------------
148template<typename T>
149tresult Message::setSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, const T &iValue)
150{
152
153 IBStreamer streamer{&stream};
154
155 tresult res = iSerializer.writeToStream(iValue, streamer);
156 if(res == kResultOk)
157 {
158 return setBinary(id, stream.getData(), static_cast<uint32>(stream.getSize()));
159 }
160 return res;
161}
162
163//------------------------------------------------------------------------
164// Message::getSerializableValue
165//------------------------------------------------------------------------
166template<typename T>
167tresult Message::getSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, T &oValue) const
168{
169 const void *data;
170 uint32 size;
171
172 tresult res = fMessage->getAttributes()->getBinary(id, data, size);
173
174 if(res != kResultOk)
175 return res;
176
177 VstUtils::ReadOnlyMemoryStream stream(static_cast<char const *>(data), size);
178 stream.seek(IBStream::kIBSeekSet, 0, nullptr); // make sure it is at the beginning of the stream
179
180 IBStreamer streamer{&stream};
181
182 return iSerializer.readFromStream(streamer, oValue);
183}
184
185}
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition ParamSerializers.h:111
virtual tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const
This method should read from the stream and populate oValue accordingly (aka deserialization).
Definition ParamSerializers.h:122
virtual tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const
This method should write iValue to the stream (aka serialization).
Definition ParamSerializers.h:128
tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T const &iValue)
Serializes the parameter value as an entry in the message.
Definition Messaging.h:149
Message(IMessage *message)
Definition Messaging.h:45
MessageID getMessageID() const
Definition Messaging.h:47
tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
Sets a binary message.
Definition Messaging.h:91
IMessage * fMessage
Definition Messaging.h:122
double getFloat(IAttributeList::AttrID id, double defaultValue) const
Definition Messaging.h:70
void setFloat(IAttributeList::AttrID id, double value)
Definition Messaging.h:78
int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
Gets a binary message.
Definition Messaging.h:129
void setInt(IAttributeList::AttrID id, int64 value)
Definition Messaging.h:65
tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T &oValue) const
Deserializes the parameter value from an entry in the message.
Definition Messaging.h:167
int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
Definition Messaging.h:57
void setMessageID(MessageID messageID)
Definition Messaging.h:52
This class is a copy of MemoryStream for the purpose of fixing the growing issue encountered on Windo...
Definition FastWriteMemoryStream.h:43
TSize getSize() const
Definition FastWriteMemoryStream.h:56
char const * getData() const
Definition FastWriteMemoryStream.h:60
Definition ReadOnlyMemoryStream.h:30
virtual tresult PLUGIN_API seek(int64 pos, int32 mode, int64 *result) SMTG_OVERRIDE
Definition ReadOnlyMemoryStream.cpp:88
Definition Clock.h:24
static const auto ATTR_MSG_ID
Definition Messaging.h:35
int MessageID
Definition Messaging.h:37