flprefix.h 12.4 KB
Newer Older
Thomas Grill's avatar
Thomas Grill committed
1 2
/*
flext - C++ layer for Max and Pure Data externals
thomas's avatar
thomas committed
3

4
Copyright (c) 2001-2017 Thomas Grill (gr@grrrr.org)
thomas's avatar
thomas committed
5
For information on usage and redistribution, and for a DISCLAIMER OF ALL
Thomas Grill's avatar
Thomas Grill committed
6
WARRANTIES, see the file, "license.txt," in this distribution.
thomas's avatar
thomas committed
7 8 9 10 11 12 13 14 15
*/

/*! \file flprefix.h
    \brief Try to find out the platform.
*/
 
#ifndef __FLEXT_PREFIX_H
#define __FLEXT_PREFIX_H

thomas's avatar
thomas committed
16
// --- definitions for FLEXT_SYS ---------------------
thomas's avatar
thomas committed
17
#define FLEXT_SYS_UNKNOWN   0
thomas's avatar
thomas committed
18

thomas's avatar
thomas committed
19
#ifndef FLEXT_SYS_MAX
thomas's avatar
thomas committed
20
    #define FLEXT_SYS_MAX   1
thomas's avatar
thomas committed
21
#else
thomas's avatar
thomas committed
22 23 24 25
    // already defined
    #undef FLEXT_SYS_MAX
    #define FLEXT_SYS_MAX   1
    #define FLEXT_SYS FLEXT_SYS_MAX
thomas's avatar
thomas committed
26 27 28
#endif

#ifndef FLEXT_SYS_PD
thomas's avatar
thomas committed
29
    #define FLEXT_SYS_PD    2
thomas's avatar
thomas committed
30
#else
thomas's avatar
thomas committed
31 32 33 34
    // already defined
    #undef FLEXT_SYS_PD
    #define FLEXT_SYS_PD    2
    #define FLEXT_SYS FLEXT_SYS_PD
thomas's avatar
thomas committed
35 36
#endif

thomas's avatar
thomas committed
37
#ifndef FLEXT_SYS_JMAX
thomas's avatar
thomas committed
38
    #define FLEXT_SYS_JMAX  3
thomas's avatar
thomas committed
39
#else
thomas's avatar
thomas committed
40 41 42 43
    // already defined
    #undef FLEXT_SYS_JMAX
    #define FLEXT_SYS_JMAX  3
    #define FLEXT_SYS FLEXT_SYS_JMAX
thomas's avatar
thomas committed
44 45
#endif

thomas's avatar
thomas committed
46
// --- definitions for FLEXT_OS ----------------------
thomas's avatar
thomas committed
47 48 49 50 51
#define FLEXT_OS_UNKNOWN    0
#define FLEXT_OS_WIN    1
#define FLEXT_OS_MAC    2  
#define FLEXT_OS_LINUX  3
#define FLEXT_OS_IRIX   4
thomas's avatar
thomas committed
52

thomas's avatar
thomas committed
53
// --- definitions for FLEXT_OS_API ---------------------
thomas's avatar
thomas committed
54
#define FLEXT_OSAPI_UNKNOWN 0
55 56 57 58 59

#define FLEXT_OSAPI_UNIX_POSIX 1

#define FLEXT_OSAPI_MAC_CLASSIC 2
#define FLEXT_OSAPI_MAC_CARBON 3
thomas's avatar
thomas committed
60
#define FLEXT_OSAPI_MAC_MACH 4
61 62

#define FLEXT_OSAPI_WIN_NATIVE 5  // WIN32 Platform
thomas's avatar
thomas committed
63
#define FLEXT_OSAPI_WIN_POSIX 6    // POSIX API (e.g. cygwin)
thomas's avatar
thomas committed
64

thomas's avatar
thomas committed
65
// --- definitions for FLEXT_CPU ---------------------
thomas's avatar
thomas committed
66
#define FLEXT_CPU_UNKNOWN   0
thomas's avatar
thomas committed
67 68 69 70 71 72 73
#define FLEXT_CPU_IA32   1
#define FLEXT_CPU_PPC    2
#define FLEXT_CPU_MIPS   3
#define FLEXT_CPU_ALPHA  4

