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.

134 lines
3.3KB

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