1 /*
2  * Carla common defines
3  * Copyright (C) 2011-2021 Filipe Coelho <falktx@falktx.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16  */
17 
18 #ifndef CARLA_DEFINES_H_INCLUDED
19 #define CARLA_DEFINES_H_INCLUDED
20 
21 /* IDE Helper */
22 #ifndef REAL_BUILD
23 # include "config.h"
24 #endif
25 
26 /* Compatibility with non-clang compilers */
27 #ifndef __has_feature
28 # define __has_feature(x) 0
29 #endif
30 #ifndef __has_extension
31 # define __has_extension __has_feature
32 #endif
33 
34 /* Set Version */
35 #define CARLA_VERSION_HEX    0x020401
36 #define CARLA_VERSION_STRING "2.4.1"
37 #define CARLA_VERSION_STRMIN "2.4"
38 
39 /* Check OS */
40 #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(_M_ARM64)
41 # define CARLA_OS_WIN64
42 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
43 # define CARLA_OS_WIN32
44 #elif defined(__APPLE__)
45 # define CARLA_OS_MAC
46 #elif defined(__HAIKU__)
47 # define CARLA_OS_HAIKU
48 #elif defined(__linux__) || defined(__linux)
49 # define CARLA_OS_LINUX
50 #elif defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
51 # define CARLA_OS_BSD
52 #elif defined(__GNU__)
53 # define CARLA_OS_GNU_HURD
54 #else
55 # warning Unsupported platform!
56 #endif
57 
58 #if defined(CARLA_OS_WIN32) || defined(CARLA_OS_WIN64)
59 # define CARLA_OS_WIN
60 #elif defined(CARLA_OS_BSD) || defined(CARLA_OS_GNU_HURD) || defined(CARLA_OS_LINUX) || defined(CARLA_OS_MAC)
61 # define CARLA_OS_UNIX
62 #endif
63 
64 #if defined(__LP64__) || defined(_LP64) || defined(__arm64__) || defined(__aarch64__) || defined(CARLA_OS_WIN64)
65 # define CARLA_OS_64BIT
66 #endif
67 
68 /* Check for C++11 support */
69 #if defined(HAVE_CPP11_SUPPORT)
70 # if HAVE_CPP11_SUPPORT
71 #   define CARLA_PROPER_CPP11_SUPPORT
72 # endif
73 #elif defined(__cplusplus)
74 # if __cplusplus >= 201103L || (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 && defined(__GXX_EXPERIMENTAL_CXX0X__)) || __has_extension(cxx_noexcept)
75 #  define CARLA_PROPER_CPP11_SUPPORT
76 #  if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 407 && ! defined(__clang__)) || (defined(__clang__) && ! __has_extension(cxx_override_control))
77 #   define override // gcc4.7+ only
78 #   define final    // gcc4.7+ only
79 #  endif
80 # endif
81 #endif
82 
83 #if defined(__cplusplus) && ! defined(CARLA_PROPER_CPP11_SUPPORT)
84 # define noexcept throw()
85 # define override
86 # define final
87 # define nullptr NULL
88 #endif
89 
90 /* Common includes */
91 #ifdef __cplusplus
92 # include <cstddef>
93 #else
94 # include <stdbool.h>
95 # include <stddef.h>
96 #endif
97 
98 /* Define various string format types */
99 #if defined(CARLA_OS_WIN64)
100 # define P_INT64   "%lli"
101 # define P_UINT64  "%llu"
102 # define P_INTPTR  "%lli"
103 # define P_UINTPTR "%llx"
104 # define P_SIZE    "%llu"
105 # define P_SSIZE   "%lli"
106 #elif defined(CARLA_OS_WIN32)
107 # define P_INT64   "%lli"
108 # define P_UINT64  "%llu"
109 # define P_INTPTR  "%i"
110 # define P_UINTPTR "%x"
111 # define P_SIZE    "%u"
112 # define P_SSIZE   "%i"
113 #elif defined(CARLA_OS_MAC)
114 # define P_INT64   "%lli"
115 # define P_UINT64  "%llu"
116 # define P_INTPTR  "%li"
117 # define P_UINTPTR "%lx"
118 # define P_SIZE    "%lu"
119 # define P_SSIZE   "%li"
120 #elif (defined(__WORDSIZE)     && __WORDSIZE     == 64) || \
121       (defined(__SIZE_WIDTH__) && __SIZE_WIDTH__ == 64) || \
122       (defined(CARLA_OS_HAIKU) && defined(CARLA_OS_64BIT))
123 # define P_INT64   "%li"
124 # define P_UINT64  "%lu"
125 # define P_INTPTR  "%li"
126 # define P_UINTPTR "%lx"
127 # define P_SIZE    "%lu"
128 # define P_SSIZE   "%li"
129 #else
130 # define P_INT64   "%lli"
131 # define P_UINT64  "%llu"
132 # define P_INTPTR  "%i"
133 # define P_UINTPTR "%x"
134 # define P_SIZE    "%u"
135 # define P_SSIZE   "%i"
136 #endif
137 
138 /* Define BINARY_NATIVE */
139 #if defined(CARLA_OS_WIN)
140 # ifdef CARLA_OS_WIN64
141 #  define BINARY_NATIVE BINARY_WIN64
142 # else
143 #  define BINARY_NATIVE BINARY_WIN32
144 # endif
145 #else
146 # ifdef CARLA_OS_64BIT
147 #  define BINARY_NATIVE BINARY_POSIX64
148 # else
149 #  define BINARY_NATIVE BINARY_POSIX32
150 # endif
151 #endif
152 
153 /* Define CARLA_ASSERT* */
154 #if defined(CARLA_NO_ASSERTS)
155 # define CARLA_ASSERT(cond)
156 # define CARLA_ASSERT_INT(cond, value)
157 # define CARLA_ASSERT_INT2(cond, v1, v2)
158 #elif defined(NDEBUG)
159 # define CARLA_ASSERT      CARLA_SAFE_ASSERT
160 # define CARLA_ASSERT_INT  CARLA_SAFE_ASSERT_INT
161 # define CARLA_ASSERT_INT2 CARLA_SAFE_ASSERT_INT2
162 #else
163 # define CARLA_ASSERT(cond)              assert(cond)
164 # define CARLA_ASSERT_INT(cond, value)   assert(cond)
165 # define CARLA_ASSERT_INT2(cond, v1, v2) assert(cond)
166 #endif
167 
168 /* Define CARLA_SAFE_ASSERT* */
169 #define CARLA_SAFE_ASSERT(cond)               if (! (cond)) carla_safe_assert      (#cond, __FILE__, __LINE__);
170 #define CARLA_SAFE_ASSERT_INT(cond, value)    if (! (cond)) carla_safe_assert_int  (#cond, __FILE__, __LINE__, static_cast<int>(value));
171 #define CARLA_SAFE_ASSERT_INT2(cond, v1, v2)  if (! (cond)) carla_safe_assert_int2 (#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2));
172 #define CARLA_SAFE_ASSERT_UINT(cond, value)   if (! (cond)) carla_safe_assert_uint (#cond, __FILE__, __LINE__, static_cast<uint>(value));
173 #define CARLA_SAFE_ASSERT_UINT2(cond, v1, v2) if (! (cond)) carla_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2));
174 
175 #define CARLA_SAFE_ASSERT_BREAK(cond)         if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); break; }
176 #define CARLA_SAFE_ASSERT_CONTINUE(cond)      if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); continue; }
177 #define CARLA_SAFE_ASSERT_RETURN(cond, ret)   if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); return ret; }
178 
179 #define CARLA_CUSTOM_SAFE_ASSERT(msg, cond)             if (! (cond)) carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__);
180 #define CARLA_CUSTOM_SAFE_ASSERT_BREAK(msg, cond)       if (! (cond)) { carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__); break; }
181 #define CARLA_CUSTOM_SAFE_ASSERT_CONTINUE(msg, cond)    if (! (cond)) { carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__); continue; }
182 #define CARLA_CUSTOM_SAFE_ASSERT_RETURN(msg, cond, ret) if (! (cond)) { carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__); return ret; }
183 
184 #define CARLA_CUSTOM_SAFE_ASSERT_ONCE_BREAK(msg, cond)       if (! (cond)) { static bool _p; if (!_p) { _p = true; carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__); } break; }
185 #define CARLA_CUSTOM_SAFE_ASSERT_ONCE_CONTINUE(msg, cond)    if (! (cond)) { static bool _p; if (!_p) { _p = true; carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__); } continue; }
186 #define CARLA_CUSTOM_SAFE_ASSERT_ONCE_RETURN(msg, cond, ret) if (! (cond)) { static bool _p; if (!_p) { _p = true; carla_custom_safe_assert(msg, #cond, __FILE__, __LINE__); } return ret; }
187 
188 #define CARLA_SAFE_ASSERT_INT_BREAK(cond, value)       if (! (cond)) { carla_safe_assert_int(#cond, __FILE__, __LINE__, static_cast<int>(value); break; }
189 #define CARLA_SAFE_ASSERT_INT_CONTINUE(cond, value)    if (! (cond)) { carla_safe_assert_int(#cond, __FILE__, __LINE__, static_cast<int>(value)); continue; }
190 #define CARLA_SAFE_ASSERT_INT_RETURN(cond, value, ret) if (! (cond)) { carla_safe_assert_int(#cond, __FILE__, __LINE__, static_cast<int>(value)); return ret; }
191 
192 #define CARLA_SAFE_ASSERT_INT2_BREAK(cond, v1, v2)        if (! (cond)) { carla_safe_assert_int2(#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); break; }
193 #define CARLA_SAFE_ASSERT_INT2_CONTINUE(cond, v1, v2)     if (! (cond)) { carla_safe_assert_int2(#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); continue; }
194 #define CARLA_SAFE_ASSERT_INT2_RETURN(cond, v1, v2, ret)  if (! (cond)) { carla_safe_assert_int2(#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); return ret; }
195 
196 #define CARLA_SAFE_ASSERT_UINT_BREAK(cond, value)       if (! (cond)) { carla_safe_assert_uint(#cond, __FILE__, __LINE__, static_cast<uint>(value); break; }
197 #define CARLA_SAFE_ASSERT_UINT_CONTINUE(cond, value)    if (! (cond)) { carla_safe_assert_uint(#cond, __FILE__, __LINE__, static_cast<uint>(value)); continue; }
198 #define CARLA_SAFE_ASSERT_UINT_RETURN(cond, value, ret) if (! (cond)) { carla_safe_assert_uint(#cond, __FILE__, __LINE__, static_cast<uint>(value)); return ret; }
199 
200 #define CARLA_SAFE_ASSERT_UINT2_BREAK(cond, v1, v2)       if (! (cond)) { carla_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); break; }
201 #define CARLA_SAFE_ASSERT_UINT2_CONTINUE(cond, v1, v2)    if (! (cond)) { carla_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); continue; }
202 #define CARLA_SAFE_ASSERT_UINT2_RETURN(cond, v1, v2, ret) if (! (cond)) { carla_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); return ret; }
203 
204 #if defined(__GNUC__) && defined(CARLA_PROPER_CPP11_SUPPORT) && ! defined(__clang__)
205 # define CARLA_CATCH_UNWIND catch (abi::__forced_unwind&) { throw; }
206 # if __GNUC__ >= 6
207 #  pragma GCC diagnostic push
208 #  pragma GCC diagnostic ignored "-Wterminate"
209 # endif
210 #else
211 # define CARLA_CATCH_UNWIND
212 #endif
213 
214 /* Define CARLA_SAFE_EXCEPTION */
215 #define CARLA_SAFE_EXCEPTION(msg)             CARLA_CATCH_UNWIND catch(...) { carla_safe_exception(msg, __FILE__, __LINE__); }
216 #define CARLA_SAFE_EXCEPTION_BREAK(msg)       CARLA_CATCH_UNWIND catch(...) { carla_safe_exception(msg, __FILE__, __LINE__); break; }
217 #define CARLA_SAFE_EXCEPTION_CONTINUE(msg)    CARLA_CATCH_UNWIND catch(...) { carla_safe_exception(msg, __FILE__, __LINE__); continue; }
218 #define CARLA_SAFE_EXCEPTION_RETURN(msg, ret) CARLA_CATCH_UNWIND catch(...) { carla_safe_exception(msg, __FILE__, __LINE__); return ret; }
219 
220 /* Define CARLA_DECLARE_NON_COPY_CLASS */
221 #ifdef CARLA_PROPER_CPP11_SUPPORT
222 # define CARLA_DECLARE_NON_COPY_CLASS(ClassName) \
223 private:                                         \
224     ClassName(ClassName&) = delete;              \
225     ClassName(const ClassName&) = delete;        \
226     ClassName& operator=(ClassName&) = delete;   \
227     ClassName& operator=(const ClassName&) = delete;
228 #else
229 # define CARLA_DECLARE_NON_COPY_CLASS(ClassName) \
230 private:                                         \
231     ClassName(ClassName&);                       \
232     ClassName(const ClassName&);                 \
233     ClassName& operator=(ClassName&);            \
234     ClassName& operator=(const ClassName&);
235 #endif
236 
237 /* Define CARLA_DECLARE_NON_COPY_STRUCT */
238 #ifdef CARLA_PROPER_CPP11_SUPPORT
239 # define CARLA_DECLARE_NON_COPY_STRUCT(StructName) \
240     StructName(StructName&) = delete;              \
241     StructName(const StructName&) = delete;        \
242     StructName& operator=(StructName&) = delete;   \
243     StructName& operator=(const StructName&) = delete;
244 #else
245 # define CARLA_DECLARE_NON_COPY_STRUCT(StructName)
246 #endif
247 
248 /* Define CARLA_PREVENT_HEAP_ALLOCATION */
249 #ifdef CARLA_PROPER_CPP11_SUPPORT
250 # define CARLA_PREVENT_HEAP_ALLOCATION \
251 private:                               \
252     static void* operator new(std::size_t) = delete; \
253     static void operator delete(void*) = delete;
254 #else
255 # define CARLA_PREVENT_HEAP_ALLOCATION \
256 private:                               \
257     static void* operator new(std::size_t); \
258     static void operator delete(void*);
259 #endif
260 
261 /* Define CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION */
262 #ifdef CARLA_PROPER_CPP11_SUPPORT
263 # define CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION  \
264 private:                                        \
265     static void* operator new(std::size_t) = delete;
266 #else
267 # define CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION  \
268 private:                                        \
269     static void* operator new(std::size_t);
270 #endif
271 
272 /* Define CARLA_API */
273 #if defined(BUILD_BRIDGE) || defined(STATIC_PLUGIN_TARGET)
274 # define CARLA_API
275 #else
276 # if defined(CARLA_OS_WIN) && ! defined(__WINE__)
277 #  ifdef BUILDING_CARLA
278 #   define CARLA_API __declspec (dllexport)
279 #  else
280 #   define CARLA_API __declspec (dllimport)
281 #  endif
282 # else
283 #  define CARLA_API __attribute__ ((visibility("default")))
284 # endif
285 #endif
286 
287 /* Define CARLA_EXTERN_C */
288 #ifdef __cplusplus
289 # define CARLA_EXTERN_C extern "C"
290 #else
291 # define CARLA_EXTERN_C
292 #endif
293 
294 /* Define CARLA_EXPORT */
295 #ifdef BUILD_BRIDGE
296 # define CARLA_EXPORT CARLA_EXTERN_C
297 #else
298 # if defined(CARLA_OS_WIN) && ! defined(__WINE__)
299 #  define CARLA_EXPORT CARLA_EXTERN_C CARLA_API
300 # else
301 #  define CARLA_EXPORT CARLA_EXTERN_C CARLA_API
302 # endif
303 #endif
304 
305 /* Define CARLA_OS_SEP */
306 #ifdef CARLA_OS_WIN
307 # define CARLA_OS_SEP       '\\'
308 # define CARLA_OS_SEP_STR   "\\"
309 # define CARLA_OS_SPLIT     ';'
310 # define CARLA_OS_SPLIT_STR ";"
311 #else
312 # define CARLA_OS_SEP       '/'
313 # define CARLA_OS_SEP_STR   "/"
314 # define CARLA_OS_SPLIT     ':'
315 # define CARLA_OS_SPLIT_STR ":"
316 #endif
317 
318 /* Useful typedefs */
319 typedef unsigned char uchar;
320 typedef unsigned long int ulong;
321 typedef unsigned short int ushort;
322 typedef unsigned int uint;
323 
324 #endif /* CARLA_DEFINES_H_INCLUDED */
325