Jamba C++ API 7.5.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 <memory>
25#include <public.sdk/source/vst/vstpresetfile.h>
28#include <string>
29#include <sstream>
30
31#include "ParamSerializers.h"
32
33namespace pongasoft::VST {
34
35using namespace Steinberg;
36using namespace Steinberg::Vst;
37
38static const auto ATTR_MSG_ID = "ATTR_MSG_ID";
39
40using MessageID = int;
41
46{
47public:
48 explicit Message(IMessage *message) : fMessage(message) {}
49
50 inline MessageID getMessageID() const
51 {
52 return static_cast<MessageID>(getInt(ATTR_MSG_ID, -1));
53 }
54
55 inline void setMessageID(MessageID messageID)
56 {
57 fMessage->getAttributes()->setInt(ATTR_MSG_ID, messageID);
58 }
59
60 inline int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
61 {
62 int64 value;
63 if(fMessage->getAttributes()->getInt(id, value) != kResultOk)
64 value = defaultValue;
65 return value;
66 }
67
68 inline void setInt(IAttributeList::AttrID id, int64 value)
69 {
70 fMessage->getAttributes()->setInt(id, value);
71 }
72
73 inline double getFloat(IAttributeList::AttrID id, double defaultValue) const
74 {
75 double value;
76 if(fMessage->getAttributes()->getFloat(id, value) != kResultOk)
77 value = defaultValue;
78 return value;
79 }
80
81 inline void setFloat(IAttributeList::AttrID id, double value)
82 {
83 fMessage->getAttributes()->setFloat(id, value);
84 }
85
93 template<typename T>
94 inline tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
95 {
96 return fMessage->getAttributes()->setBinary(id, iData, iSize * sizeof(T));
97 }
98
107 template<typename T>
108 inline int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const;
109
114 template<typename T>
115 tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer<T> const &iSerializer, T const &iValue);
116
121 template<typename T>
122 tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer<T> const &iSerializer, T &oValue) const;
123
124private:
125 IMessage *fMessage;
126};
127
128//------------------------------------------------------------------------
129// Message::getBinary
130//------------------------------------------------------------------------
131template<typename T>
132int32 Message::getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
133{
134 const void *data;
135 uint32 size;
136
137 if(fMessage->getAttributes()->getBinary(id, data, size) != kResultOk)
138 return -1;
139
140 uint32 oSize = size / sizeof(T);
141 oSize = std::min(iSize, oSize);
142
143 memcpy(iData, data, oSize * sizeof(T));
144
145 return oSize;
146}
147
148//------------------------------------------------------------------------
149// Message::setSerializableValue
150//------------------------------------------------------------------------
151template<typename T>
152tresult Message::setSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, const T &iValue)
153{
155
156 IBStreamer streamer{&stream};
157
158 tresult res = iSerializer.writeToStream(iValue, streamer);
159 if(res == kResultOk)
160 {
161 return setBinary(id, stream.getData(), static_cast<uint32>(stream.getSize()));
162 }
163 return res;
164}
165
166//------------------------------------------------------------------------
167// Message::getSerializableValue
168//------------------------------------------------------------------------
169template<typename T>
170tresult Message::getSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, T &oValue) const
171{
172 const void *data;
173 uint32 size;
174
175 tresult res = fMessage->getAttributes()->getBinary(id, data, size);
176
177 if(res != kResultOk)
178 return res;
179
180 VstUtils::ReadOnlyMemoryStream stream(static_cast<char const *>(data), size);
181 stream.seek(IBStream::kIBSeekSet, 0, nullptr); // make sure it is at the beginning of the stream
182
183 IBStreamer streamer{&stream};
184
185 return iSerializer.readFromStream(streamer, oValue);
186}
187
188}
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition ParamSerializers.h:109
virtual tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const
This method should read from the stream and populate oValue accordingly (aka deserialization).
Definition ParamSerializers.h:117
virtual tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const
This method should write iValue to the stream (aka serialization).
Definition ParamSerializers.h:123
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:152
Message(IMessage *message)
Definition Messaging.h:48
MessageID getMessageID() const
Definition Messaging.h:50
tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
Sets a binary message.
Definition Messaging.h:94
IMessage * fMessage
Definition Messaging.h:125
double getFloat(IAttributeList::AttrID id, double defaultValue) const
Definition Messaging.h:73
void setFloat(IAttributeList::AttrID id, double value)
Definition Messaging.h:81
int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
Gets a binary message.
Definition Messaging.h:132
void setInt(IAttributeList::AttrID id, int64 value)
Definition Messaging.h:68
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:170
int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
Definition Messaging.h:60
void setMessageID(MessageID messageID)
Definition Messaging.h:55
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:38
int MessageID
Definition Messaging.h:40