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.

174 lines
4.2KB

  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. // -----------------------------------------------------------------------
  58. // string print functions
  59. #ifndef DEBUG
  60. # define d_debug(...)
  61. #else
  62. static inline
  63. void d_debug(const char* const fmt, ...) noexcept
  64. {
  65. try {
  66. ::va_list args;
  67. ::va_start(args, fmt);
  68. std::fprintf(stdout, "\x1b[30;1m");
  69. std::vfprintf(stdout, fmt, args);
  70. std::fprintf(stdout, "\x1b[0m\n");
  71. ::va_end(args);
  72. } catch (...) {}
  73. }
  74. #endif
  75. static inline
  76. void d_stdout(const char* const fmt, ...) noexcept
  77. {
  78. try {
  79. ::va_list args;
  80. ::va_start(args, fmt);
  81. std::vfprintf(stdout, fmt, args);
  82. std::fprintf(stdout, "\n");
  83. ::va_end(args);
  84. } catch (...) {}
  85. }
  86. static inline
  87. void d_stderr(const char* const fmt, ...) noexcept
  88. {
  89. try {
  90. ::va_list args;
  91. ::va_start(args, fmt);
  92. std::vfprintf(stderr, fmt, args);
  93. std::fprintf(stderr, "\n");
  94. ::va_end(args);
  95. } catch (...) {}
  96. }
  97. static inline
  98. void d_stderr2(const char* const fmt, ...) noexcept
  99. {
  100. try {
  101. ::va_list args;
  102. ::va_start(args, fmt);
  103. std::fprintf(stderr, "\x1b[31m");
  104. std::vfprintf(stderr, fmt, args);
  105. std::fprintf(stderr, "\x1b[0m\n");
  106. ::va_end(args);
  107. } catch (...) {}
  108. }
  109. static inline
  110. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  111. {
  112. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  113. }
  114. static inline
  115. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  116. {
  117. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  118. }
  119. // -----------------------------------------------------------------------
  120. // d_*sleep
  121. static inline
  122. void d_sleep(const uint secs)
  123. {
  124. DISTRHO_SAFE_ASSERT_RETURN(secs > 0,);
  125. try {
  126. #ifdef DISTRHO_OS_WIN
  127. ::Sleep(secs * 1000);
  128. #else
  129. ::sleep(secs);
  130. #endif
  131. } DISTRHO_SAFE_EXCEPTION("carla_sleep");
  132. }
  133. static inline
  134. void d_msleep(const uint msecs)
  135. {
  136. DISTRHO_SAFE_ASSERT_RETURN(msecs > 0,);
  137. try {
  138. #ifdef DISTRHO_OS_WIN
  139. ::Sleep(msecs);
  140. #else
  141. ::usleep(msecs * 1000);
  142. #endif
  143. } DISTRHO_SAFE_EXCEPTION("carla_msleep");
  144. }
  145. // -----------------------------------------------------------------------
  146. // we always need this class
  147. #include "extra/d_string.hpp"
  148. // -----------------------------------------------------------------------
  149. #endif // DISTRHO_UTILS_HPP_INCLUDED