#define FLEXT_CPU_IA64   5 // Itanium
#define FLEXT_CPU_X86_64 6 // AMD-K8, EMT64
thomas's avatar
thomas committed
74
#define FLEXT_CPU_PPC64  7 // G5 in 64 bit mode
thomas's avatar
thomas committed
75 76 77

// compatibility
#define FLEXT_CPU_INTEL FLEXT_CPU_IA32
thomas's avatar
thomas committed
78

thomas's avatar
thomas committed
79 80
// --- definitions for FLEXT_THREADS -----------------
#define FLEXT_THR_POSIX 1 // pthreads
thomas's avatar
thomas committed
81 82
#define FLEXT_THR_WIN32 2 // Win32 native
#define FLEXT_THR_MP    3 // MacOS MPThreads
thomas's avatar
thomas committed
83

thomas's avatar
thomas committed
84 85
// ---------------------------------------------------
// support old definitions
thomas's avatar
thomas committed
86

thomas's avatar
thomas committed
87
#ifndef FLEXT_SYS
thomas's avatar
thomas committed
88 89 90 91 92 93 94 95
    #if defined(MAXMSP)
        #define FLEXT_SYS FLEXT_SYS_MAX
    //  #undef MAXMSP
    #elif defined(PD)
        #define FLEXT_SYS FLEXT_SYS_PD
    //  #undef PD
    //  #undef NT
    #endif
thomas's avatar
thomas committed
96 97
#endif

thomas's avatar
thomas committed
98
#if defined(_DEBUG) && !defined(FLEXT_DEBUG)
thomas's avatar
thomas committed
99
    #define FLEXT_DEBUG
thomas's avatar
thomas committed
100 101
#endif

thomas's avatar
thomas committed
102 103
// ---------------------------------------------------

thomas's avatar
thomas committed
104
// Definition of supported real-time systems
thomas's avatar
thomas committed
105
#if FLEXT_SYS == FLEXT_SYS_MAX || FLEXT_SYS == FLEXT_SYS_PD
thomas's avatar
thomas committed
106
#else
thomas's avatar
thomas committed
107
    #error "System must be defined by either FLEXT_SYS_MAX or FLEXT_SYS_PD"
thomas's avatar
thomas committed
108 109 110
#endif

// Definition of OS/CPU
111
#if defined(_MSC_VER) || (defined(__ICC) && (FLEXT_OS == FLEXT_OS_WIN || defined(_WIN32)))
thomas's avatar
thomas committed
112
    // Microsoft C++
113
    // and Intel C++ (as guessed)
thomas's avatar
thomas committed
114 115
    
    #ifndef FLEXT_CPU
thomas's avatar
thomas committed
116 117 118 119 120 121
        #if defined(_M_AMD64)
            #define FLEXT_CPU FLEXT_CPU_X86_64
        #elif defined(_M_IA64)
            #define FLEXT_CPU FLEXT_CPU_IA64
        #elif defined(_M_IX86)
            #define FLEXT_CPU FLEXT_CPU_IA32
thomas's avatar
thomas committed
122 123 124 125 126 127 128 129 130 131 132 133
        #elif defined(_M_PPC)
            #define FLEXT_CPU FLEXT_CPU_PPC
        #elif defined(_M_MRX000)
            #define FLEXT_CPU FLEXT_CPU_MIPS
        #elif defined(_M_ALPHA)
            #define FLEXT_CPU FLEXT_CPU_ALPHA
        #else
            #define FLEXT_CPU FLEXT_CPU_UNKNOWN
        #endif
    #endif

    #ifndef FLEXT_OS
thomas's avatar
thomas committed
134
        #if defined(_WIN32) || defined(_WIN64)
thomas's avatar
thomas committed
135 136 137 138 139 140 141
            #define FLEXT_OS FLEXT_OS_WIN
            #define FLEXT_OSAPI FLEXT_OSAPI_WIN_NATIVE
        #else
            #define FLEXT_OS FLEXT_OS_UNKNOWN
            #define FLEXT_OSAPI FLEXT_OSAPI_UNKNOWN
        #endif
    #endif
