DistrhoDefines.h (11462B)
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_DEFINES_H_INCLUDED 18 #define DISTRHO_DEFINES_H_INCLUDED 19 20 /* Compatibility with non-clang compilers */ 21 #ifndef __has_feature 22 # define __has_feature(x) 0 23 #endif 24 #ifndef __has_extension 25 # define __has_extension __has_feature 26 #endif 27 28 /* Check OS */ 29 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 30 # define DISTRHO_API 31 # define DISTRHO_PLUGIN_EXPORT extern "C" __declspec (dllexport) 32 # define DISTRHO_OS_WINDOWS 1 33 # define DISTRHO_DLL_EXTENSION "dll" 34 #else 35 # define DISTRHO_API 36 # define DISTRHO_PLUGIN_EXPORT extern "C" __attribute__ ((visibility("default"))) 37 # if defined(__APPLE__) 38 # define DISTRHO_OS_MAC 1 39 # define DISTRHO_DLL_EXTENSION "dylib" 40 # elif defined(__HAIKU__) 41 # define DISTRHO_OS_HAIKU 1 42 # elif defined(__linux__) || defined(__linux) 43 # define DISTRHO_OS_LINUX 1 44 # elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 45 # define DISTRHO_OS_BSD 1 46 # elif defined(__GNU__) 47 # define DISTRHO_OS_GNU_HURD 1 48 # elif defined(__EMSCRIPTEN__) 49 # define DISTRHO_OS_WASM 1 50 # endif 51 #endif 52 53 #ifndef DISTRHO_DLL_EXTENSION 54 # define DISTRHO_DLL_EXTENSION "so" 55 #endif 56 57 /* Check for C++11 support */ 58 #if defined(HAVE_CPP11_SUPPORT) 59 # if HAVE_CPP11_SUPPORT 60 # define DISTRHO_PROPER_CPP11_SUPPORT 61 # endif 62 #elif __cplusplus >= 201103L || (defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 405) || __has_extension(cxx_noexcept) || (defined(_MSC_VER) && _MSVC_LANG >= 201103L) 63 # define DISTRHO_PROPER_CPP11_SUPPORT 64 # if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) < 407 && ! defined(__clang__)) || (defined(__clang__) && ! __has_extension(cxx_override_control)) 65 # define override /* gcc4.7+ only */ 66 # define final /* gcc4.7+ only */ 67 # endif 68 #endif 69 70 #ifndef DISTRHO_PROPER_CPP11_SUPPORT 71 # define constexpr 72 # define noexcept throw() 73 # define override 74 # define final 75 # define nullptr NULL 76 #endif 77 78 /* Define unlikely */ 79 #ifdef __GNUC__ 80 # define unlikely(x) __builtin_expect(x,0) 81 #else 82 # define unlikely(x) x 83 #endif 84 85 /* Define DISTRHO_DEPRECATED */ 86 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 480 87 # define DISTRHO_DEPRECATED __attribute__((deprecated)) 88 #elif defined(_MSC_VER) 89 # define DISTRHO_DEPRECATED [[deprecated]] /* Note: __declspec(deprecated) it not applicable to enum members */ 90 #else 91 # define DISTRHO_DEPRECATED 92 #endif 93 94 /* Define DISTRHO_DEPRECATED_BY */ 95 #if defined(__clang__) && (__clang_major__ * 100 + __clang_minor__) >= 502 96 # define DISTRHO_DEPRECATED_BY(other) __attribute__((deprecated("", other))) 97 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 408 98 # define DISTRHO_DEPRECATED_BY(other) __attribute__((deprecated("Use " other))) 99 #else 100 # define DISTRHO_DEPRECATED_BY(other) DISTRHO_DEPRECATED 101 #endif 102 103 /* Define DISTRHO_SAFE_ASSERT* */ 104 #define DISTRHO_SAFE_ASSERT(cond) if (unlikely(!(cond))) d_safe_assert (#cond, __FILE__, __LINE__); 105 #define DISTRHO_SAFE_ASSERT_INT(cond, value) if (unlikely(!(cond))) d_safe_assert_int (#cond, __FILE__, __LINE__, static_cast<int>(value)); 106 #define DISTRHO_SAFE_ASSERT_INT2(cond, v1, v2) if (unlikely(!(cond))) d_safe_assert_int2 (#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); 107 #define DISTRHO_SAFE_ASSERT_UINT(cond, value) if (unlikely(!(cond))) d_safe_assert_uint (#cond, __FILE__, __LINE__, static_cast<uint>(value)); 108 #define DISTRHO_SAFE_ASSERT_UINT2(cond, v1, v2) if (unlikely(!(cond))) d_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); 109 110 #define DISTRHO_SAFE_ASSERT_BREAK(cond) if (unlikely(!(cond))) { d_safe_assert(#cond, __FILE__, __LINE__); break; } 111 #define DISTRHO_SAFE_ASSERT_CONTINUE(cond) if (unlikely(!(cond))) { d_safe_assert(#cond, __FILE__, __LINE__); continue; } 112 #define DISTRHO_SAFE_ASSERT_RETURN(cond, ret) if (unlikely(!(cond))) { d_safe_assert(#cond, __FILE__, __LINE__); return ret; } 113 114 #define DISTRHO_CUSTOM_SAFE_ASSERT(msg, cond) if (unlikely(!(cond))) d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); 115 #define DISTRHO_CUSTOM_SAFE_ASSERT_BREAK(msg, cond) if (unlikely(!(cond))) { d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); break; } 116 #define DISTRHO_CUSTOM_SAFE_ASSERT_CONTINUE(msg, cond) if (unlikely(!(cond))) { d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); continue; } 117 #define DISTRHO_CUSTOM_SAFE_ASSERT_RETURN(msg, cond, ret) if (unlikely(!(cond))) { d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); return ret; } 118 119 #define DISTRHO_CUSTOM_SAFE_ASSERT_ONCE_BREAK(msg, cond) if (unlikely(!(cond))) { static bool _p; if (!_p) { _p = true; d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); } break; } 120 #define DISTRHO_CUSTOM_SAFE_ASSERT_ONCE_CONTINUE(msg, cond) if (unlikely(!(cond))) { static bool _p; if (!_p) { _p = true; d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); } continue; } 121 #define DISTRHO_CUSTOM_SAFE_ASSERT_ONCE_RETURN(msg, cond, ret) if (unlikely(!(cond))) { static bool _p; if (!_p) { _p = true; d_custom_safe_assert(msg, #cond, __FILE__, __LINE__); } return ret; } 122 123 #define DISTRHO_SAFE_ASSERT_INT_BREAK(cond, value) if (unlikely(!(cond))) { d_safe_assert_int(#cond, __FILE__, __LINE__, static_cast<int>(value)); break; } 124 #define DISTRHO_SAFE_ASSERT_INT_CONTINUE(cond, value) if (unlikely(!(cond))) { d_safe_assert_int(#cond, __FILE__, __LINE__, static_cast<int>(value)); continue; } 125 #define DISTRHO_SAFE_ASSERT_INT_RETURN(cond, value, ret) if (unlikely(!(cond))) { d_safe_assert_int(#cond, __FILE__, __LINE__, static_cast<int>(value)); return ret; } 126 127 #define DISTRHO_SAFE_ASSERT_INT2_BREAK(cond, v1, v2) if (unlikely(!(cond))) { d_safe_assert_int2(#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); break; } 128 #define DISTRHO_SAFE_ASSERT_INT2_CONTINUE(cond, v1, v2) if (unlikely(!(cond))) { d_safe_assert_int2(#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); continue; } 129 #define DISTRHO_SAFE_ASSERT_INT2_RETURN(cond, v1, v2, ret) if (unlikely(!(cond))) { d_safe_assert_int2(#cond, __FILE__, __LINE__, static_cast<int>(v1), static_cast<int>(v2)); return ret; } 130 131 #define DISTRHO_SAFE_ASSERT_UINT_BREAK(cond, value) if (unlikely(!(cond))) { d_safe_assert_uint(#cond, __FILE__, __LINE__, static_cast<uint>(value)); break; } 132 #define DISTRHO_SAFE_ASSERT_UINT_CONTINUE(cond, value) if (unlikely(!(cond))) { d_safe_assert_uint(#cond, __FILE__, __LINE__, static_cast<uint>(value)); continue; } 133 #define DISTRHO_SAFE_ASSERT_UINT_RETURN(cond, value, ret) if (unlikely(!(cond))) { d_safe_assert_uint(#cond, __FILE__, __LINE__, static_cast<uint>(value)); return ret; } 134 135 #define DISTRHO_SAFE_ASSERT_UINT2_BREAK(cond, v1, v2) if (unlikely(!(cond))) { d_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); break; } 136 #define DISTRHO_SAFE_ASSERT_UINT2_CONTINUE(cond, v1, v2) if (unlikely(!(cond))) { d_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); continue; } 137 #define DISTRHO_SAFE_ASSERT_UINT2_RETURN(cond, v1, v2, ret) if (unlikely(!(cond))) { d_safe_assert_uint2(#cond, __FILE__, __LINE__, static_cast<uint>(v1), static_cast<uint>(v2)); return ret; } 138 139 /* Define DISTRHO_SAFE_EXCEPTION */ 140 #define DISTRHO_SAFE_EXCEPTION(msg) catch(...) { d_safe_exception(msg, __FILE__, __LINE__); } 141 #define DISTRHO_SAFE_EXCEPTION_BREAK(msg) catch(...) { d_safe_exception(msg, __FILE__, __LINE__); break; } 142 #define DISTRHO_SAFE_EXCEPTION_CONTINUE(msg) catch(...) { d_safe_exception(msg, __FILE__, __LINE__); continue; } 143 #define DISTRHO_SAFE_EXCEPTION_RETURN(msg, ret) catch(...) { d_safe_exception(msg, __FILE__, __LINE__); return ret; } 144 145 /* Define DISTRHO_DECLARE_NON_COPYABLE */ 146 #ifdef DISTRHO_PROPER_CPP11_SUPPORT 147 # define DISTRHO_DECLARE_NON_COPYABLE(ClassName) \ 148 private: \ 149 ClassName(ClassName&) = delete; \ 150 ClassName(const ClassName&) = delete; \ 151 ClassName& operator=(ClassName&) = delete; \ 152 ClassName& operator=(const ClassName&) = delete; 153 #else 154 # define DISTRHO_DECLARE_NON_COPYABLE(ClassName) \ 155 private: \ 156 ClassName(ClassName&); \ 157 ClassName(const ClassName&); \ 158 ClassName& operator=(ClassName&); \ 159 ClassName& operator=(const ClassName&); 160 #endif 161 162 /* Define DISTRHO_PREVENT_HEAP_ALLOCATION */ 163 #ifdef DISTRHO_PROPER_CPP11_SUPPORT 164 # define DISTRHO_PREVENT_HEAP_ALLOCATION \ 165 private: \ 166 static void* operator new(size_t) = delete; \ 167 static void operator delete(void*) = delete; 168 #else 169 # define DISTRHO_PREVENT_HEAP_ALLOCATION \ 170 private: \ 171 static void* operator new(size_t); \ 172 static void operator delete(void*); 173 #endif 174 175 /* Define DISTRHO_PREVENT_VIRTUAL_HEAP_ALLOCATION */ 176 #ifdef DISTRHO_PROPER_CPP11_SUPPORT 177 # define DISTRHO_PREVENT_VIRTUAL_HEAP_ALLOCATION \ 178 private: \ 179 static void* operator new(std::size_t) = delete; 180 #else 181 # define DISTRHO_PREVENT_VIRTUAL_HEAP_ALLOCATION \ 182 private: \ 183 static void* operator new(std::size_t); 184 #endif 185 186 /* Define namespace */ 187 #ifndef DISTRHO_NAMESPACE 188 # define DISTRHO_NAMESPACE DISTRHO 189 #endif 190 #define START_NAMESPACE_DISTRHO namespace DISTRHO_NAMESPACE { 191 #define END_NAMESPACE_DISTRHO } 192 #define USE_NAMESPACE_DISTRHO using namespace DISTRHO_NAMESPACE; 193 194 /* Define DISTRHO_OS_SEP and DISTRHO_OS_SPLIT */ 195 #ifdef DISTRHO_OS_WINDOWS 196 # define DISTRHO_OS_SEP '\\' 197 # define DISTRHO_OS_SEP_STR "\\" 198 # define DISTRHO_OS_SPLIT ';' 199 # define DISTRHO_OS_SPLIT_STR ";" 200 #else 201 # define DISTRHO_OS_SEP '/' 202 # define DISTRHO_OS_SEP_STR "/" 203 # define DISTRHO_OS_SPLIT ':' 204 # define DISTRHO_OS_SPLIT_STR ":" 205 #endif 206 207 /* MSVC warnings */ 208 #ifdef _MSC_VER 209 # define strdup _strdup 210 # pragma warning(disable:4244) /* possible loss of data */ 211 #endif 212 213 /* Useful macros */ 214 #define ARRAY_SIZE(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0])) 215 #define STRINGIFY2(s) #s 216 #define STRINGIFY(s) STRINGIFY2(s) 217 218 /* Useful typedefs */ 219 typedef unsigned char uchar; 220 typedef unsigned short int ushort; 221 typedef unsigned int uint; 222 typedef unsigned long int ulong; 223 typedef unsigned long long int ulonglong; 224 225 /* Deprecated macros */ 226 #define DISTRHO_DECLARE_NON_COPY_CLASS(ClassName) DISTRHO_DECLARE_NON_COPYABLE(ClassName) 227 #define DISTRHO_DECLARE_NON_COPY_STRUCT(StructName) DISTRHO_DECLARE_NON_COPYABLE(StructName) 228 #define DISTRHO_MACRO_AS_STRING(MACRO) STRINGIFY2(MACRO) 229 230 #endif // DISTRHO_DEFINES_H_INCLUDED