JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
hostclasses.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : VST SDK
3//
4// Category : Helpers
5// Filename : public.sdk/source/vst/hosting/hostclasses.cpp
6// Created by : Steinberg, 03/05/2008.
7// Description : VST 3 hostclasses, example impl. for IHostApplication, IAttributeList and IMessage
8//
9//-----------------------------------------------------------------------------
10// LICENSE
11// (c) 2023, Steinberg Media Technologies GmbH, All Rights Reserved
12//-----------------------------------------------------------------------------
13// Redistribution and use in source and binary forms, with or without modification,
14// are permitted provided that the following conditions are met:
15//
16// * Redistributions of source code must retain the above copyright notice,
17// this list of conditions and the following disclaimer.
18// * Redistributions in binary form must reproduce the above copyright notice,
19// this list of conditions and the following disclaimer in the documentation
20// and/or other materials provided with the distribution.
21// * Neither the name of the Steinberg Media Technologies nor the names of its
22// contributors may be used to endorse or promote products derived from this
23// software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34// OF THE POSSIBILITY OF SUCH DAMAGE.
35//-----------------------------------------------------------------------------
36
37#include "hostclasses.h"
39
40#include <algorithm>
41
42namespace Steinberg {
43namespace Vst {
44
45//-----------------------------------------------------------------------------
46HostApplication::HostApplication ()
47{
48 FUNKNOWN_CTOR
49
50 mPlugInterfaceSupport = owned (new PlugInterfaceSupport);
51}
52
53//-----------------------------------------------------------------------------
54tresult PLUGIN_API HostApplication::getName (String128 name)
55{
56 return VST3::StringConvert::convert ("My VST3 HostApplication", name) ? kResultTrue :
57 kInternalError;
58}
59
60//-----------------------------------------------------------------------------
61tresult PLUGIN_API HostApplication::createInstance (TUID cid, TUID _iid, void** obj)
62{
63 if (FUnknownPrivate::iidEqual (cid, IMessage::iid) &&
64 FUnknownPrivate::iidEqual (_iid, IMessage::iid))
65 {
66 *obj = new HostMessage;
67 return kResultTrue;
68 }
69 if (FUnknownPrivate::iidEqual (cid, IAttributeList::iid) &&
70 FUnknownPrivate::iidEqual (_iid, IAttributeList::iid))
71 {
72 if (auto al = HostAttributeList::make ())
73 {
74 *obj = al.take ();
75 return kResultTrue;
76 }
77 return kOutOfMemory;
78 }
79 *obj = nullptr;
80 return kResultFalse;
81}
82
83//-----------------------------------------------------------------------------
84tresult PLUGIN_API HostApplication::queryInterface (const char* _iid, void** obj)
85{
86 QUERY_INTERFACE (_iid, obj, FUnknown::iid, IHostApplication)
87 QUERY_INTERFACE (_iid, obj, IHostApplication::iid, IHostApplication)
88
89 if (mPlugInterfaceSupport && mPlugInterfaceSupport->queryInterface (iid, obj) == kResultTrue)
90 return kResultOk;
91
92 *obj = nullptr;
93 return kResultFalse;
94}
95
96//-----------------------------------------------------------------------------
97uint32 PLUGIN_API HostApplication::addRef ()
98{
99 return 1;
100}
101
102//-----------------------------------------------------------------------------
103uint32 PLUGIN_API HostApplication::release ()
104{
105 return 1;
106}
107
108//-----------------------------------------------------------------------------
109//-----------------------------------------------------------------------------
110//-----------------------------------------------------------------------------
111IMPLEMENT_FUNKNOWN_METHODS (HostMessage, IMessage, IMessage::iid)
112//-----------------------------------------------------------------------------
113HostMessage::HostMessage () {FUNKNOWN_CTOR}
114
115//-----------------------------------------------------------------------------
116HostMessage::~HostMessage () noexcept
117{
118 setMessageID (nullptr);
119 FUNKNOWN_DTOR
120}
121
122//-----------------------------------------------------------------------------
123const char* PLUGIN_API HostMessage::getMessageID ()
124{
125 return messageId;
126}
127
128//-----------------------------------------------------------------------------
129void PLUGIN_API HostMessage::setMessageID (const char* mid)
130{
131 if (messageId)
132 delete[] messageId;
133 messageId = nullptr;
134 if (mid)
135 {
136 size_t len = strlen (mid) + 1;
137 messageId = new char[len];
138 strcpy (messageId, mid);
139 }
140}
141
142//-----------------------------------------------------------------------------
144{
145 if (!attributeList)
146 attributeList = HostAttributeList::make ();
147 return attributeList;
148}
149
150//-----------------------------------------------------------------------------
151//-----------------------------------------------------------------------------
152//-----------------------------------------------------------------------------
154{
155 enum class Type
156 {
157 kUninitialized,
158 kInteger,
159 kFloat,
160 kString,
161 kBinary
162 };
163 Attribute () = default;
164
165 Attribute (int64 value) : type (Type::kInteger) { v.intValue = value; }
166 Attribute (double value) : type (Type::kFloat) { v.floatValue = value; }
167 /* size is in code unit (count of TChar) */
168 Attribute (const TChar* value, uint32 sizeInCodeUnit)
169 : size (sizeInCodeUnit), type (Type::kString)
170 {
171 v.stringValue = new TChar[sizeInCodeUnit];
172 memcpy (v.stringValue, value, sizeInCodeUnit * sizeof (TChar));
173 }
174 Attribute (const void* value, uint32 sizeInBytes) : size (sizeInBytes), type (Type::kBinary)
175 {
176 v.binaryValue = new char[sizeInBytes];
177 memcpy (v.binaryValue, value, sizeInBytes);
178 }
179 Attribute (Attribute&& o) { *this = std::move (o); }
180 Attribute& operator= (Attribute&& o)
181 {
182 v = o.v;
183 size = o.size;
184 type = o.type;
185 o.size = 0;
186 o.type = Type::kUninitialized;
187 o.v = {};
188 return *this;
189 }
190 ~Attribute () noexcept
191 {
192 if (size)
193 delete[] v.binaryValue;
194 }
195
196 int64 intValue () const { return v.intValue; }
197 double floatValue () const { return v.floatValue; }
198 /* sizeInCodeUnit is in code unit (count of TChar) */
199 const TChar* stringValue (uint32& sizeInCodeUnit)
200 {
201 sizeInCodeUnit = size;
202 return v.stringValue;
203 }
204 const void* binaryValue (uint32& sizeInBytes)
205 {
206 sizeInBytes = size;
207 return v.binaryValue;
208 }
209
210 Type getType () const { return type; }
211
212private:
213 union v
214 {
215 int64 intValue;
216 double floatValue;
217 TChar* stringValue;
218 char* binaryValue;
219 } v;
220 uint32 size {0};
221 Type type {Type::kUninitialized};
222};
223
224//-----------------------------------------------------------------------------
225//-----------------------------------------------------------------------------
226//-----------------------------------------------------------------------------
227IMPLEMENT_FUNKNOWN_METHODS (HostAttributeList, IAttributeList, IAttributeList::iid)
228
229//-----------------------------------------------------------------------------
231{
232 return owned (new HostAttributeList);
233}
234
235//-----------------------------------------------------------------------------
236HostAttributeList::HostAttributeList () {FUNKNOWN_CTOR}
237
238//-----------------------------------------------------------------------------
239HostAttributeList::~HostAttributeList () noexcept
240{
241 FUNKNOWN_DTOR
242}
243
244//-----------------------------------------------------------------------------
245tresult PLUGIN_API HostAttributeList::setInt (AttrID aid, int64 value)
246{
247 if (!aid)
248 return kInvalidArgument;
249 list[aid] = Attribute (value);
250 return kResultTrue;
251}
252
253//-----------------------------------------------------------------------------
254tresult PLUGIN_API HostAttributeList::getInt (AttrID aid, int64& value)
255{
256 if (!aid)
257 return kInvalidArgument;
258 auto it = list.find (aid);
259 if (it != list.end () && it->second.getType () == Attribute::Type::kInteger)
260 {
261 value = it->second.intValue ();
262 return kResultTrue;
263 }
264 return kResultFalse;
265}
266
267//-----------------------------------------------------------------------------
268tresult PLUGIN_API HostAttributeList::setFloat (AttrID aid, double value)
269{
270 if (!aid)
271 return kInvalidArgument;
272 list[aid] = Attribute (value);
273 return kResultTrue;
274}
275
276//-----------------------------------------------------------------------------
277tresult PLUGIN_API HostAttributeList::getFloat (AttrID aid, double& value)
278{
279 if (!aid)
280 return kInvalidArgument;
281 auto it = list.find (aid);
282 if (it != list.end () && it->second.getType () == Attribute::Type::kFloat)
283 {
284 value = it->second.floatValue ();
285 return kResultTrue;
286 }
287 return kResultFalse;
288}
289
290//-----------------------------------------------------------------------------
291tresult PLUGIN_API HostAttributeList::setString (AttrID aid, const TChar* string)
292{
293 if (!aid)
294 return kInvalidArgument;
295 // + 1 for the null-terminate
296 auto length = tstrlen (string) + 1;
297 list[aid] = Attribute (string, length);
298 return kResultTrue;
299}
300
301//-----------------------------------------------------------------------------
302tresult PLUGIN_API HostAttributeList::getString (AttrID aid, TChar* string, uint32 sizeInBytes)
303{
304 if (!aid)
305 return kInvalidArgument;
306 auto it = list.find (aid);
307 if (it != list.end () && it->second.getType () == Attribute::Type::kString)
308 {
309 uint32 sizeInCodeUnit = 0;
310 const TChar* _string = it->second.stringValue (sizeInCodeUnit);
311 memcpy (string, _string, std::min<uint32> (sizeInCodeUnit * sizeof (TChar), sizeInBytes));
312 return kResultTrue;
313 }
314 return kResultFalse;
315}
316
317//-----------------------------------------------------------------------------
318tresult PLUGIN_API HostAttributeList::setBinary (AttrID aid, const void* data, uint32 sizeInBytes)
319{
320 if (!aid)
321 return kInvalidArgument;
322 list[aid] = Attribute (data, sizeInBytes);
323 return kResultTrue;
324}
325
326//-----------------------------------------------------------------------------
327tresult PLUGIN_API HostAttributeList::getBinary (AttrID aid, const void*& data, uint32& sizeInBytes)
328{
329 if (!aid)
330 return kInvalidArgument;
331 auto it = list.find (aid);
332 if (it != list.end () && it->second.getType () == Attribute::Type::kBinary)
333 {
334 data = it->second.binaryValue (sizeInBytes);
335 return kResultTrue;
336 }
337 sizeInBytes = 0;
338 return kResultFalse;
339}
340
341} // Vst
342} // Steinberg
virtual uint32 PLUGIN_API addRef()=0
Adds a reference and returns the new reference count.
virtual uint32 PLUGIN_API release()=0
Releases a reference and returns the new reference count.
virtual tresult PLUGIN_API queryInterface(const TUID _iid, void **obj)=0
Query for a pointer to the specified interface.
IPtr - Smart pointer template class.
tresult PLUGIN_API getName(String128 name) override
Gets host application name.
tresult PLUGIN_API createInstance(TUID cid, TUID _iid, void **obj) override
Creates host object (e.g.
Example, ready to use implementation of IAttributeList.
Definition hostclasses.h:75
tresult PLUGIN_API getInt(AttrID aid, int64 &value) override
Gets integer value.
tresult PLUGIN_API setString(AttrID aid, const TChar *string) override
Sets string value (UTF16) (must be null-terminated!).
tresult PLUGIN_API getBinary(AttrID aid, const void *&data, uint32 &sizeInBytes) override
Gets binary data.
static IPtr< IAttributeList > make()
make a new attribute list instance
tresult PLUGIN_API setInt(AttrID aid, int64 value) override
Sets integer value.
tresult PLUGIN_API setFloat(AttrID aid, double value) override
Sets float value.
tresult PLUGIN_API getString(AttrID aid, TChar *string, uint32 sizeInBytes) override
Gets string value (UTF16).
tresult PLUGIN_API setBinary(AttrID aid, const void *data, uint32 sizeInBytes) override
Sets binary data.
tresult PLUGIN_API getFloat(AttrID aid, double &value) override
Gets float value.
Example implementation of IMessage.
IAttributeList *PLUGIN_API getAttributes() override
Returns the attribute list associated to the message.
const char *PLUGIN_API getMessageID() override
Returns the message ID (for example "TextMessage").
Attribute list used in IMessage and IStreamAttributes: Vst::IAttributeList.
Basic host callback interface: Vst::IHostApplication.
memcpy
TChar String128[128]
128 character UTF-16 string
Definition vsttypes.h:69
char16 TChar
UTF-16 character.
Definition vsttypes.h:68
IPtr< I > owned(I *p)
Assigning newly created object to an IPtr.
strcpy
strlen