thomas's avatar
thomas committed
142

thomas's avatar
thomas committed
143

thomas's avatar
thomas committed
144
#elif defined(__BORLANDC__) 
thomas's avatar
thomas committed
145
    // Borland C++
thomas's avatar
thomas committed
146

thomas's avatar
thomas committed
147 148 149 150 151 152 153 154 155
    #ifndef FLEXT_CPU
        #define FLEXT_CPU FLEXT_CPU_INTEL
    #endif
    #ifndef FLEXT_OS
        #define FLEXT_OS FLEXT_OS_WIN
        #define FLEXT_OSAPI FLEXT_OSAPI_WIN_NATIVE
    #else   
        #define FLEXT_OSAPI FLEXT_OSAPI_UNKNOWN
    #endif
thomas's avatar
thomas committed
156

thomas's avatar
thomas committed
157

thomas's avatar
thomas committed
158
#elif defined(__MWERKS__)
thomas's avatar
thomas committed
159 160 161 162
    // Metrowerks CodeWarrior

    #ifdef __MACH__
        // quick fix for OSX Mach-O
thomas's avatar
thomas committed
163 164 165 166 167 168 169 170 171 172 173 174 175
        #ifdef __POWERPC__
            #ifdef __LP64__
                #define TARGET_CPU_PPC64 1
            #else
                #define TARGET_CPU_PPC 1
            #endif
        #else
            #ifdef __LP64__
                #define TARGET_CPU_X86_64 1
            #else
                #define TARGET_CPU_IA32 1
            #endif
        #endif
thomas's avatar
thomas committed
176 177 178 179 180 181 182 183 184
        #define TARGET_OS_MAC 1
        #define TARGET_API_MAC_OSX 1
    #else
        #ifndef __CONDITIONALMACROS__
        #include <ConditionalMacros.h>
        #endif
    #endif

    #ifndef FLEXT_CPU
thomas's avatar
thomas committed
185 186 187 188 189 190
        #if TARGET_CPU_X86_64
            #define FLEXT_CPU FLEXT_CPU_X86_64
        #elif TARGET_CPU_X86
            #define FLEXT_CPU FLEXT_CPU_IA32
        #elif TARGET_CPU_PPC64
            #define FLEXT_CPU FLEXT_CPU_PPC64
thomas's avatar
thomas committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        #elif TARGET_CPU_PPC
            #define FLEXT_CPU FLEXT_CPU_PPC
        #elif TARGET_CPU_MIPS
            #define FLEXT_CPU FLEXT_CPU_MIPS
        #elif TARGET_CPU_ALPHA
            #define FLEXT_CPU FLEXT_CPU_ALPHA
        #else
            #define FLEXT_CPU FLEXT_CPU_UNKNOWN
        #endif
    #endif

    #ifndef FLEXT_OS
        #if TARGET_OS_MAC
            #define FLEXT_OS FLEXT_OS_MAC
        #elif TARGET_OS_WIN32
            // assume Windows
            #define FLEXT_OS FLEXT_OS_WIN
        #else
            #define FLEXT_OS FLEXT_OS_UNKNOWN
        #endif
    #endif
    
    #ifndef FLEXT_OSAPI
        #if TARGET_API_MAC_MACH
            // this is for Mach-O
            // this has the precedence (MACH also supports Carbon, of course)
            #define FLEXT_OSAPI FLEXT_OSAPI_MAC_MACH
        #elif TARGET_API_MAC_CARBON
            // this is for CFM
            #define FLEXT_OSAPI FLEXT_OSAPI_MAC_CARBON
        #else
            #define FLEXT_OSAPI FLEXT_OSAPI_UNKNOWN
        #endif
    #endif
    
    // This is important for method and attribute callbacks
    #pragma enumsalwaysint on
    // This is important for everything
    #pragma bool on
thomas's avatar
thomas committed
230

231 232
#elif defined(__GNUG__) || (defined(__ICC) && (FLEXT_OS == FLEXT_OS_LINUX || defined(linux) || defined(__linux__)))

