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.

126 lines
3.2KB

  1. #pragma once
  2. #include "logger.hpp"
  3. // Include most of the C stdlib for convenience
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cstdint>
  7. #include <cstdarg>
  8. #include <climits>
  9. #include <cmath>
  10. #include <cstring>
  11. #include <cassert>
  12. // Include some of the C++ stdlib for convenience
  13. #include <string>
  14. /** Deprecation notice for GCC */
  15. #define DEPRECATED __attribute__ ((deprecated))
  16. /** Concatenates two literals or two macros
  17. Example:
  18. #define COUNT 42
  19. CONCAT(myVariable, COUNT)
  20. expands to
  21. myVariable42
  22. */
  23. #define CONCAT_LITERAL(x, y) x ## y
  24. #define CONCAT(x, y) CONCAT_LITERAL(x, y)
  25. /** Surrounds raw text with quotes
  26. Example:
  27. #define NAME "world"
  28. printf("Hello " TOSTRING(NAME))
  29. expands to
  30. printf("Hello " "world")
  31. and of course the C++ lexer/parser then concatenates the string literals.
  32. */
  33. #define TOSTRING_LITERAL(x) #x
  34. #define TOSTRING(x) TOSTRING_LITERAL(x)
  35. /** Produces the length of a static array in number of elements */
  36. #define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0]))
  37. /** Reserve space for `count` enums starting with `name`.
  38. Example:
  39. enum Foo {
  40. ENUMS(BAR, 14),
  41. BAZ
  42. };
  43. BAR + 0 to BAR + 13 is reserved. BAZ has a value of 14.
  44. */
  45. #define ENUMS(name, count) name, name ## _LAST = name + (count) - 1
  46. /** References binary files compiled into the program.
  47. For example, to include a file "Test.dat" directly into your program binary, add
  48. BINARIES += Test.dat
  49. to your Makefile and declare
  50. BINARY(Test_dat);
  51. at the root of a .c or .cpp source file. Note that special characters are replaced with "_". Then use
  52. BINARY_START(Test_dat)
  53. BINARY_END(Test_dat)
  54. to reference the data beginning and end as a void* array, and
  55. BINARY_SIZE(Test_dat)
  56. to get its size in bytes.
  57. */
  58. #ifdef ARCH_MAC
  59. // Use output from `xxd -i`
  60. #define BINARY(sym) extern unsigned char sym[]; extern unsigned int sym##_len
  61. #define BINARY_START(sym) ((const void*) sym)
  62. #define BINARY_END(sym) ((const void*) sym + sym##_len)
  63. #define BINARY_SIZE(sym) (sym##_len)
  64. #else
  65. #define BINARY(sym) extern char _binary_##sym##_start, _binary_##sym##_end, _binary_##sym##_size
  66. #define BINARY_START(sym) ((const void*) &_binary_##sym##_start)
  67. #define BINARY_END(sym) ((const void*) &_binary_##sym##_end)
  68. // 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.
  69. #define BINARY_SIZE(sym) ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))
  70. #endif
  71. /** C#-style property constructor
  72. Example:
  73. Foo *foo = construct<Foo>(&Foo::greeting, "Hello world", &Foo::legs, 2);
  74. */
  75. template<typename T>
  76. T *construct() {
  77. return new T;
  78. }
  79. template<typename T, typename F, typename V, typename... Args>
  80. T *construct(F f, V v, Args... args) {
  81. T *o = construct<T>(args...);
  82. o->*f = v;
  83. return o;
  84. }
  85. /** Defers code until the scope is destructed
  86. From http://www.gingerbill.org/article/defer-in-cpp.html
  87. Example:
  88. file = fopen(...);
  89. DEFER({
  90. fclose(file);
  91. });
  92. */
  93. template<typename F>
  94. struct DeferWrapper {
  95. F f;
  96. DeferWrapper(F f) : f(f) {}
  97. ~DeferWrapper() { f(); }
  98. };
  99. template<typename F>
  100. DeferWrapper<F> deferWrapper(F f) {
  101. return DeferWrapper<F>(f);
  102. }
  103. #define DEFER(code) auto CONCAT(_defer_, __COUNTER__) = deferWrapper([&]() code)