DISTRHO Plugin Framework
 All Classes Functions Variables Modules Pages
DistrhoUtils.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2015 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_MAC) && ! defined(CARLA_OS_MAC)
37 namespace std {
38 inline float fmin(float __x, float __y)
39  { return __builtin_fminf(__x, __y); }
40 inline float fmax(float __x, float __y)
41  { return __builtin_fmaxf(__x, __y); }
42 inline float rint(float __x)
43  { return __builtin_rintf(__x); }
44 inline float round(float __x)
45  { return __builtin_roundf(__x); }
46 }
47 #endif
48 
49 // -----------------------------------------------------------------------
50 // misc functions
51 
52 /*
53  * Return a 64-bit number from 4 8-bit numbers.
54  */
55 static inline
56 int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
57 {
58  return (a << 24) | (b << 16) | (c << 8) | (d << 0);
59 }
60 
61 /*
62  * Dummy function.
63  */
64 static inline
65 void d_pass() noexcept {}
66 
67 // -----------------------------------------------------------------------
68 // string print functions
69 
70 /*
71  * Print a string to stdout with newline (gray color).
72  * Does nothing if DEBUG is not defined.
73  */
74 #ifndef DEBUG
75 # define d_debug(...)
76 #else
77 static inline
78 void d_debug(const char* const fmt, ...) noexcept
79 {
80  try {
81  ::va_list args;
82  ::va_start(args, fmt);
83  std::fprintf(stdout, "\x1b[30;1m");
84  std::vfprintf(stdout, fmt, args);
85  std::fprintf(stdout, "\x1b[0m\n");
86  ::va_end(args);
87  } catch (...) {}
88 }
89 #endif
90 
91 /*
92  * Print a string to stdout with newline.
93  */
94 static inline
95 void d_stdout(const char* const fmt, ...) noexcept
96 {
97  try {
98  ::va_list args;
99  ::va_start(args, fmt);
100  std::vfprintf(stdout, fmt, args);
101  std::fprintf(stdout, "\n");
102  ::va_end(args);
103  } catch (...) {}
104 }
105 
106 /*
107  * Print a string to stderr with newline.
108  */
109 static inline
110 void d_stderr(const char* const fmt, ...) noexcept
111 {
112  try {
113  ::va_list args;
114  ::va_start(args, fmt);
115  std::vfprintf(stderr, fmt, args);
116  std::fprintf(stderr, "\n");
117  ::va_end(args);
118  } catch (...) {}
119 }
120 
121 /*
122  * Print a string to stderr with newline (red color).
123  */
124 static inline
125 void d_stderr2(const char* const fmt, ...) noexcept
126 {
127  try {
128  ::va_list args;
129  ::va_start(args, fmt);
130  std::fprintf(stderr, "\x1b[31m");
131  std::vfprintf(stderr, fmt, args);
132  std::fprintf(stderr, "\x1b[0m\n");
133  ::va_end(args);
134  } catch (...) {}
135 }
136 
137 /*
138  * Print a safe assertion error message.
139  */
140 static inline
141 void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
142 {
143  d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
144 }
145 
146 /*
147  * Print a safe exception error message.
148  */
149 static inline
150 void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
151 {
152  d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
153 }
154 
155 // -----------------------------------------------------------------------
156 // math functions
157 
158 /*
159  * Safely compare two floating point numbers.
160  * Returns true if they match.
161  */
162 template<typename T>
163 static inline
164 bool d_isEqual(const T& v1, const T& v2)
165 {
166  return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
167 }
168 
169 /*
170  * Safely compare two floating point numbers.
171  * Returns true if they don't match.
172  */
173 template<typename T>
174 static inline
175 bool d_isNotEqual(const T& v1, const T& v2)
176 {
177  return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
178 }
179 
180 /*
181  * Safely check if a floating point number is zero.
182  */
183 template<typename T>
184 static inline
185 bool d_isZero(const T& value)
186 {
187  return std::abs(value) < std::numeric_limits<T>::epsilon();
188 }
189 
190 /*
191  * Safely check if a floating point number is not zero.
192  */
193 template<typename T>
194 static inline
195 bool d_isNotZero(const T& value)
196 {
197  return std::abs(value) >= std::numeric_limits<T>::epsilon();
198 }
199 
200 // -----------------------------------------------------------------------
201 
202 #endif // DISTRHO_UTILS_HPP_INCLUDED