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