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.

87 lines
1.7KB

  1. #pragma once
  2. // Include most of the C++ standard library for convenience
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <cstdint>
  6. #include <cstring>
  7. #include <cassert>
  8. #include <climits>
  9. #include <string>
  10. #include <vector>
  11. #include <condition_variable>
  12. #include <mutex>
  13. #include "macros.hpp"
  14. #include "math.hpp"
  15. #include "string.hpp"
  16. #include "logger.hpp"
  17. #include "system.hpp"
  18. namespace rack {
  19. ////////////////////
  20. // Template hacks
  21. ////////////////////
  22. /** C#-style property constructor
  23. Example:
  24. Foo *foo = construct<Foo>(&Foo::greeting, "Hello world");
  25. */
  26. template<typename T>
  27. T *construct() {
  28. return new T();
  29. }
  30. template<typename T, typename F, typename V, typename... Args>
  31. T *construct(F f, V v, Args... args) {
  32. T *o = construct<T>(args...);
  33. o->*f = v;
  34. return o;
  35. }
  36. /** Defers code until the scope is destructed
  37. From http://www.gingerbill.org/article/defer-in-cpp.html
  38. Example:
  39. file = fopen(...);
  40. defer({
  41. fclose(file);
  42. });
  43. */
  44. template<typename F>
  45. struct DeferWrapper {
  46. F f;
  47. DeferWrapper(F f) : f(f) {}
  48. ~DeferWrapper() { f(); }
  49. };
  50. template<typename F>
  51. DeferWrapper<F> deferWrapper(F f) {
  52. return DeferWrapper<F>(f);
  53. }
  54. #define defer(code) auto CONCAT(_defer_, __COUNTER__) = deferWrapper([&]() code)
  55. ////////////////////
  56. // Random number generator
  57. // random.cpp
  58. ////////////////////
  59. /** Seeds the RNG with the current time */
  60. void randomInit();
  61. /** Returns a uniform random uint32_t from 0 to UINT32_MAX */
  62. uint32_t randomu32();
  63. uint64_t randomu64();
  64. /** Returns a uniform random float in the interval [0.0, 1.0) */
  65. float randomUniform();
  66. /** Returns a normal random number with mean 0 and standard deviation 1 */
  67. float randomNormal();
  68. DEPRECATED inline float randomf() {return randomUniform();}
  69. } // namespace rack