VCV Rack API v2
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1#pragma once
2// Include most of the C stdlib for convenience
3#include <cstddef>
4#include <cstdlib>
5#include <cstdio>
6#include <cstdint>
7#include <cinttypes>
8#include <cstdarg>
9#include <climits>
10#include <cmath>
11#include <cstring>
12#include <cassert>
13
14// Include some of the C++ stdlib for convenience
15#include <string>
16#include <stdexcept>
17
18#include <arch.hpp>
19
20
26#define DEPRECATED __attribute__((deprecated))
27
31#ifndef PRIVATE
32#define PRIVATE
33#endif
34
35
46#define CONCAT_LITERAL(x, y) x ## y
47#define CONCAT(x, y) CONCAT_LITERAL(x, y)
48
49
62#define TOSTRING_LITERAL(x) #x
63#define TOSTRING(x) TOSTRING_LITERAL(x)
64
65
67#define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0]))
68
69
80#define ENUMS(name, count) name, name ## _LAST = name + (count) - 1
81
82
103#if defined ARCH_MAC
104 // Use output from `xxd -i`
105 #define BINARY(sym) extern unsigned char sym[]; extern unsigned int sym##_len
106 #define BINARY_START(sym) ((const void*) sym)
107 #define BINARY_END(sym) ((const void*) sym + sym##_len)
108 #define BINARY_SIZE(sym) (sym##_len)
109#else
110 #define BINARY(sym) extern char _binary_##sym##_start, _binary_##sym##_end, _binary_##sym##_size
111 #define BINARY_START(sym) ((const void*) &_binary_##sym##_start)
112 #define BINARY_END(sym) ((const void*) &_binary_##sym##_end)
113 // The symbol "_binary_##sym##_size" doesn't seem to be valid after a plugin is dynamically loaded, so simply take the difference between the two addresses.
114 #define BINARY_SIZE(sym) ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))
115#endif
116
117
127inline int8_t operator"" _i8(unsigned long long x) {return x;}
128inline int16_t operator"" _i16(unsigned long long x) {return x;}
129inline int32_t operator"" _i32(unsigned long long x) {return x;}
130inline int64_t operator"" _i64(unsigned long long x) {return x;}
131inline uint8_t operator"" _u8(unsigned long long x) {return x;}
132inline uint16_t operator"" _u16(unsigned long long x) {return x;}
133inline uint32_t operator"" _u32(unsigned long long x) {return x;}
134inline uint64_t operator"" _u64(unsigned long long x) {return x;}
135inline float operator"" _f32(long double x) {return x;}
136inline float operator"" _f32(unsigned long long x) {return x;}
137inline double operator"" _f64(long double x) {return x;}
138inline double operator"" _f64(unsigned long long x) {return x;}
139
140
141#if defined ARCH_WIN
142// wchar_t on Windows should be 2 bytes
143static_assert(sizeof(wchar_t) == 2);
144
145// Windows C standard functions are ASCII-8 instead of UTF-8, so redirect these functions to wrappers which convert to UTF-8
146#define fopen fopen_u8
147
148extern "C" {
149FILE* fopen_u8(const char* filename, const char* mode);
150}
151
152namespace std {
153 using ::fopen_u8;
154}
155#endif
156
157
159namespace rack {
160
161
163template <typename To, typename From>
164To bitCast(From from) {
165 static_assert(sizeof(From) == sizeof(To), "Types must be the same size");
166 To to;
167 // This is optimized out
168 std::memcpy(&to, &from, sizeof(From));
169 return to;
170}
171
172
178template <typename T>
180 return new T;
181}
182
183template <typename T, typename F, typename V, typename... Args>
184T* construct(F f, V v, Args... args) {
185 T* o = construct<T>(args...);
186 o->*f = v;
187 return o;
188}
189
190
201template <typename F>
203 F f;
204 DeferWrapper(F f) : f(f) {}
206 f();
207 }
208};
209
210template <typename F>
214
215#define DEFER(code) auto CONCAT(_defer_, __COUNTER__) = rack::deferWrapper([&]() code)
216
217
221struct Exception : std::exception {
222 std::string msg;
223
224 // Attribute index 1 refers to `Exception*` argument so use 2.
225 __attribute__((format(printf, 2, 3)))
226 Exception(const char* format, ...);
227 Exception(const std::string& msg) : msg(msg) {}
228 const char* what() const noexcept override {
229 return msg.c_str();
230 }
231};
232
233
247template <typename C>
248const typename C::mapped_type& get(const C& c, const typename C::key_type& key, const typename C::mapped_type& def = typename C::mapped_type()) {
249 typename C::const_iterator it = c.find(key);
250 if (it == c.end())
251 return def;
252 return it->second;
253}
254
255
258template <typename C>
259const typename C::value_type& get(const C& c, typename C::size_type i, const typename C::value_type& def = typename C::value_type()) {
260 if (i >= c.size())
261 return def;
262 return c[i];
263}
264
265
266// config
267
268extern const std::string APP_NAME;
269extern const std::string APP_EDITION;
270extern const std::string APP_EDITION_NAME;
271extern const std::string APP_VERSION_MAJOR;
272extern const std::string APP_VERSION;
273extern const std::string APP_OS;
274extern const std::string APP_OS_NAME;
275extern const std::string APP_CPU;
276extern const std::string APP_CPU_NAME;
277extern const std::string API_URL;
278
279
280} // namespace rack
281
282
283// Logger depends on common.hpp, but it is handy to include it along with common.hpp.
284#include <logger.hpp>
const char int const char const char * format
Definition logger.hpp:41
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
T * construct()
C#-style property constructor Example:
Definition common.hpp:179
const std::string APP_NAME
To bitCast(From from)
Casts a primitive, preserving its bits instead of converting.
Definition common.hpp:164
const std::string APP_OS_NAME
const std::string API_URL
const std::string APP_VERSION_MAJOR
const std::string APP_CPU
const std::string APP_VERSION
const std::string APP_EDITION
const std::string APP_OS
const std::string APP_EDITION_NAME
DeferWrapper< F > deferWrapper(F f)
Definition common.hpp:211
const std::string APP_CPU_NAME
const C::mapped_type & get(const C &c, const typename C::key_type &key, const typename C::mapped_type &def=typename C::mapped_type())
Given a std::map c, returns the value of the given key, or returns def if the key doesn't exist.
Definition common.hpp:248
Defers running code until the scope is destructed.
Definition common.hpp:202
~DeferWrapper()
Definition common.hpp:205
DeferWrapper(F f)
Definition common.hpp:204
F f
Definition common.hpp:203
An exception explicitly thrown by Rack or a Rack plugin.
Definition common.hpp:221
__attribute__((format(printf, 2, 3))) Exception(const char *format
std::string msg
Definition common.hpp:222
const char * what() const noexcept override
Definition common.hpp:228