VCV Rack API v2
Loading...
Searching...
No Matches
common.hpp File Reference
#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cinttypes>
#include <cstdarg>
#include <climits>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <stdexcept>
#include <logger.hpp>
Include dependency graph for common.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  rack::DeferWrapper< F >
 Defers running code until the scope is destructed. More...
 
struct  rack::Exception
 An exception explicitly thrown by Rack or a Rack plugin. More...
 

Namespaces

namespace  rack
 Root namespace for the Rack API.
 

Macros

#define DEPRECATED   __attribute__((deprecated))
 Attribute for deprecated functions and symbols.
 
#define PRIVATE
 Attribute for private functions not intended to be called by plugins.
 
#define CONCAT_LITERAL(x, y)   x ## y
 Concatenates two literals or two macros Example:
 
#define CONCAT(x, y)   CONCAT_LITERAL(x, y)
 
#define TOSTRING_LITERAL(x)   #x
 Surrounds raw text with quotes Example:
 
#define TOSTRING(x)   TOSTRING_LITERAL(x)
 
#define LENGTHOF(arr)   (sizeof(arr) / sizeof((arr)[0]))
 Produces the length of a static array in number of elements.
 
#define ENUMS(name, count)   name, name ## _LAST = name + (count) - 1
 Reserve space for count enums starting with name.
 
#define BINARY(sym)   extern char _binary_##sym##_start, _binary_##sym##_end, _binary_##sym##_size
 References binary files compiled into the program.
 
#define BINARY_START(sym)   ((const void*) &_binary_##sym##_start)
 
#define BINARY_END(sym)   ((const void*) &_binary_##sym##_end)
 
#define BINARY_SIZE(sym)   ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))
 
#define DEFER(code)   auto CONCAT(_defer_, __COUNTER__) = rack::deferWrapper([&]() code)
 

Functions

int8_t operator""_i8 (unsigned long long x)
 Helpful user-defined literals for specifying exact integer and float types.
 
int16_t operator""_i16 (unsigned long long x)
 
int32_t operator""_i32 (unsigned long long x)
 
int64_t operator""_i64 (unsigned long long x)
 
uint8_t operator""_u8 (unsigned long long x)
 
uint16_t operator""_u16 (unsigned long long x)
 
uint32_t operator""_u32 (unsigned long long x)
 
uint64_t operator""_u64 (unsigned long long x)
 
float operator""_f32 (long double x)
 
float operator""_f32 (unsigned long long x)
 
double operator""_f64 (long double x)
 
double operator""_f64 (unsigned long long x)
 
template<typename To , typename From >
To rack::bitCast (From from)
 Casts a primitive, preserving its bits instead of converting.
 
template<typename T >
T * rack::construct ()
 C#-style property constructor Example:
 
template<typename T , typename F , typename V , typename... Args>
T * rack::construct (F f, V v, Args... args)
 
template<typename F >
DeferWrapper< F > rack::deferWrapper (F f)
 
template<typename C >
const C::mapped_type & rack::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.
 
template<typename C >
const C::value_type & rack::get (const C &c, typename C::size_type i, const typename C::value_type &def=typename C::value_type())
 Given a std::vector c, returns the value of the given index i, or returns def if the index is out of bounds.
 

Variables

const std::string rack::APP_NAME
 
const std::string rack::APP_EDITION
 
const std::string rack::APP_EDITION_NAME
 
const std::string rack::APP_VERSION_MAJOR
 
const std::string rack::APP_VERSION
 
const std::string rack::APP_OS
 
const std::string rack::APP_OS_NAME
 
const std::string rack::APP_CPU
 
const std::string rack::APP_CPU_NAME
 
const std::string rack::API_URL
 

Macro Definition Documentation

◆ DEPRECATED

#define DEPRECATED   __attribute__((deprecated))

Attribute for deprecated functions and symbols.

E.g.

DEPRECATED void foo();

◆ PRIVATE

#define PRIVATE

Attribute for private functions not intended to be called by plugins.

When #including rack.hpp, attempting to call PRIVATE functions will result in a compile-time error.

◆ CONCAT_LITERAL

#define CONCAT_LITERAL ( x,
y )   x ## y

Concatenates two literals or two macros Example:

#define COUNT 42
CONCAT(myVariable, COUNT)

expands to

myVariable42

◆ CONCAT

#define CONCAT ( x,
y )   CONCAT_LITERAL(x, y)

◆ TOSTRING_LITERAL

#define TOSTRING_LITERAL ( x)    #x

Surrounds raw text with quotes Example:

#define NAME "world"
printf("Hello " TOSTRING(NAME))

expands to

printf("Hello " "world")

and of course the C++ lexer/parser then concatenates the string literals.

◆ TOSTRING

#define TOSTRING ( x)    TOSTRING_LITERAL(x)

◆ LENGTHOF

#define LENGTHOF ( arr)    (sizeof(arr) / sizeof((arr)[0]))

Produces the length of a static array in number of elements.

◆ ENUMS

#define ENUMS ( name,
count )   name, name ## _LAST = name + (count) - 1

Reserve space for count enums starting with name.

Example:

enum Foo {
    ENUMS(BAR, 14),
    BAZ
};

BAR + 0 to BAR + 13 is reserved. BAZ has a value of 14.

◆ BINARY

#define BINARY ( sym)    extern char _binary_##sym##_start, _binary_##sym##_end, _binary_##sym##_size

References binary files compiled into the program.

For example, to include a file "Test.dat" directly into your program binary, add

BINARIES += Test.dat

to your Makefile and declare

BINARY(Test_dat);

at the root of a .c or .cpp source file. Note that special characters are replaced with "_". Then use

BINARY_START(Test_dat)
BINARY_END(Test_dat)

to reference the data beginning and end as a void* array, and

BINARY_SIZE(Test_dat)

to get its size in bytes.

◆ BINARY_START

#define BINARY_START ( sym)    ((const void*) &_binary_##sym##_start)

◆ BINARY_END

#define BINARY_END ( sym)    ((const void*) &_binary_##sym##_end)

◆ BINARY_SIZE

#define BINARY_SIZE ( sym)    ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))

◆ DEFER

#define DEFER ( code)    auto CONCAT(_defer_, __COUNTER__) = rack::deferWrapper([&]() code)

Function Documentation

◆ operator""_i8()

int8_t operator""_i8 ( unsigned long long x)
inline

Helpful user-defined literals for specifying exact integer and float types.

Usage examples: 42_i8 -4242_u16 0x4a2b_i32 0b11010111000000_u64 42_f32 4.2e-4_f64

◆ operator""_i16()

int16_t operator""_i16 ( unsigned long long x)
inline

◆ operator""_i32()

int32_t operator""_i32 ( unsigned long long x)
inline

◆ operator""_i64()

int64_t operator""_i64 ( unsigned long long x)
inline

◆ operator""_u8()

uint8_t operator""_u8 ( unsigned long long x)
inline

◆ operator""_u16()

uint16_t operator""_u16 ( unsigned long long x)
inline

◆ operator""_u32()

uint32_t operator""_u32 ( unsigned long long x)
inline

◆ operator""_u64()

uint64_t operator""_u64 ( unsigned long long x)
inline

◆ operator""_f32() [1/2]

float operator""_f32 ( long double x)
inline

◆ operator""_f32() [2/2]

float operator""_f32 ( unsigned long long x)
inline

◆ operator""_f64() [1/2]

double operator""_f64 ( long double x)
inline

◆ operator""_f64() [2/2]

double operator""_f64 ( unsigned long long x)
inline