DISTRHO Plugin Framework
DistrhoUtils.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2021 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 
17 #ifndef DISTRHO_UTILS_HPP_INCLUDED
18 #define DISTRHO_UTILS_HPP_INCLUDED
19 
20 #include "src/DistrhoDefines.h"
21 
22 #include <cstdarg>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 
27 #include <cmath>
28 #include <limits>
29 
30 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
31 # include <cstdint>
32 #else
33 # include <stdint.h>
34 #endif
35 
36 #if defined(DISTRHO_OS_WINDOWS) && defined(_MSC_VER)
37 #include <basetsd.h>
38 typedef SSIZE_T ssize_t;
39 #endif
40 
41 #if ! defined(CARLA_MATH_UTILS_HPP_INCLUDED) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
42 namespace std {
43 inline float fmin(float __x, float __y)
44  { return __builtin_fminf(__x, __y); }
45 inline float fmax(float __x, float __y)
46  { return __builtin_fmaxf(__x, __y); }
47 inline float rint(float __x)
48  { return __builtin_rintf(__x); }
49 inline float round(float __x)
50  { return __builtin_roundf(__x); }
51 }
52 #endif
53 
54 #ifndef M_PI
55 # define M_PI 3.14159265358979323846
56 #endif
57 
58 #define DISTRHO_MACRO_AS_STRING_VALUE(MACRO) #MACRO
59 #define DISTRHO_MACRO_AS_STRING(MACRO) DISTRHO_MACRO_AS_STRING_VALUE(MACRO)
60 
61 /* ------------------------------------------------------------------------------------------------------------
62  * misc functions */
63 
64 /**
65  @defgroup MiscellaneousFunctions Miscellaneous functions
66 
67  @{
68  */
69 
70 /**
71  Return a 32-bit number from 4 8-bit numbers.@n
72  The return type is a int64_t for better compatibility with plugin formats that use such numbers.
73  */
74 static inline constexpr
75 int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
76 {
77  return (a << 24) | (b << 16) | (c << 8) | (d << 0);
78 }
79 
80 /**
81  Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
82  */
83 static inline constexpr
84 uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
85 {
86  return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
87 }
88 
89 /**
90  Dummy, no-op function.
91  */
92 static inline
93 void d_pass() noexcept {}
94 
95 /** @} */
96 
97 /* ------------------------------------------------------------------------------------------------------------
98  * string print functions */
99 
100 /**
101  @defgroup StringPrintFunctions String print functions
102 
103  @{
104  */
105 
106 /**
107  Print a string to stdout with newline (gray color).
108  Does nothing if DEBUG is not defined.
109  */
110 #ifndef DEBUG
111 # define d_debug(...)
112 #else
113 static inline
114 void d_debug(const char* const fmt, ...) noexcept
115 {
116  try {
117  va_list args;
118  va_start(args, fmt);
119  std::fprintf(stdout, "\x1b[30;1m");
120  std::vfprintf(stdout, fmt, args);
121  std::fprintf(stdout, "\x1b[0m\n");
122  va_end(args);
123  } catch (...) {}
124 }
125 #endif
126 
127 /**
128  Print a string to stdout with newline.
129  */
130 static inline
131 void d_stdout(const char* const fmt, ...) noexcept
132 {
133  try {
134  va_list args;
135  va_start(args, fmt);
136  std::vfprintf(stdout, fmt, args);
137  std::fprintf(stdout, "\n");
138  va_end(args);
139  } catch (...) {}
140 }
141 
142 /**
143  Print a string to stderr with newline.
144  */
145 static inline
146 void d_stderr(const char* const fmt, ...) noexcept
147 {
148  try {
149  va_list args;
150  va_start(args, fmt);
151  std::vfprintf(stderr, fmt, args);
152  std::fprintf(stderr, "\n");
153  va_end(args);
154  } catch (...) {}
155 }
156 
157 /**
158  Print a string to stderr with newline (red color).
159  */
160 static inline
161 void d_stderr2(const char* const fmt, ...) noexcept
162 {
163  try {
164  va_list args;
165  va_start(args, fmt);
166  std::fprintf(stderr, "\x1b[31m");
167  std::vfprintf(stderr, fmt, args);
168  std::fprintf(stderr, "\x1b[0m\n");
169  va_end(args);
170  } catch (...) {}
171 }
172 
173 /**
174  Print a safe assertion error message.
175  */
176 static inline
177 void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
178 {
179  d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
180 }
181 
182 /**
183  Print a safe assertion error message, with 1 extra signed integer value.
184  */
185 static inline
186 void d_safe_assert_int(const char* const assertion, const char* const file,
187  const int line, const int value) noexcept
188 {
189  d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
190 }
191 
192 /**
193  Print a safe assertion error message, with 1 extra unsigned integer value.
194  */
195 static inline
196 void d_safe_assert_uint(const char* const assertion, const char* const file,
197  const int line, const uint value) noexcept
198 {
199  d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
200 }
201 
202 /**
203  Print a safe assertion error message, with 2 extra signed integer values.
204  */
205 static inline
206 void d_safe_assert_int2(const char* const assertion, const char* const file,
207  const int line, const int v1, const int v2) noexcept
208 {
209  d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
210 }
211 
212 /**
213  Print a safe assertion error message, with 2 extra unsigned integer values.
214  */
215 static inline
216 void d_safe_assert_uint2(const char* const assertion, const char* const file,
217  const int line, const uint v1, const uint v2) noexcept
218 {
219  d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
220 }
221 
222 /**
223  Print a safe assertion error message, with a custom error message.
224  */
225 static inline
226 void d_custom_safe_assert(const char* const message, const char* const assertion, const char* const file,
227  const int line) noexcept
228 {
229  d_stderr2("assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
230 }
231 
232 /**
233  Print a safe exception error message.
234  */
235 static inline
236 void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
237 {
238  d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
239 }
240 
241 /** @} */
242 
243 /* ------------------------------------------------------------------------------------------------------------
244  * math functions */
245 
246 /**
247  @defgroup MathFunctions Math related functions
248 
249  @{
250  */
251 
252 /**
253  Safely compare two floating point numbers.
254  Returns true if they match.
255  */
256 template<typename T>
257 static inline
258 bool d_isEqual(const T& v1, const T& v2)
259 {
260  return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
261 }
262 
263 /**
264  Safely compare two floating point numbers.
265  Returns true if they don't match.
266  */
267 template<typename T>
268 static inline
269 bool d_isNotEqual(const T& v1, const T& v2)
270 {
271  return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
272 }
273 
274 /**
275  Safely check if a floating point number is zero.
276  */
277 template<typename T>
278 static inline
279 bool d_isZero(const T& value)
280 {
281  return std::abs(value) < std::numeric_limits<T>::epsilon();
282 }
283 
284 /**
285  Safely check if a floating point number is not zero.
286  */
287 template<typename T>
288 static inline
289 bool d_isNotZero(const T& value)
290 {
291  return std::abs(value) >= std::numeric_limits<T>::epsilon();
292 }
293 
294 /**
295  Get next power of 2.
296  */
297 static inline
298 uint32_t d_nextPowerOf2(uint32_t size) noexcept
299 {
300  DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
301 
302  // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
303  --size;
304  size |= size >> 1;
305  size |= size >> 2;
306  size |= size >> 4;
307  size |= size >> 8;
308  size |= size >> 16;
309  return ++size;
310 }
311 
312 /** @} */
313 
314 // -----------------------------------------------------------------------
315 
316 #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
317  // If your code uses a lot of DISTRHO classes, then this will obviously save you
318  // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
319  namespace DISTRHO_NAMESPACE {}
320  using namespace DISTRHO_NAMESPACE;
321 #endif
322 
323 // -----------------------------------------------------------------------
324 
325 #endif // DISTRHO_UTILS_HPP_INCLUDED
static bool d_isNotZero(const T &value)
Definition: DistrhoUtils.hpp:289
static bool d_isEqual(const T &v1, const T &v2)
Definition: DistrhoUtils.hpp:258
static uint32_t d_nextPowerOf2(uint32_t size) noexcept
Definition: DistrhoUtils.hpp:298
static bool d_isZero(const T &value)
Definition: DistrhoUtils.hpp:279
static bool d_isNotEqual(const T &v1, const T &v2)
Definition: DistrhoUtils.hpp:269
static void d_pass() noexcept
Definition: DistrhoUtils.hpp:93
static constexpr int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
Definition: DistrhoUtils.hpp:75
static constexpr uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
Definition: DistrhoUtils.hpp:84
#define DISTRHO_NAMESPACE
Definition: DistrhoInfo.hpp:907
static void d_stderr(const char *const fmt,...) noexcept
Definition: DistrhoUtils.hpp:146
static void d_custom_safe_assert(const char *const message, const char *const assertion, const char *const file, const int line) noexcept
Definition: DistrhoUtils.hpp:226
static void d_stdout(const char *const fmt,...) noexcept
Definition: DistrhoUtils.hpp:131
static void d_safe_assert(const char *const assertion, const char *const file, const int line) noexcept
Definition: DistrhoUtils.hpp:177
static void d_safe_exception(const char *const exception, const char *const file, const int line) noexcept
Definition: DistrhoUtils.hpp:236
static void d_stderr2(const char *const fmt,...) noexcept
Definition: DistrhoUtils.hpp:161
static void d_safe_assert_int2(const char *const assertion, const char *const file, const int line, const int v1, const int v2) noexcept
Definition: DistrhoUtils.hpp:206
static void d_debug(const char *const fmt,...) noexcept
Definition: DistrhoUtils.hpp:114
static void d_safe_assert_int(const char *const assertion, const char *const file, const int line, const int value) noexcept
Definition: DistrhoUtils.hpp:186
static void d_safe_assert_uint2(const char *const assertion, const char *const file, const int line, const uint v1, const uint v2) noexcept
Definition: DistrhoUtils.hpp:216
static void d_safe_assert_uint(const char *const assertion, const char *const file, const int line, const uint value) noexcept
Definition: DistrhoUtils.hpp:196