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.

68 lines
2.1KB

  1. #pragma once
  2. /** Concatenates two literals or two macros
  3. Example:
  4. #define COUNT 42
  5. CONCAT(myVariable, COUNT)
  6. expands to
  7. myVariable42
  8. */
  9. #define CONCAT_LITERAL(x, y) x ## y
  10. #define CONCAT(x, y) CONCAT_LITERAL(x, y)
  11. /** Surrounds raw text with quotes
  12. Example:
  13. #define NAME "world"
  14. printf("Hello " TOSTRING(NAME))
  15. expands to
  16. printf("Hello " "world")
  17. and of course the C++ lexer/parser then concatenates the string literals.
  18. */
  19. #define TOSTRING_LITERAL(x) #x
  20. #define TOSTRING(x) TOSTRING_LITERAL(x)
  21. /** Produces the length of a static array in number of elements */
  22. #define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0]))
  23. /** Reserve space for `count` enums starting with `name`.
  24. Example:
  25. enum Foo {
  26. ENUMS(BAR, 14),
  27. BAZ
  28. };
  29. BAR + 0 to BAR + 13 is reserved. BAZ has a value of 14.
  30. */
  31. #define ENUMS(name, count) name, name ## _LAST = name + (count) - 1
  32. /** Deprecation notice for GCC */
  33. #define DEPRECATED __attribute__ ((deprecated))
  34. /** References binary files compiled into the program.
  35. For example, to include a file "Test.dat" directly into your program binary, add
  36. BINARIES += Test.dat
  37. to your Makefile and declare
  38. BINARY(Test_dat);
  39. at the root of a .c or .cpp source file. Note that special characters are replaced with "_". Then use
  40. BINARY_START(Test_dat)
  41. BINARY_END(Test_dat)
  42. to reference the data beginning and end as a void* array, and
  43. BINARY_SIZE(Test_dat)
  44. to get its size in bytes.
  45. */
  46. #ifdef ARCH_MAC
  47. // Use output from `xxd -i`
  48. #define BINARY(sym) extern unsigned char sym[]; extern unsigned int sym##_len
  49. #define BINARY_START(sym) ((const void*) sym)
  50. #define BINARY_END(sym) ((const void*) sym + sym##_len)
  51. #define BINARY_SIZE(sym) (sym##_len)
  52. #else
  53. #define BINARY(sym) extern char _binary_##sym##_start, _binary_##sym##_end, _binary_##sym##_size
  54. #define BINARY_START(sym) ((const void*) &_binary_##sym##_start)
  55. #define BINARY_END(sym) ((const void*) &_binary_##sym##_end)
  56. // 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.
  57. #define BINARY_SIZE(sym) ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))
  58. #endif