Loading [MathJax]/jax/output/HTML-CSS/config.js
VCV Rack API v2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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// Use synbols generated from `xxd -i`
104#define BINARY(sym) extern "C" {extern const unsigned char sym[]; extern const unsigned int sym##_len;}
105#define BINARY_START(sym) (sym)
106#define BINARY_END(sym) (sym + sym##_len)
107#define BINARY_SIZE(sym) (sym##_len)
108
109
119inline int8_t operator"" _i8(unsigned long long x) {return x;}
120inline int16_t operator"" _i16(unsigned long long x) {return x;}
121inline int32_t operator"" _i32(unsigned long long x) {return x;}
122inline int64_t operator"" _i64(unsigned long long x) {return x;}
123inline uint8_t operator"" _u8(unsigned long long x) {return x;}
124inline uint16_t operator"" _u16(unsigned long long x) {return x;}
125inline uint32_t operator"" _u32(unsigned long long x) {return x;}
126inline uint64_t operator"" _u64(unsigned long long x) {return x;}
127inline float operator"" _f32(long double x) {return x;}
128inline float operator"" _f32(unsigned long long x) {return x;}
129inline double operator"" _f64(long double x) {return x;}
130inline double operator"" _f64(unsigned long long x) {return x;}
131
132
133#if defined ARCH_WIN
134// wchar_t on Windows should be 2 bytes
135static_assert(sizeof(wchar_t) == 2);
136
137// Windows C standard functions are ASCII-8 instead of UTF-8, so redirect these functions to wrappers which convert to UTF-8
138#define fopen fopen_u8
139
140extern "C" {
141FILE* fopen_u8(const char* filename, const char* mode);
142}
143
144namespace std {
145 using ::fopen_u8;
146}
147#endif
148
149
151namespace rack {
152
153
155template <typename To, typename From>
156To bitCast(From from) {
157 static_assert(sizeof(From) == sizeof(To), "Types must be the same size");
158 To to;
159 // This is optimized out
160 std::memcpy(&to, &from, sizeof(From));
161 return to;
162}
163
164
170template <typename T>
172 return new T;
173}
174
175template <typename T, typename F, typename V, typename... Args>
176T* construct(F f, V v, Args... args) {
177 T* o = construct<T>(args...);
178 o->*f = v;
179 return o;
180}
181
182
193template <typename F>
195 F f;
196 DeferWrapper(F f) : f(f) {}
198 f();
199 }
200};
201
202template <typename F>
206
207#define DEFER(code) auto CONCAT(_defer_, __COUNTER__) = rack::deferWrapper([&]() code)
208
209
213struct Exception : std::exception {
214 std::string msg;
215
216 // Attribute index 1 refers to `Exception*` argument so use 2.
217 __attribute__((format(printf, 2, 3)))
218 Exception(const char* format, ...);
219 Exception(const std::string& msg) : msg(msg) {}
220 const char* what() const noexcept override {
221 return msg.c_str();
222 }
223};
224
225
239template <typename C>
240const typename C::mapped_type& get(const C& c, const typename C::key_type& key, const typename C::mapped_type& def = typename C::mapped_type()) {
241 typename C::const_iterator it = c.find(key);
242 if (it == c.end())
243 return def;
244 return it->second;
245}
246
247
250template <typename C>
251const typename C::value_type& get(const C& c, typename C::size_type i, const typename C::value_type& def = typename C::value_type()) {
252 if (i >= c.size())
253 return def;
254 return c[i];
255}
256
257
258// config
259
260extern const std::string APP_NAME;
261extern const std::string APP_EDITION;
262extern const std::string APP_EDITION_NAME;
263extern const std::string APP_VERSION_MAJOR;
264extern const std::string APP_VERSION;
265extern const std::string APP_OS;
266extern const std::string APP_OS_NAME;
267extern const std::string APP_CPU;
268extern const std::string APP_CPU_NAME;
269extern const std::string API_URL;
270
271
272} // namespace rack
273
274
275// Logger depends on common.hpp, but it is handy to include it along with common.hpp.
276#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:171
const std::string APP_NAME
To bitCast(From from)
Casts a primitive, preserving its bits instead of converting.
Definition common.hpp:156
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:203
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:240
Defers running code until the scope is destructed.
Definition common.hpp:194
~DeferWrapper()
Definition common.hpp:197
DeferWrapper(F f)
Definition common.hpp:196
F f
Definition common.hpp:195
__attribute__((format(printf, 2, 3))) Exception(const char *format
Exception(const std::string &msg)
Definition common.hpp:219
std::string msg
Definition common.hpp:214
const char * what() const noexcept override
Definition common.hpp:220