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
uid.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : VST SDK
3//
4// Category : Helpers
5// Filename : public.sdk/source/vst/utility/uid.h
6// Created by : Steinberg, 08/2016
7// Description : UID
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#pragma once
38
39#include "optional.h"
41#include <string>
42
43//------------------------------------------------------------------------
44namespace VST3 {
45
46//------------------------------------------------------------------------
47struct UID
48{
49#if defined(SMTG_OS_WINDOWS) && SMTG_OS_WINDOWS == 1
50 static constexpr bool defaultComFormat = true;
51#else
52 static constexpr bool defaultComFormat = false;
53#endif
54
55 using TUID = Steinberg::TUID;
56
57 constexpr UID () noexcept = default;
58 UID (uint32_t l1, uint32_t l2, uint32_t l3, uint32_t l4, bool comFormat = defaultComFormat)
59 noexcept;
60 UID (const TUID& uid) noexcept;
61 UID (const UID& uid) noexcept;
62 UID& operator= (const UID& uid) noexcept;
63 UID& operator= (const TUID& uid) noexcept;
64
65 constexpr const TUID& data () const noexcept;
66 constexpr size_t size () const noexcept;
67
68 std::string toString (bool comFormat = defaultComFormat) const noexcept;
69
70 template<typename StringT>
71 static Optional<UID> fromString (const StringT& str,
72 bool comFormat = defaultComFormat) noexcept;
73
74 static UID fromTUID (const TUID _uid) noexcept;
75//------------------------------------------------------------------------
76private:
77 Steinberg::TUID _data {};
78
79 struct GUID
80 {
81 uint32_t Data1;
82 uint16_t Data2;
83 uint16_t Data3;
84 uint8_t Data4[8];
85 };
86};
87
88//------------------------------------------------------------------------
89inline bool operator== (const UID& uid1, const UID& uid2)
90{
91 const uint64_t* p1 = reinterpret_cast<const uint64_t*> (uid1.data ());
92 const uint64_t* p2 = reinterpret_cast<const uint64_t*> (uid2.data ());
93 return p1[0] == p2[0] && p1[1] == p2[1];
94}
95
96//------------------------------------------------------------------------
97inline bool operator!= (const UID& uid1, const UID& uid2)
98{
99 return !(uid1 == uid2);
100}
101
102//------------------------------------------------------------------------
103inline bool operator< (const UID& uid1, const UID& uid2)
104{
105 const uint64_t* p1 = reinterpret_cast<const uint64_t*> (uid1.data ());
106 const uint64_t* p2 = reinterpret_cast<const uint64_t*> (uid2.data ());
107 return (p1[0] < p2[0]) && (p1[1] < p2[1]);
108}
109
110//------------------------------------------------------------------------
111inline UID::UID (uint32_t l1, uint32_t l2, uint32_t l3, uint32_t l4, bool comFormat) noexcept
112{
113 if (comFormat)
114 {
115 _data[0] = static_cast<int8_t> ((l1 & 0x000000FF));
116 _data[1] = static_cast<int8_t> ((l1 & 0x0000FF00) >> 8);
117 _data[2] = static_cast<int8_t> ((l1 & 0x00FF0000) >> 16);
118 _data[3] = static_cast<int8_t> ((l1 & 0xFF000000) >> 24);
119 _data[4] = static_cast<int8_t> ((l2 & 0x00FF0000) >> 16);
120 _data[5] = static_cast<int8_t> ((l2 & 0xFF000000) >> 24);
121 _data[6] = static_cast<int8_t> ((l2 & 0x000000FF));
122 _data[7] = static_cast<int8_t> ((l2 & 0x0000FF00) >> 8);
123 _data[8] = static_cast<int8_t> ((l3 & 0xFF000000) >> 24);
124 _data[9] = static_cast<int8_t> ((l3 & 0x00FF0000) >> 16);
125 _data[10] = static_cast<int8_t> ((l3 & 0x0000FF00) >> 8);
126 _data[11] = static_cast<int8_t> ((l3 & 0x000000FF));
127 _data[12] = static_cast<int8_t> ((l4 & 0xFF000000) >> 24);
128 _data[13] = static_cast<int8_t> ((l4 & 0x00FF0000) >> 16);
129 _data[14] = static_cast<int8_t> ((l4 & 0x0000FF00) >> 8);
130 _data[15] = static_cast<int8_t> ((l4 & 0x000000FF));
131 }
132 else
133 {
134 _data[0] = static_cast<int8_t> ((l1 & 0xFF000000) >> 24);
135 _data[1] = static_cast<int8_t> ((l1 & 0x00FF0000) >> 16);
136 _data[2] = static_cast<int8_t> ((l1 & 0x0000FF00) >> 8);
137 _data[3] = static_cast<int8_t> ((l1 & 0x000000FF));
138 _data[4] = static_cast<int8_t> ((l2 & 0xFF000000) >> 24);
139 _data[5] = static_cast<int8_t> ((l2 & 0x00FF0000) >> 16);
140 _data[6] = static_cast<int8_t> ((l2 & 0x0000FF00) >> 8);
141 _data[7] = static_cast<int8_t> ((l2 & 0x000000FF));
142 _data[8] = static_cast<int8_t> ((l3 & 0xFF000000) >> 24);
143 _data[9] = static_cast<int8_t> ((l3 & 0x00FF0000) >> 16);
144 _data[10] = static_cast<int8_t> ((l3 & 0x0000FF00) >> 8);
145 _data[11] = static_cast<int8_t> ((l3 & 0x000000FF));
146 _data[12] = static_cast<int8_t> ((l4 & 0xFF000000) >> 24);
147 _data[13] = static_cast<int8_t> ((l4 & 0x00FF0000) >> 16);
148 _data[14] = static_cast<int8_t> ((l4 & 0x0000FF00) >> 8);
149 _data[15] = static_cast<int8_t> ((l4 & 0x000000FF));
150 }
151}
152
153//------------------------------------------------------------------------
154inline UID::UID (const TUID& uid) noexcept
155{
156 *this = uid;
157}
158
159//------------------------------------------------------------------------
160inline UID::UID (const UID& uid) noexcept
161{
162 *this = uid;
163}
164
165//------------------------------------------------------------------------
166inline UID& UID::operator= (const UID& uid) noexcept
167{
168 *this = uid.data ();
169 return *this;
170}
171
172//------------------------------------------------------------------------
173inline UID& UID::operator= (const TUID& uid) noexcept
174{
175 uint64_t* p1 = reinterpret_cast<uint64_t*> (_data);
176 const uint64_t* p2 = reinterpret_cast<const uint64_t*> (uid);
177 p1[0] = p2[0];
178 p1[1] = p2[1];
179 return *this;
180}
181
182//------------------------------------------------------------------------
183inline constexpr auto UID::data () const noexcept -> const TUID&
184{
185 return _data;
186}
187
188//------------------------------------------------------------------------
189inline constexpr size_t UID::size () const noexcept
190{
191 return sizeof (TUID);
192}
193
194//------------------------------------------------------------------------
195inline std::string UID::toString (bool comFormat) const noexcept
196{
197 std::string result;
198 result.reserve (32);
199 if (comFormat)
200 {
201 const auto& g = reinterpret_cast<const GUID*> (_data);
202
203 char tmp[21] {};
204 snprintf (tmp, 21, "%08X%04X%04X", g->Data1, g->Data2, g->Data3);
205 result = tmp;
206
207 for (uint32_t i = 0; i < 8; ++i)
208 {
209 char s[3] {};
210 snprintf (s, 3, "%02X", g->Data4[i]);
211 result += s;
212 }
213 }
214 else
215 {
216 for (uint32_t i = 0; i < 16; ++i)
217 {
218 char s[3] {};
219 snprintf (s, 3, "%02X", static_cast<uint8_t> (_data[i]));
220 result += s;
221 }
222 }
223 return result;
224}
225
226//------------------------------------------------------------------------
227template<typename StringT>
228inline Optional<UID> UID::fromString (const StringT& str, bool comFormat) noexcept
229{
230 if (str.length () != 32)
231 return {};
232 // TODO: this is a copy from FUID. there are no input validation checks !!!
233 if (comFormat)
234 {
235 TUID uid {};
236 GUID g;
237 char s[33];
238
239 strcpy (s, str.data ());
240 s[8] = 0;
241 sscanf (s, "%x", &g.Data1);
242 strcpy (s, str.data () + 8);
243 s[4] = 0;
244 sscanf (s, "%hx", &g.Data2);
245 strcpy (s, str.data () + 12);
246 s[4] = 0;
247 sscanf (s, "%hx", &g.Data3);
248
249 memcpy (uid, &g, 8);
250
251 for (uint32_t i = 8; i < 16; ++i)
252 {
253 char s2[3] {};
254 s2[0] = str[i * 2];
255 s2[1] = str[i * 2 + 1];
256
257 int32_t d = 0;
258 sscanf (s2, "%2x", &d);
259 uid[i] = static_cast<char> (d);
260 }
261 return {uid};
262 }
263 else
264 {
265 TUID uid {};
266 for (uint32_t i = 0; i < 16; ++i)
267 {
268 char s[3] {};
269 s[0] = str[i * 2];
270 s[1] = str[i * 2 + 1];
271
272 int32_t d = 0;
273 sscanf (s, "%2x", &d);
274 uid[i] = static_cast<char> (d);
275 }
276 return {uid};
277 }
278}
279
280//------------------------------------------------------------------------
281inline UID UID::fromTUID (const TUID _uid) noexcept
282{
283 UID result;
284
285 uint64_t* p1 = reinterpret_cast<uint64_t*> (result._data);
286 const uint64_t* p2 = reinterpret_cast<const uint64_t*> (_uid);
287 p1[0] = p2[0];
288 p1[1] = p2[1];
289
290 return result;
291}
292
293//------------------------------------------------------------------------
294} // VST3
snprintf
sscanf
char TUID[16]
plain UID type
Definition funknown.h:209
memcpy
T reserve(T... args)
typedef uint32_t
strcpy