thomas's avatar
thomas committed
233
    // GNU C++
234
    // and Intel (as suggested by Tim Blechmann)
thomas's avatar
thomas committed
235

thomas's avatar
thomas committed
236
    #ifndef FLEXT_CPU
thomas's avatar
thomas committed
237
        #if defined(__x86_64__)
thomas's avatar
thomas committed
238 239 240
            #define FLEXT_CPU FLEXT_CPU_X86_64
        #elif defined(_X86_) || defined(__i386__) || defined(__i586__) || defined(__i686__)
            #define FLEXT_CPU FLEXT_CPU_IA32
thomas's avatar
thomas committed
241 242 243
        #elif defined(__ppc64__)
            #define FLEXT_CPU FLEXT_CPU_PPC64
        #elif defined(__ppc__)
thomas's avatar
thomas committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            #define FLEXT_CPU FLEXT_CPU_PPC
        #elif defined(__MIPS__)
            #define FLEXT_CPU FLEXT_CPU_MIPS
        #else
            #define FLEXT_CPU FLEXT_CPU_UNKNOWN
        #endif
    #endif

    #ifndef FLEXT_OS
        #if defined(linux) || defined(__linux__)
            #define FLEXT_OS FLEXT_OS_LINUX
        #elif defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__MINGW32__)
            #define FLEXT_OS FLEXT_OS_WIN
        #elif defined(__APPLE__) && defined(__MACH__)
            #define FLEXT_OS FLEXT_OS_MAC
        // how about IRIX??
        #else
            #define FLEXT_OS FLEXT_OS_UNKNOWN
        #endif
    #endif

    #ifndef FLEXT_OSAPI
        #if FLEXT_OS == FLEXT_OS_MAC
            #define FLEXT_OSAPI FLEXT_OSAPI_MAC_MACH
        #elif FLEXT_OS == FLEXT_OS_WIN
thomas's avatar
thomas committed
269
            #if defined(__MINGW32__)
thomas's avatar
thomas committed
270
                #define FLEXT_OSAPI FLEXT_OSAPI_WIN_NATIVE
thomas's avatar
thomas committed
271
            #else
thomas's avatar
thomas committed
272
                #define FLEXT_OSAPI FLEXT_OSAPI_WIN_POSIX
thomas's avatar
thomas committed
273
            #endif
thomas's avatar
thomas committed
274 275 276 277 278 279
        #elif FLEXT_OS == FLEXT_OS_LINUX || FLEXT_OS == FLEXT_OS_IRIX
            #define FLEXT_OSAPI FLEXT_OSAPI_UNIX_POSIX
        #else
            #define FLEXT_OSAPI FLEXT_OSAPI_UNKNOWN
        #endif
    #endif
thomas's avatar
thomas committed
280

thomas's avatar
thomas committed
281
#elif defined(__MRC__) && defined(MPW_CPLUS)
thomas's avatar
thomas committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    // Apple MPW MrCpp

    #if __MRC__ < 0x500
        #error Apple MPW MrCpp v.5.0.0 or later compiler required
    #endif

    #ifndef FLEXT_CPU
        #if defined(__POWERPC__)
            #define FLEXT_CPU FLEXT_CPU_PPC
        #else
            #define FLEXT_CPU FLEXT_CPU_UNKNOWN
        #endif
    #endif

    #ifndef FLEXT_OS
        #if defined(macintosh)
            #define FLEXT_OS FLEXT_OS_MAC
        #else
            #define FLEXT_OS FLEXT_OS_UNKNOWN
        #endif
    #endif

    #ifndef FLEXT_OSAPI
        #if FLEXT_OS == FLEXT_OS_MAC
            #define FLEXT_OSAPI FLEXT_OSAPI_MAC_CLASSIC
        #else
            #define FLEXT_OSAPI FLEXT_OSAPI_UNKNOWN
        #endif
    #endif
thomas's avatar
thomas committed
311 312
#endif

313 314


