Jamba C++ API  4.2.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>
25 #include <string>
26 #include <sstream>
27 
28 #include "ParamSerializers.h"
29 
30 namespace pongasoft::VST {
31 
32 using namespace Steinberg;
33 using namespace Steinberg::Vst;
34 
35 static const auto ATTR_MSG_ID = "ATTR_MSG_ID";
36 
37 using MessageID = int;
38 
42 class Message
43 {
44 public:
45  explicit Message(IMessage *message) : fMessage(message) {}
46 
47  inline MessageID getMessageID() const
48  {
49  return static_cast<MessageID>(getInt(ATTR_MSG_ID, -1));
50  }
51 
52  inline void setMessageID(MessageID messageID)
53  {
54  fMessage->getAttributes()->setInt(ATTR_MSG_ID, messageID);
55  }
56 
57  inline 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  inline void setInt(IAttributeList::AttrID id, int64 value)
66  {
67  fMessage->getAttributes()->setInt(id, value);
68  }
69 
70  inline 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  inline void setFloat(IAttributeList::AttrID id, double value)
79  {
80  fMessage->getAttributes()->setFloat(id, value);
81  }
82 
90  template<typename T>
91  inline 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  inline 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 
121 private:
122  IMessage *fMessage;
123 };
124 
125 //------------------------------------------------------------------------
126 // Message::getBinary
127 //------------------------------------------------------------------------
128 template<typename T>
129 int32 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 
148 class BufferStream : public Steinberg::Vst::BufferStream
149 {
150 public:
151  BufferStream() : Steinberg::Vst::BufferStream() {}
152  explicit BufferStream(Buffer &&iFrom) : Steinberg::Vst::BufferStream()
153  {
154  mBuffer.take(iFrom);
155  }
156  Buffer const &getBuffer() const { return mBuffer; }
157 };
158 
159 //------------------------------------------------------------------------
160 // Message::setSerializableValue
161 //------------------------------------------------------------------------
162 template<typename T>
163 tresult Message::setSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, const T &iValue)
164 {
165  BufferStream stream{};
166 
167  IBStreamer streamer{&stream};
168 
169  tresult res = iSerializer.writeToStream(iValue, streamer);
170  if(res == kResultOk)
171  {
172  auto const &buffer = stream.getBuffer();
173  return setBinary(id, buffer.int8Ptr(), buffer.getFillSize());
174  }
175  return res;
176 }
177 
178 //------------------------------------------------------------------------
179 // Message::getSerializableValue
180 //------------------------------------------------------------------------
181 template<typename T>
182 tresult Message::getSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, T &oValue) const
183 {
184  const void *data;
185  uint32 size;
186 
187  tresult res = fMessage->getAttributes()->getBinary(id, data, size);
188 
189  if(res != kResultOk)
190  return res;
191 
193  Buffer buffer(data, size);
194 
195  BufferStream stream{std::move(buffer)};
196  stream.seek(IBStream::kIBSeekSet, 0, nullptr); // make sure it is at the beginning of the stream
197 
198  IBStreamer streamer{&stream};
199 
200  return iSerializer.readFromStream(streamer, oValue);
201 }
202 
203 }
IMessage * fMessage
Definition: Messaging.h:122
BufferStream(Buffer &&iFrom)
Definition: Messaging.h:152
int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
Gets a binary message.
Definition: Messaging.h:129
virtual tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const =0
Simple wrapper class with better api.
Definition: Messaging.h:42
BufferStream()
Definition: Messaging.h:151
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:163
void setInt(IAttributeList::AttrID id, int64 value)
Definition: Messaging.h:65
Message(IMessage *message)
Definition: Messaging.h:45
MessageID getMessageID() const
Definition: Messaging.h:47
static const auto ATTR_MSG_ID
Definition: Messaging.h:35
double getFloat(IAttributeList::AttrID id, double defaultValue) const
Definition: Messaging.h:70
void setMessageID(MessageID messageID)
Definition: Messaging.h:52
void setFloat(IAttributeList::AttrID id, double value)
Definition: Messaging.h:78
tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
Sets a binary message.
Definition: Messaging.h:91
Internal class to override the BufferStream class and give access to the buffer.
Definition: Messaging.h:148
int MessageID
Definition: Messaging.h:37
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition: ParamSerializers.h:105
Buffer const & getBuffer() const
Definition: Messaging.h:156
virtual tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const =0
Definition: Clock.h:23
int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
Definition: Messaging.h:57
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:182