You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
6.2KB

  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. // Include some of the C++ stdlib for convenience
  14. #include <string>
  15. #include <stdexcept>
  16. #include <logger.hpp>
  17. /** Attribute for deprecated functions and symbols.
  18. E.g.
  19. DEPRECATED void foo();
  20. */
  21. #if defined(__GNUC__) || defined(__clang__)
  22. #define DEPRECATED __attribute__((deprecated))
  23. #elif defined(_MSC_VER)
  24. #define DEPRECATED __declspec(deprecated)
  25. #endif
  26. /** Attribute for private functions and symbols not intended to be used by plugins.
  27. By default this does nothing, but when #including rack.hpp, it prints a compile-time warning.
  28. */
  29. #define INTERNAL __attribute__((visibility("hidden")))
  30. /** Concatenates two literals or two macros
  31. Example:
  32. #define COUNT 42
  33. CONCAT(myVariable, COUNT)
  34. expands to
  35. myVariable42
  36. */
  37. #define CONCAT_LITERAL(x, y) x ## y
  38. #define CONCAT(x, y) CONCAT_LITERAL(x, y)
  39. /** Surrounds raw text with quotes
  40. Example:
  41. #define NAME "world"
  42. printf("Hello " TOSTRING(NAME))
  43. expands to
  44. printf("Hello " "world")
  45. and of course the C++ lexer/parser then concatenates the string literals.
  46. */
  47. #define TOSTRING_LITERAL(x) #x
  48. #define TOSTRING(x) TOSTRING_LITERAL(x)
  49. /** Produces the length of a static array in number of elements */
  50. #define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0]))
  51. /** Reserve space for `count` enums starting with `name`.
  52. Example:
  53. enum Foo {
  54. ENUMS(BAR, 14),
  55. BAZ
  56. };
  57. `BAR + 0` to `BAR + 13` is reserved. `BAZ` has a value of 14.
  58. */
  59. #define ENUMS(name, count) name, name ## _LAST = name + (count) - 1
  60. /** References binary files compiled into the program.
  61. For example, to include a file "Test.dat" directly into your program binary, add
  62. BINARIES += Test.dat
  63. to your Makefile and declare
  64. BINARY(Test_dat);
  65. at the root of a .c or .cpp source file. Note that special characters are replaced with "_". Then use
  66. BINARY_START(Test_dat)
  67. BINARY_END(Test_dat)
  68. to reference the data beginning and end as a void* array, and
  69. BINARY_SIZE(Test_dat)
  70. to get its size in bytes.
  71. */
  72. #if defined ARCH_MAC
  73. // Use output from `xxd -i`
  74. #define BINARY(sym) extern unsigned char sym[]; extern unsigned int sym##_len
  75. #define BINARY_START(sym) ((const void*) sym)
  76. #define BINARY_END(sym) ((const void*) sym + sym##_len)
  77. #define BINARY_SIZE(sym) (sym##_len)
  78. #else
  79. #define BINARY(sym) extern char _binary_##sym##_start, _binary_##sym##_end, _binary_##sym##_size
  80. #define BINARY_START(sym) ((const void*) &_binary_##sym##_start)
  81. #define BINARY_END(sym) ((const void*) &_binary_##sym##_end)
  82. // 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.
  83. #define BINARY_SIZE(sym) ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))
  84. #endif
  85. /** Helpful user-defined literals for specifying exact integer and float types.
  86. Usage examples:
  87. 123_i8
  88. -1234_u16
  89. 0x1000_i32
  90. 0b10000000_u64
  91. 12.3_f32
  92. 1e4_f64
  93. */
  94. inline int8_t operator"" _i8(unsigned long long x) {return x;}
  95. inline int16_t operator"" _i16(unsigned long long x) {return x;}
  96. inline int32_t operator"" _i32(unsigned long long x) {return x;}
  97. inline int64_t operator"" _i64(unsigned long long x) {return x;}
  98. inline uint8_t operator"" _u8(unsigned long long x) {return x;}
  99. inline uint16_t operator"" _u16(unsigned long long x) {return x;}
  100. inline uint32_t operator"" _u32(unsigned long long x) {return x;}
  101. inline uint64_t operator"" _u64(unsigned long long x) {return x;}
  102. inline float operator"" _f32(long double x) {return x;}
  103. inline double operator"" _f64(long double x) {return x;}
  104. #if defined ARCH_WIN
  105. // wchar_t on Windows should be 2 bytes
  106. static_assert(sizeof(wchar_t) == 2);
  107. // Windows C standard functions are ASCII-8 instead of UTF-8, so redirect these functions to wrappers which convert to UTF-8
  108. #define fopen fopen_u8
  109. extern "C" {
  110. FILE* fopen_u8(const char* filename, const char* mode);
  111. }
  112. namespace std {
  113. using ::fopen_u8;
  114. }
  115. #endif
  116. namespace rack {
  117. /** C#-style property constructor
  118. Example:
  119. Foo *foo = construct<Foo>(&Foo::greeting, "Hello world", &Foo::legs, 2);
  120. */
  121. template <typename T>
  122. T* construct() {
  123. return new T;
  124. }
  125. template <typename T, typename F, typename V, typename... Args>
  126. T* construct(F f, V v, Args... args) {
  127. T* o = construct<T>(args...);
  128. o->*f = v;
  129. return o;
  130. }
  131. /** Defers code until the scope is destructed
  132. From http://www.gingerbill.org/article/defer-in-cpp.html
  133. Example:
  134. file = fopen(...);
  135. DEFER({
  136. fclose(file);
  137. });
  138. */
  139. template <typename F>
  140. struct DeferWrapper {
  141. F f;
  142. DeferWrapper(F f) : f(f) {}
  143. ~DeferWrapper() {
  144. f();
  145. }
  146. };
  147. template <typename F>
  148. DeferWrapper<F> deferWrapper(F f) {
  149. return DeferWrapper<F>(f);
  150. }
  151. #define DEFER(code) auto CONCAT(_defer_, __COUNTER__) = rack::deferWrapper([&]() code)
  152. /** An exception explicitly thrown by Rack or a Rack plugin.
  153. Can be subclassed to throw/catch specific custom exceptions.
  154. */
  155. struct Exception : std::exception {
  156. std::string msg;
  157. // Attribute index 1 refers to `Exception*` argument so use 2.
  158. __attribute__((format(printf, 2, 3)))
  159. Exception(const char* format, ...);
  160. Exception(const std::string& msg) : msg(msg) {}
  161. const char* what() const noexcept override {
  162. return msg.c_str();
  163. }
  164. };
  165. /** Given a std::map, returns the value of the given key, or returns `def` if the key doesn't exist.
  166. Does *not* add the default value to the map.
  167. Posted to https://stackoverflow.com/a/63683271/272642.
  168. Example:
  169. std::map<std::string, int> m;
  170. int v = get(m, "a", 3);
  171. // v is 3 because the key "a" does not exist
  172. int w = get(m, "a");
  173. // w is 0 because no default value is given, so it assumes the default int.
  174. */
  175. template <typename C>
  176. typename C::mapped_type get(const C& m, const typename C::key_type& key, const typename C::mapped_type& def = typename C::mapped_type()) {
  177. typename C::const_iterator it = m.find(key);
  178. if (it == m.end())
  179. return def;
  180. return it->second;
  181. }
  182. // config
  183. extern const std::string APP_NAME;
  184. extern const std::string APP_VERSION;
  185. extern const std::string APP_ARCH;
  186. extern const std::string ABI_VERSION;
  187. extern const std::string API_URL;
  188. extern const std::string API_VERSION;
  189. } // namespace rack