Add initial bits for Qt6 support
[carla.git] / source / modules / distrho / DistrhoUtils.hpp
bloba76948662cc89b74d787462a62dfc8bb04c1af6e
1 /*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
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.
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.
17 #ifndef DISTRHO_UTILS_HPP_INCLUDED
18 #define DISTRHO_UTILS_HPP_INCLUDED
20 #include "src/DistrhoDefines.h"
22 #include <cstdarg>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
27 #include <cmath>
28 #include <limits>
30 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
31 # include <cstdint>
32 #else
33 # include <stdint.h>
34 #endif
36 #if defined(DISTRHO_OS_WINDOWS) && defined(_MSC_VER)
37 #include <basetsd.h>
38 typedef SSIZE_T ssize_t;
39 #endif
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); }
52 #endif
54 #ifndef M_PI
55 # define M_PI 3.14159265358979323846
56 #endif
58 #define DISTRHO_MACRO_AS_STRING_VALUE(MACRO) #MACRO
59 #define DISTRHO_MACRO_AS_STRING(MACRO) DISTRHO_MACRO_AS_STRING_VALUE(MACRO)
61 /* ------------------------------------------------------------------------------------------------------------
62 * misc functions */
64 /**
65 @defgroup MiscellaneousFunctions Miscellaneous functions
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.
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
77 return (a << 24) | (b << 16) | (c << 8) | (d << 0);
80 /**
81 Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
83 static inline constexpr
84 uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
86 return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
89 /**
90 Dummy, no-op function.
92 static inline
93 void d_pass() noexcept {}
95 /** @} */
97 /* ------------------------------------------------------------------------------------------------------------
98 * string print functions */
101 @defgroup StringPrintFunctions String print functions
107 Print a string to stdout with newline (gray color).
108 Does nothing if DEBUG is not defined.
110 #ifndef DEBUG
111 # define d_debug(...)
112 #else
113 static inline
114 void d_debug(const char* const fmt, ...) noexcept
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 (...) {}
125 #endif
128 Print a string to stdout with newline.
130 static inline
131 void d_stdout(const char* const fmt, ...) noexcept
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 (...) {}
143 Print a string to stderr with newline.
145 static inline
146 void d_stderr(const char* const fmt, ...) noexcept
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 (...) {}
158 Print a string to stderr with newline (red color).
160 static inline
161 void d_stderr2(const char* const fmt, ...) noexcept
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 (...) {}
174 Print a safe assertion error message.
176 static inline
177 void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
179 d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
183 Print a safe assertion error message, with 1 extra signed integer value.
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
189 d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
193 Print a safe assertion error message, with 1 extra unsigned integer value.
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
199 d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
203 Print a safe assertion error message, with 2 extra signed integer values.
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
209 d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
213 Print a safe assertion error message, with 2 extra unsigned integer values.
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
219 d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
223 Print a safe assertion error message, with a custom error message.
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
229 d_stderr2("assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
233 Print a safe exception error message.
235 static inline
236 void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
238 d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
241 /** @} */
243 /* ------------------------------------------------------------------------------------------------------------
244 * math functions */
247 @defgroup MathFunctions Math related functions
253 Safely compare two floating point numbers.
254 Returns true if they match.
256 template<typename T>
257 static inline
258 bool d_isEqual(const T& v1, const T& v2)
260 return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
264 Safely compare two floating point numbers.
265 Returns true if they don't match.
267 template<typename T>
268 static inline
269 bool d_isNotEqual(const T& v1, const T& v2)
271 return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
275 Safely check if a floating point number is zero.
277 template<typename T>
278 static inline
279 bool d_isZero(const T& value)
281 return std::abs(value) < std::numeric_limits<T>::epsilon();
285 Safely check if a floating point number is not zero.
287 template<typename T>
288 static inline
289 bool d_isNotZero(const T& value)
291 return std::abs(value) >= std::numeric_limits<T>::epsilon();
295 Get next power of 2.
297 static inline
298 uint32_t d_nextPowerOf2(uint32_t size) noexcept
300 DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
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;
312 /** @} */
314 // -----------------------------------------------------------------------
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
323 // -----------------------------------------------------------------------
325 #endif // DISTRHO_UTILS_HPP_INCLUDED