thomas's avatar
thomas committed
315
#if FLEXT_OS == FLEXT_OS_WIN
thomas's avatar
thomas committed
316 317 318 319 320 321 322 323
//  #pragma message("Compiling for Windows")

    #if FLEXT_SYS == FLEXT_SYS_MAX
//      #define WIN_VERSION 1
    #elif FLEXT_SYS == FLEXT_SYS_PD
//      #define PD
//      #define NT
    #endif
thomas's avatar
thomas committed
324
#elif FLEXT_OS == FLEXT_OS_LINUX
thomas's avatar
thomas committed
325
//  #pragma message("Compiling for Linux")
thomas's avatar
thomas committed
326

thomas's avatar
thomas committed
327 328 329 330 331
    #if FLEXT_SYS == FLEXT_SYS_PD
//      #define PD
    #else
        #error "Flext SYS/OS combination unknown"
    #endif
thomas's avatar
thomas committed
332
#elif FLEXT_OS == FLEXT_OS_IRIX
thomas's avatar
thomas committed
333
//  #pragma message("Compiling for Irix")
thomas's avatar
thomas committed
334

thomas's avatar
thomas committed
335 336 337 338 339
    #if FLEXT_SYS == FLEXT_SYS_PD
//      #define PD
    #else
        #error "Flext SYS/OS combination unknown"
    #endif
thomas's avatar
thomas committed
340
#elif FLEXT_OS == FLEXT_OS_MAC
thomas's avatar
thomas committed
341
//  #pragma message("Compiling for MacOS")
thomas's avatar
thomas committed
342

thomas's avatar
thomas committed
343 344 345
    #if FLEXT_SYS == FLEXT_SYS_PD
//      #define PD
    #endif
thomas's avatar
thomas committed
346
#else
thomas's avatar
thomas committed
347
    #error "Operating system could not be determined"
thomas's avatar
thomas committed
348 349
#endif

thomas's avatar
thomas committed
350
#if FLEXT_SYS == FLEXT_SYS_MAX
thomas's avatar
thomas committed
351
//  #pragma message("Compiling for Max/MSP")
martin hermant's avatar
martin hermant committed
352
#ifndef MSP64
Thomas Grill's avatar
Thomas Grill committed
353
#if FLEXT_CPU == FLEXT_CPU_X86_64
martin hermant's avatar
martin hermant committed
354 355 356 357 358
#define MSP64 1
#else
#define MSP64 0
#endif
#endif
thomas's avatar
thomas committed
359
#elif FLEXT_SYS == FLEXT_SYS_PD
thomas's avatar
thomas committed
360
//  #pragma message("Compiling for PD")
thomas's avatar
thomas committed
361
#endif
thomas's avatar
thomas committed
362

thomas's avatar
thomas committed
363
// ----- set threading model -----
364
// shared builds are always threaded
thomas's avatar
thomas committed
365
#ifdef FLEXT_SHARED
thomas's avatar
thomas committed
366 367
    #undef FLEXT_THREADS
    #define FLEXT_THREADS
thomas's avatar
thomas committed
368 369
#endif

thomas's avatar
thomas committed
370
#ifdef FLEXT_THREADS
thomas's avatar
thomas committed
371
    #undef FLEXT_THREADS
thomas's avatar
thomas committed
372 373
    #if FLEXT_SYS == FLEXT_SYS_MAX && FLEXT_OS == FLEXT_OS_MAC && FLEXT_OSAPI != FLEXT_OSAPI_MAC_MACH
        // Max for CFM doesn't like posix threads
thomas's avatar
thomas committed
374 375 376 377 378 379 380
        #define FLEXT_THREADS FLEXT_THR_MP      
    #elif FLEXT_SYS == FLEXT_SYS_MAX && FLEXT_OS == FLEXT_OS_WIN
        // for wmax use native Windows threads
        #define FLEXT_THREADS FLEXT_THR_WIN32
    #else
        #define FLEXT_THREADS FLEXT_THR_POSIX
    #endif
thomas's avatar
thomas committed
381 382
#endif

