DISTRHO Plugin Framework
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.

172 lines
4.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_UTILS_HPP_INCLUDED
  17. #define DISTRHO_UTILS_HPP_INCLUDED
  18. #include "src/DistrhoDefines.h"
  19. #include <cstdarg>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #ifdef PROPER_CPP11_SUPPORT
  24. # include <cstdint>
  25. #else
  26. # include <stdint.h>
  27. #endif
  28. #ifdef DISTRHO_OS_WINDOWS
  29. # include <winsock2.h>
  30. # include <windows.h>
  31. #else
  32. # include <unistd.h>
  33. #endif
  34. #if defined(DISTRHO_OS_MAC) && ! defined(CARLA_OS_MAC)
  35. namespace std {
  36. inline float
  37. fmin(float __x, float __y)
  38. { return __builtin_fminf(__x, __y); }
  39. inline float
  40. fmax(float __x, float __y)
  41. { return __builtin_fmaxf(__x, __y); }
  42. inline float
  43. rint(float __x)
  44. { return __builtin_rintf(__x); }
  45. inline float
  46. round(float __x)
  47. { return __builtin_roundf(__x); }
  48. }
  49. #endif
  50. // -----------------------------------------------------------------------
  51. // misc functions
  52. static inline
  53. long d_cconst(const int a, const int b, const int c, const int d) noexcept
  54. {
  55. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  56. }
  57. static inline
  58. void d_pass() noexcept {}
  59. // -----------------------------------------------------------------------
  60. // string print functions
  61. #ifndef DEBUG
  62. # define d_debug(...)
  63. #else
  64. static inline
  65. void d_debug(const char* const fmt, ...) noexcept
  66. {
  67. try {
  68. ::va_list args;
  69. ::va_start(args, fmt);
  70. std::fprintf(stdout, "\x1b[30;1m");
  71. std::vfprintf(stdout, fmt, args);
  72. std::fprintf(stdout, "\x1b[0m\n");
  73. ::va_end(args);
  74. } catch (...) {}
  75. }
  76. #endif
  77. static inline
  78. void d_stdout(const char* const fmt, ...) noexcept
  79. {
  80. try {
  81. ::va_list args;
  82. ::va_start(args, fmt);
  83. std::vfprintf(stdout, fmt, args);
  84. std::fprintf(stdout, "\n");
  85. ::va_end(args);
  86. } catch (...) {}
  87. }
  88. static inline
  89. void d_stderr(const char* const fmt, ...) noexcept
  90. {
  91. try {
  92. ::va_list args;
  93. ::va_start(args, fmt);
  94. std::vfprintf(stderr, fmt, args);
  95. std::fprintf(stderr, "\n");
  96. ::va_end(args);
  97. } catch (...) {}
  98. }
  99. static inline
  100. void d_stderr2(const char* const fmt, ...) noexcept
  101. {
  102. try {
  103. ::va_list args;
  104. ::va_start(args, fmt);
  105. std::fprintf(stderr, "\x1b[31m");
  106. std::vfprintf(stderr, fmt, args);
  107. std::fprintf(stderr, "\x1b[0m\n");
  108. ::va_end(args);
  109. } catch (...) {}
  110. }
  111. static inline
  112. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  113. {
  114. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  115. }
  116. static inline
  117. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  118. {
  119. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  120. }
  121. // -----------------------------------------------------------------------
  122. // d_*sleep
  123. static inline
  124. void d_sleep(const uint secs)
  125. {
  126. DISTRHO_SAFE_ASSERT_RETURN(secs > 0,);
  127. try {
  128. #ifdef DISTRHO_OS_WINDOWS
  129. ::Sleep(secs * 1000);
  130. #else
  131. ::sleep(secs);
  132. #endif
  133. } DISTRHO_SAFE_EXCEPTION("carla_sleep");
  134. }
  135. static inline
  136. void d_msleep(const uint msecs)
  137. {
  138. DISTRHO_SAFE_ASSERT_RETURN(msecs > 0,);
  139. try {
  140. #ifdef DISTRHO_OS_WINDOWS
  141. ::Sleep(msecs);
  142. #else
  143. ::usleep(msecs * 1000);
  144. #endif
  145. } DISTRHO_SAFE_EXCEPTION("carla_msleep");
  146. }
  147. // -----------------------------------------------------------------------
  148. #endif // DISTRHO_UTILS_HPP_INCLUDED