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.

142 lines
3.2KB

  1. #pragma once
  2. // Include most of the C standard library for convenience
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <assert.h>
  7. #include <string>
  8. #include <condition_variable>
  9. #include <mutex>
  10. /** Surrounds raw text with quotes
  11. Example:
  12. printf("Hello " STRINGIFY(world))
  13. will expand to
  14. printf("Hello " "world")
  15. and of course the C++ lexer/parser will then concatenate the string literals
  16. */
  17. #define STRINGIFY(x) #x
  18. /** Converts a macro to a string literal
  19. Example:
  20. #define NAME "world"
  21. printf("Hello " TOSTRING(NAME))
  22. will expand to
  23. printf("Hello " "world")
  24. */
  25. #define TOSTRING(x) STRINGIFY(x)
  26. #define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0]))
  27. /** Deprecation notice for GCC */
  28. #define DEPRECATED __attribute__ ((deprecated))
  29. namespace rack {
  30. /** C#-style property constructor
  31. Example:
  32. Foo *foo = construct<Foo>(&Foo::greeting, "Hello world");
  33. */
  34. template<typename T>
  35. T *construct() {
  36. return new T();
  37. }
  38. template<typename T, typename F, typename V, typename... Args>
  39. T *construct(F f, V v, Args... args) {
  40. T *o = construct<T>(args...);
  41. o->*f = v;
  42. return o;
  43. }
  44. ////////////////////
  45. // RNG
  46. ////////////////////
  47. /** Seeds the RNG with the current time */
  48. void randomSeedTime();
  49. uint32_t randomu32();
  50. uint64_t randomu64();
  51. /** Returns a uniform random float in the interval [0.0, 1.0) */
  52. float randomf();
  53. /** Returns a normal random number with mean 0 and std dev 1 */
  54. float randomNormal();
  55. ////////////////////
  56. // String functions
  57. ////////////////////
  58. /** Converts a printf format string and optional arguments into a std::string */
  59. std::string stringf(const char *format, ...);
  60. std::string tolower(std::string s);
  61. std::string toupper(std::string s);
  62. /** Truncates and adds "..." to a string, not exceeding `len` characters */
  63. std::string ellipsize(std::string s, size_t len);
  64. std::string extractDirectory(std::string path);
  65. std::string extractFilename(std::string path);
  66. std::string extractExtension(std::string path);
  67. ////////////////////
  68. // Operating system functions
  69. ////////////////////
  70. /** Opens a URL, also happens to work with PDFs and folders.
  71. Shell injection is possible, so make sure the URL is trusted or hard coded.
  72. May block, so open in a new thread.
  73. */
  74. void openBrowser(std::string url);
  75. ////////////////////
  76. // Thread functions
  77. ////////////////////
  78. /** Threads which obtain a VIPLock will cause wait() to block for other less important threads.
  79. This does not provide the VIPs with an exclusive lock. That should be left up to another mutex shared between the less important thread.
  80. */
  81. struct VIPMutex {
  82. int count = 0;
  83. std::condition_variable cv;
  84. std::mutex countMutex;
  85. /** Blocks until there are no remaining VIPLocks */
  86. void wait() {
  87. std::unique_lock<std::mutex> lock(countMutex);
  88. while (count > 0)
  89. cv.wait(lock);
  90. }
  91. };
  92. struct VIPLock {
  93. VIPMutex &m;
  94. VIPLock(VIPMutex &m) : m(m) {
  95. std::unique_lock<std::mutex> lock(m.countMutex);
  96. m.count++;
  97. }
  98. ~VIPLock() {
  99. std::unique_lock<std::mutex> lock(m.countMutex);
  100. m.count--;
  101. lock.unlock();
  102. m.cv.notify_all();
  103. }
  104. };
  105. ////////////////////
  106. // logger
  107. ////////////////////
  108. extern FILE *gLogFile;
  109. void debug(const char *format, ...);
  110. void info(const char *format, ...);
  111. void warn(const char *format, ...);
  112. void fatal(const char *format, ...);
  113. } // namespace rack