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.

common.hpp 3.7KB

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