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.

130 lines
3.5KB

  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 DISTRHO_PROPER_CPP11_SUPPORT
  24. # include <cstdint>
  25. #else
  26. # include <stdint.h>
  27. #endif
  28. #if defined(DISTRHO_OS_MAC) && ! defined(CARLA_OS_MAC)
  29. namespace std {
  30. inline float fmin(float __x, float __y)
  31. { return __builtin_fminf(__x, __y); }
  32. inline float fmax(float __x, float __y)
  33. { return __builtin_fmaxf(__x, __y); }
  34. inline float rint(float __x)
  35. { return __builtin_rintf(__x); }
  36. inline float round(float __x)
  37. { return __builtin_roundf(__x); }
  38. }
  39. #endif
  40. // -----------------------------------------------------------------------
  41. // misc functions
  42. static inline
  43. int64_t d_cconst(const int a, const int b, const int c, const int d) noexcept
  44. {
  45. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  46. }
  47. static inline
  48. void d_pass() noexcept {}
  49. // -----------------------------------------------------------------------
  50. // string print functions
  51. #ifndef DEBUG
  52. # define d_debug(...)
  53. #else
  54. static inline
  55. void d_debug(const char* const fmt, ...) noexcept
  56. {
  57. try {
  58. ::va_list args;
  59. ::va_start(args, fmt);
  60. std::fprintf(stdout, "\x1b[30;1m");
  61. std::vfprintf(stdout, fmt, args);
  62. std::fprintf(stdout, "\x1b[0m\n");
  63. ::va_end(args);
  64. } catch (...) {}
  65. }
  66. #endif
  67. static inline
  68. void d_stdout(const char* const fmt, ...) noexcept
  69. {
  70. try {
  71. ::va_list args;
  72. ::va_start(args, fmt);
  73. std::vfprintf(stdout, fmt, args);
  74. std::fprintf(stdout, "\n");
  75. ::va_end(args);
  76. } catch (...) {}
  77. }
  78. static inline
  79. void d_stderr(const char* const fmt, ...) noexcept
  80. {
  81. try {
  82. ::va_list args;
  83. ::va_start(args, fmt);
  84. std::vfprintf(stderr, fmt, args);
  85. std::fprintf(stderr, "\n");
  86. ::va_end(args);
  87. } catch (...) {}
  88. }
  89. static inline
  90. void d_stderr2(const char* const fmt, ...) noexcept
  91. {
  92. try {
  93. ::va_list args;
  94. ::va_start(args, fmt);
  95. std::fprintf(stderr, "\x1b[31m");
  96. std::vfprintf(stderr, fmt, args);
  97. std::fprintf(stderr, "\x1b[0m\n");
  98. ::va_end(args);
  99. } catch (...) {}
  100. }
  101. static inline
  102. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  103. {
  104. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  105. }
  106. static inline
  107. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  108. {
  109. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  110. }
  111. // -----------------------------------------------------------------------
  112. #endif // DISTRHO_UTILS_HPP_INCLUDED