383
// ----- macros for class names -----
thomas's avatar
thomas committed
384 385 386
/*
        With linux (flat linker namespace) and more than one flext-based external loaded all calls to static 
        exported functions refer to the first instance loaded!
thomas's avatar
thomas committed
387
        Therefore different class names are used so that the correct type of flext function is called.
thomas's avatar
thomas committed
388
*/
thomas's avatar
thomas committed
389 390
#ifdef __DOXYGEN__
    #define FLEXT_CLASSDEF(CL) CL
391 392 393 394 395 396 397 398
#elif defined(FLEXT_DEBUG)
    #if defined(FLEXT_SHARED)
        #define FLEXT_CLASSDEF(CL) CL##_shared_d
    #elif defined(FLEXT_THREADS)
        #define FLEXT_CLASSDEF(CL) CL##_multi_d
    #else
        #define FLEXT_CLASSDEF(CL) CL##_single_d
    #endif
399
#else
400 401 402 403 404 405 406
    #if defined(FLEXT_SHARED)
        #define FLEXT_CLASSDEF(CL) CL##_shared
    #elif defined(FLEXT_THREADS)
        #define FLEXT_CLASSDEF(CL) CL##_multi
    #else
        #define FLEXT_CLASSDEF(CL) CL##_single
    #endif
407 408
#endif

thomas's avatar
thomas committed
409 410 411

/* Set the right calling convention (and exporting) for the OS */

412
#if defined(_MSC_VER)
thomas's avatar
thomas committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	#ifdef FLEXT_SHARED
        // for compiling a shared flext library FLEXT_EXPORTS must be defined
        #ifdef FLEXT_EXPORTS
		    #define FLEXT_SHARE __declspec(dllexport)
        #else
		    #define FLEXT_SHARE __declspec(dllimport)
        #endif
	#else
		#define FLEXT_SHARE
	#endif
	#define FLEXT_EXT __declspec(dllexport)
#else                   // other OS's
	#define FLEXT_SHARE
	#define FLEXT_EXT
#endif


thomas's avatar
thomas committed
430 431
// std namespace
#ifdef __MWERKS__
thomas's avatar
thomas committed
432
#	define STD std
thomas's avatar
thomas committed
433
#else
thomas's avatar
thomas committed
434
#	define STD
thomas's avatar
thomas committed
435 436
#endif

thomas's avatar
thomas committed
437
// branching hints
thomas's avatar
thomas committed
438
#if __GNUC__ >= 3
thomas's avatar
thomas committed
439 440 441 442 443 444
#	ifndef LIKELY
#		define LIKELY(expression) (__builtin_expect(!!(expression), 1))
#	endif
#	ifndef UNLIKELY
#		define UNLIKELY(expression) (__builtin_expect(!!(expression), 0))
#	endif
thomas's avatar
thomas committed
445
#else
thomas's avatar
thomas committed
446 447 448 449 450 451
#	ifndef LIKELY
#		define LIKELY(expression) (expression)
#	endif
#	ifndef UNLIKELY
#		define UNLIKELY(expression) (expression)
#	endif
thomas's avatar
thomas committed
452 453
#endif

Thomas Grill's avatar
Thomas Grill committed
454
// macro definitions for inline flext usage
455
#ifdef FLEXT_INLINE
Thomas Grill's avatar
Thomas Grill committed
456 457
#   define FLEXT_TEMPLATE template<typename flext_T>
#   define FLEXT_TEMPIMPL(fun) template<typename flext_T> fun<flext_T>
458
#   define FLEXT_TEMPINST(fun) fun<void>
Thomas Grill's avatar
Thomas Grill committed
459
#   define FLEXT_TEMPSUB(fun) typename fun<flext_T>
Thomas Grill's avatar
Thomas Grill committed
460
#   define FLEXT_TEMP_TYPENAME typename
461 462 463 464 465
#else
#   define FLEXT_TEMPLATE
#   define FLEXT_TEMPIMPL(fun) fun
#   define FLEXT_TEMPINST(fun) fun
#   define FLEXT_TEMPSUB(fun) fun
Thomas Grill's avatar
Thomas Grill committed
466
#   define FLEXT_TEMP_TYPENAME
467 468
#endif

thomas's avatar
thomas committed
469
#endif // __FLEXT_PREFIX_H