Jamba  3.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 {
31 namespace VST {
32 
33 using namespace Steinberg;
34 using namespace Steinberg::Vst;
35 
36 static const auto ATTR_MSG_ID = "ATTR_MSG_ID";
37 
38 using MessageID = int;
39 
43 class Message
44 {
45 public:
46  explicit Message(IMessage *message) : fMessage(message) {}
47 
48  inline MessageID getMessageID() const
49  {
50  return static_cast<MessageID>(getInt(ATTR_MSG_ID, -1));
51  }
52 
53  inline void setMessageID(MessageID messageID)
54  {
55  fMessage->getAttributes()->setInt(ATTR_MSG_ID, messageID);
56  }
57 
58  inline int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
59  {
60  int64 value;
61  if(fMessage->getAttributes()->getInt(id, value) != kResultOk)
62  value = defaultValue;
63  return value;
64  }
65 
66  inline void setInt(IAttributeList::AttrID id, int64 value)
67  {
68  fMessage->getAttributes()->setInt(id, value);
69  }
70 
71  inline double getFloat(IAttributeList::AttrID id, double defaultValue) const
72  {
73  double value;
74  if(fMessage->getAttributes()->getFloat(id, value) != kResultOk)
75  value = defaultValue;
76  return value;
77  }
78 
79  inline void setFloat(IAttributeList::AttrID id, double value)
80  {
81  fMessage->getAttributes()->setFloat(id, value);
82  }
83 
88  template<typename T>
89  inline tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
90  {
91  return fMessage->getAttributes()->setBinary(id, iData, iSize * sizeof(T));
92  }
93 
100  template<typename T>
101  inline int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const;
102 
107  template<typename T>
108  tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer<T> const &iSerializer, T const &iValue);
109 
114  template<typename T>
115  tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer<T> const &iSerializer, T &oValue) const;
116 
117 private:
118  IMessage *fMessage;
119 };
120 
121 //------------------------------------------------------------------------
122 // Message::getBinary
123 //------------------------------------------------------------------------
124 template<typename T>
125 int32 Message::getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
126 {
127  const void *data;
128  uint32 size;
129 
130  if(fMessage->getAttributes()->getBinary(id, data, size) != kResultOk)
131  return -1;
132 
133  uint32 oSize = size / sizeof(T);
134  oSize = std::min(iSize, oSize);
135 
136  memcpy(iData, data, oSize * sizeof(T));
137 
138  return oSize;
139 }
140 
144 class BufferStream : public Steinberg::Vst::BufferStream
145 {
146 public:
147  BufferStream() : Steinberg::Vst::BufferStream() {}
148  explicit BufferStream(Buffer &&iFrom) : Steinberg::Vst::BufferStream()
149  {
150  mBuffer.take(iFrom);
151  }
152  Buffer const &getBuffer() const { return mBuffer; }
153 };
154 
155 //------------------------------------------------------------------------
156 // Message::setSerializableValue
157 //------------------------------------------------------------------------
158 template<typename T>
159 tresult Message::setSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, const T &iValue)
160 {
161  BufferStream stream{};
162 
163  IBStreamer streamer{&stream};
164 
165  tresult res = iSerializer.writeToStream(iValue, streamer);
166  if(res == kResultOk)
167  {
168  auto const &buffer = stream.getBuffer();
169  return setBinary(id, buffer.int8Ptr(), buffer.getFillSize());
170  }
171  return res;
172 }
173 
174 //------------------------------------------------------------------------
175 // Message::getSerializableValue
176 //------------------------------------------------------------------------
177 template<typename T>
178 tresult Message::getSerializableValue(IAttributeList::AttrID id, const IParamSerializer<T> &iSerializer, T &oValue) const
179 {
180  const void *data;
181  uint32 size;
182 
183  tresult res = fMessage->getAttributes()->getBinary(id, data, size);
184 
185  if(res != kResultOk)
186  return res;
187 
189  Buffer buffer(data, size);
190 
191  BufferStream stream{std::move(buffer)};
192  stream.seek(IBStream::kIBSeekSet, 0, nullptr); // make sure it is at the beginning of the stream
193 
194  IBStreamer streamer{&stream};
195 
196  return iSerializer.readFromStream(streamer, oValue);
197 }
198 
199 }
200 }
int64 getInt(IAttributeList::AttrID id, int64 defaultValue) const
Definition: Messaging.h:58
int32 getBinary(IAttributeList::AttrID id, T *iData, uint32 iSize) const
Definition: Messaging.h:125
IMessage * fMessage
Definition: Messaging.h:118
void setInt(IAttributeList::AttrID id, int64 value)
Definition: Messaging.h:66
Definition: Messaging.h:43
Definition: Clock.h:22
void setMessageID(MessageID messageID)
Definition: Messaging.h:53
tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T &oValue) const
Definition: Messaging.h:178
MessageID getMessageID() const
Definition: Messaging.h:48
virtual tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const =0
Message(IMessage *message)
Definition: Messaging.h:46
void setFloat(IAttributeList::AttrID id, double value)
Definition: Messaging.h:79
BufferStream()
Definition: Messaging.h:147
Definition: Messaging.h:144
BufferStream(Buffer &&iFrom)
Definition: Messaging.h:148
Definition: ParamSerializers.h:43
tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T const &iValue)
Definition: Messaging.h:159
tresult setBinary(IAttributeList::AttrID id, const T *iData, uint32 iSize)
Definition: Messaging.h:89
static const auto ATTR_MSG_ID
Definition: Messaging.h:36
double getFloat(IAttributeList::AttrID id, double defaultValue) const
Definition: Messaging.h:71
int MessageID
Definition: Messaging.h:38
virtual tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const =0
Buffer const & getBuffer() const
Definition: Messaging.h:152