ovr_sdk

view LibOVR/Src/Kernel/OVR_Types.h @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line source
1 /************************************************************************************
3 PublicHeader: OVR_Kernel.h
4 Filename : OVR_Types.h
5 Content : Standard library defines and simple types
6 Created : September 19, 2012
7 Notes :
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
13 which is provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
16 You may obtain a copy of the License at
18 http://www.oculusvr.com/licenses/LICENSE-3.2
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
26 ************************************************************************************/
28 #ifndef OVR_Types_H
29 #define OVR_Types_H
31 #include "OVR_Compiler.h"
34 // Unsupported compiler configurations
35 #if _MSC_VER == 0x1600
36 # if _MSC_FULL_VER < 160040219
37 # error "Oculus does not support VS2010 without SP1 installed: It will crash in Release mode"
38 # endif
39 #endif
42 //-----------------------------------------------------------------------------------
43 // ****** Operating system identification
44 //
45 // Try to use the most generic version of these defines as possible in order to achieve
46 // the simplest portable code. For example, instead of using #if (defined(OVR_OS_IPHONE) || defined(OVR_OS_MAC)),
47 // consider using #if defined(OVR_OS_APPLE).
48 //
49 // Type definitions exist for the following operating systems: (OVR_OS_x)
50 //
51 // WIN32 - Win32 and Win64 (Windows XP and later) Does not include Microsoft phone and console platforms, despite that Microsoft's _WIN32 may be defined by the compiler for them.
52 // WIN64 - Win64 (Windows XP and later)
53 // MAC - Mac OS X (may be defined in addition to BSD)
54 // LINUX - Linux
55 // BSD - BSD Unix
56 // ANDROID - Android (may be defined in addition to LINUX)
57 // IPHONE - iPhone
58 // MS_MOBILE - Microsoft mobile OS.
59 //
60 // Meta platforms
61 // MS - Any OS by Microsoft (e.g. Win32, Win64, phone, console)
62 // APPLE - Any OS by Apple (e.g. iOS, OS X)
63 // UNIX - Linux, BSD, Mac OS X.
64 // MOBILE - iOS, Android, Microsoft phone
65 // CONSOLE - Console platforms
66 //
68 #if (defined(__APPLE__) && (defined(__GNUC__) ||\
69 defined(__xlC__) || defined(__xlc__))) || defined(__MACOS__)
70 # if (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) || defined(__IPHONE_OS_VERSION_MIN_REQUIRED))
71 # define OVR_OS_IPHONE
72 # else
73 # define OVR_OS_DARWIN
74 # define OVR_OS_MAC
75 # define OVR_OS_BSD
76 # endif
77 #elif (defined(WIN64) || defined(_WIN64) || defined(__WIN64__))
78 # define OVR_OS_WIN64
79 # define OVR_OS_WIN32 // Defined for compatibility and because the Win64 API supports the Win32 API.
80 #elif (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))
81 # define OVR_OS_WIN32
82 #elif defined(ANDROID) || defined(__ANDROID__)
83 # define OVR_OS_ANDROID
84 # define OVR_OS_LINUX
85 #elif defined(__linux__) || defined(__linux)
86 # define OVR_OS_LINUX
87 #elif defined(_BSD_) || defined(__FreeBSD__)
88 # define OVR_OS_BSD
89 #else
90 # define OVR_OS_OTHER
91 #endif
93 #if !defined(OVR_OS_MS_MOBILE)
94 # if (defined(_M_ARM) || defined(_M_IX86) || defined(_M_AMD64)) && !defined(OVR_OS_WIN32) && !defined(OVR_OS_CONSOLE)
95 # define OVR_OS_MS_MOBILE
96 # endif
97 #endif
99 #if !defined(OVR_OS_MS)
100 # if defined(OVR_OS_WIN32) || defined(OVR_OS_WIN64) || defined(OVR_OS_MS_MOBILE)
101 # define OVR_OS_MS
102 # endif
103 #endif
105 #if !defined(OVR_OS_APPLE)
106 # if defined(OVR_OS_MAC) || defined(OVR_OS_IPHONE)
107 # define OVR_OS_APPLE
108 # endif
109 #endif
111 #if !defined(OVR_OS_UNIX)
112 # if defined(OVR_OS_ANDROID) || defined(OVR_OS_BSD) || defined(OVR_OS_LINUX) || defined(OVR_OS_MAC)
113 # define OVR_OS_UNIX
114 # endif
115 #endif
117 #if !defined(OVR_OS_MOBILE)
118 # if defined(OVR_OS_ANDROID) || defined(OVR_OS_IPHONE) || defined(OVR_OS_MS_MOBILE)
119 # define OVR_OS_MOBILE
120 # endif
121 #endif
126 //-----------------------------------------------------------------------------------
127 // ***** CPU Architecture
128 //
129 // The following CPUs are defined: (OVR_CPU_x)
130 //
131 // X86 - x86 (IA-32)
132 // X86_64 - x86_64 (amd64)
133 // PPC - PowerPC
134 // PPC64 - PowerPC64
135 // MIPS - MIPS
136 // OTHER - CPU for which no special support is present or needed
139 #if defined(__x86_64__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(_M_AMD64)
140 # define OVR_CPU_X86_64
141 # define OVR_64BIT_POINTERS
142 #elif defined(__i386__) || defined(OVR_OS_WIN32)
143 # define OVR_CPU_X86
144 #elif defined(__powerpc64__)
145 # define OVR_CPU_PPC64
146 #elif defined(__ppc__)
147 # define OVR_CPU_PPC
148 #elif defined(__mips__) || defined(__MIPSEL__)
149 # define OVR_CPU_MIPS
150 #elif defined(__arm__)
151 # define OVR_CPU_ARM
152 #else
153 # define OVR_CPU_OTHER
154 #endif
156 //-----------------------------------------------------------------------------------
157 // ***** Co-Processor Architecture
158 //
159 // The following co-processors are defined: (OVR_CPU_x)
160 //
161 // SSE - Available on all modern x86 processors.
162 // Altivec - Available on all modern ppc processors.
163 // Neon - Available on some armv7+ processors.
165 #if defined(__SSE__) || defined(_M_IX86) || defined(_M_AMD64) // _M_IX86 and _M_AMD64 are Microsoft identifiers for Intel-based platforms.
166 # define OVR_CPU_SSE
167 #endif // __SSE__
169 #if defined( __ALTIVEC__ )
170 # define OVR_CPU_ALTIVEC
171 #endif // __ALTIVEC__
173 #if defined(__ARM_NEON__)
174 # define OVR_CPU_ARM_NEON
175 #endif // __ARM_NEON__
178 //-----------------------------------------------------------------------------------
179 // ***** Compiler Warnings
181 // Disable MSVC warnings
182 #if defined(OVR_CC_MSVC)
183 # pragma warning(disable : 4127) // Inconsistent dll linkage
184 # pragma warning(disable : 4530) // Exception handling
185 # if (OVR_CC_MSVC<1300)
186 # pragma warning(disable : 4514) // Unreferenced inline function has been removed
187 # pragma warning(disable : 4710) // Function not inlined
188 # pragma warning(disable : 4714) // _force_inline not inlined
189 # pragma warning(disable : 4786) // Debug variable name longer than 255 chars
190 # endif // (OVR_CC_MSVC<1300)
191 #endif // (OVR_CC_MSVC)
195 // *** Linux Unicode - must come before Standard Includes
197 #ifdef OVR_OS_LINUX
198 // Use glibc unicode functions on linux.
199 # ifndef _GNU_SOURCE
200 # define _GNU_SOURCE
201 # endif
202 #endif
204 //-----------------------------------------------------------------------------------
205 // ***** Standard Includes
206 //
207 #include <stddef.h>
208 #include <limits.h>
209 #include <float.h>
212 // MSVC Based Memory Leak checking - for now
213 #if defined(OVR_CC_MSVC) && defined(OVR_BUILD_DEBUG)
214 # define _CRTDBG_MAP_ALLOC
215 # include <stdlib.h>
216 # include <crtdbg.h>
217 #endif
220 //-----------------------------------------------------------------------------------
221 // ***** int8_t, int16_t, etc.
223 #if defined(OVR_CC_MSVC) && (OVR_CC_VER <= 1500) // VS2008 and earlier
224 typedef signed char int8_t;
225 typedef unsigned char uint8_t;
226 typedef signed short int16_t;
227 typedef unsigned short uint16_t;
228 typedef signed int int32_t;
229 typedef unsigned int uint32_t;
230 typedef signed __int64 int64_t;
231 typedef unsigned __int64 uint64_t;
232 #else
233 #include <stdint.h>
234 #endif
237 //-----------------------------------------------------------------------------------
238 // ***** Type definitions for Common Systems
240 namespace OVR {
242 typedef char Char;
244 // Pointer-sized integer
245 typedef size_t UPInt;
246 typedef ptrdiff_t SPInt;
249 #if defined(OVR_OS_MS)
251 typedef char SByte; // 8 bit Integer (Byte)
252 typedef unsigned char UByte;
253 typedef short SInt16; // 16 bit Integer (Word)
254 typedef unsigned short UInt16;
255 typedef long SInt32; // 32 bit Integer
256 typedef unsigned long UInt32;
257 typedef __int64 SInt64; // 64 bit Integer (QWord)
258 typedef unsigned __int64 UInt64;
261 #elif defined(OVR_OS_MAC) || defined(OVR_OS_IPHONE) || defined(OVR_CC_GNU)
263 typedef int SByte __attribute__((__mode__ (__QI__)));
264 typedef unsigned int UByte __attribute__((__mode__ (__QI__)));
265 typedef int SInt16 __attribute__((__mode__ (__HI__)));
266 typedef unsigned int UInt16 __attribute__((__mode__ (__HI__)));
267 typedef int SInt32 __attribute__((__mode__ (__SI__)));
268 typedef unsigned int UInt32 __attribute__((__mode__ (__SI__)));
269 typedef int SInt64 __attribute__((__mode__ (__DI__)));
270 typedef unsigned int UInt64 __attribute__((__mode__ (__DI__)));
272 #else
274 #include <sys/types.h>
275 typedef int8_t SByte;
276 typedef uint8_t UByte;
277 typedef int16_t SInt16;
278 typedef uint16_t UInt16;
279 typedef int32_t SInt32;
280 typedef uint32_t UInt32;
281 typedef int64_t SInt64;
282 typedef uint64_t UInt64;
284 #endif
287 //osx PID is a signed int32 (already defined to pid_t in OSX framework)
288 //linux PID is a signed int32 (already defined)
289 //win32 PID is an unsigned int64
290 #ifdef OVR_OS_WIN32
291 //process ID representation
292 typedef unsigned long pid_t;
293 #endif
295 struct OVR_GUID
296 {
297 uint32_t Data1;
298 uint16_t Data2;
299 uint16_t Data3;
300 uint8_t Data4[8];
301 };
305 } // OVR
309 //-----------------------------------------------------------------------------------
310 // ****** Standard C/C++ Library
311 //
312 // Identifies which standard library is currently being used.
313 //
314 // LIBSTDCPP - GNU libstdc++, used by GCC.
315 // LIBCPP - LLVM libc++, typically used by clang and GCC.
316 // DINKUMWARE - Used by Microsoft and various non-Microsoft compilers (e.g. Sony clang).
318 #if !defined(OVR_STDLIB_LIBSTDCPP)
319 #if defined(__GLIBCXX__)
320 #define OVR_STDLIB_LIBSTDCPP 1
321 #endif
322 #endif
324 #if !defined(OVR_STDLIB_LIBCPP)
325 #if defined(__clang__)
326 #if defined(__cplusplus) && __has_include(<__config>)
327 #define OVR_STDLIB_LIBCPP 1
328 #endif
329 #endif
330 #endif
332 #if !defined(OVR_STDLIB_DINKUMWARE)
333 #if defined(_YVALS) // Dinkumware globally #defines _YVALS from the #includes above.
334 #define OVR_STDLIB_DINKUMWARE 1
335 #endif
336 #endif
339 //-----------------------------------------------------------------------------------
340 // ***** Macro Definitions
341 //
342 // We define the following:
343 //
344 // OVR_BYTE_ORDER - Defined to either OVR_LITTLE_ENDIAN or OVR_BIG_ENDIAN
345 // OVR_FORCE_INLINE - Forces inline expansion of function
346 // OVR_ASM - Assembly language prefix
347 // OVR_STR - Prefixes string with L"" if building unicode
348 //
349 // OVR_STDCALL - Use stdcall calling convention (Pascal arg order)
350 // OVR_CDECL - Use cdecl calling convention (C argument order)
351 // OVR_FASTCALL - Use fastcall calling convention (registers)
352 //
354 // Byte order constants, OVR_BYTE_ORDER is defined to be one of these.
355 #define OVR_LITTLE_ENDIAN 1
356 #define OVR_BIG_ENDIAN 2
359 #if defined(OVR_OS_MS)
361 // ***** Windows and non-desktop platforms
363 // Byte order
364 #define OVR_BYTE_ORDER OVR_LITTLE_ENDIAN
366 // Calling convention - goes after function return type but before function name
367 #ifdef __cplusplus_cli
368 # define OVR_FASTCALL __stdcall
369 #else
370 # define OVR_FASTCALL __fastcall
371 #endif
373 #define OVR_STDCALL __stdcall
374 #define OVR_CDECL __cdecl
377 // Assembly macros
378 #if defined(OVR_CC_MSVC)
379 # define OVR_ASM _asm
380 #else
381 # define OVR_ASM asm
382 #endif // (OVR_CC_MSVC)
384 #ifdef UNICODE
385 # define OVR_STR(str) L##str
386 #else
387 # define OVR_STR(str) str
388 #endif // UNICODE
390 #else
392 // **** Standard systems
394 #if (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN))|| \
395 (defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN))
396 # define OVR_BYTE_ORDER OVR_BIG_ENDIAN
397 #elif (defined(__ARMEB__) || defined(OVR_CPU_PPC) || defined(OVR_CPU_PPC64))
398 # define OVR_BYTE_ORDER OVR_BIG_ENDIAN
399 #else
400 # define OVR_BYTE_ORDER OVR_LITTLE_ENDIAN
401 #endif
403 // Assembly macros
404 #define OVR_ASM __asm__
405 #define OVR_ASM_PROC(procname) OVR_ASM
406 #define OVR_ASM_END OVR_ASM
408 // Calling convention - goes after function return type but before function name
409 #define OVR_FASTCALL
410 #define OVR_STDCALL
411 #define OVR_CDECL
413 #endif // defined(OVR_OS_WIN32)
416 //-----------------------------------------------------------------------------------
417 // ***** OVR_PTR_SIZE
418 //
419 // Specifies the byte size of pointers (same as sizeof void*).
421 #if !defined(OVR_PTR_SIZE)
422 #if defined(__WORDSIZE)
423 #define OVR_PTR_SIZE ((__WORDSIZE) / 8)
424 #elif defined(_WIN64) || defined(__LP64__) || defined(_LP64) || defined(_M_IA64) || defined(__ia64__) || defined(__arch64__) || defined(__64BIT__) || defined(__Ptr_Is_64)
425 #define OVR_PTR_SIZE 8
426 #elif defined(__CC_ARM) && (__sizeof_ptr == 8)
427 #define OVR_PTR_SIZE 8
428 #else
429 #define OVR_PTR_SIZE 4
430 #endif
431 #endif
434 //-----------------------------------------------------------------------------------
435 // ***** OVR_WORD_SIZE
436 //
437 // Specifies the byte size of a machine word/register. Not necessarily the same as
438 // the size of pointers, but usually >= the size of pointers.
440 #if !defined(OVR_WORD_SIZE)
441 #define OVR_WORD_SIZE OVR_PTR_SIZE // For our currently supported platforms these are equal.
442 #endif
445 // ------------------------------------------------------------------------
446 // ***** OVR_FORCE_INLINE
447 //
448 // Force inline substitute - goes before function declaration
449 // Example usage:
450 // OVR_FORCE_INLINE void Test();
452 #if !defined(OVR_FORCE_INLINE)
453 #if defined(OVR_CC_MSVC)
454 #define OVR_FORCE_INLINE __forceinline
455 #elif defined(OVR_CC_GNU)
456 #define OVR_FORCE_INLINE __attribute__((always_inline)) inline
457 #else
458 #define OVR_FORCE_INLINE inline
459 #endif // OVR_CC_MSVC
460 #endif
463 // ------------------------------------------------------------------------
464 // ***** OVR_NO_INLINE
465 //
466 // Cannot be used with inline or OVR_FORCE_INLINE.
467 // Example usage:
468 // OVR_NO_INLINE void Test();
470 #if !defined(OVR_NO_INLINE)
471 #if defined(OVR_CC_MSVC) && (_MSC_VER >= 1500) // VS2008+
472 #define OVR_NO_INLINE __declspec(noinline)
473 #elif !defined(OVR_CC_MSVC)
474 #define OVR_NO_INLINE __attribute__((noinline))
475 #endif
476 #endif
479 // -----------------------------------------------------------------------------------
480 // ***** OVR_STRINGIZE
481 //
482 // Converts a preprocessor symbol to a string.
483 //
484 // Example usage:
485 // printf("Line: %s", OVR_STRINGIZE(__LINE__));
486 //
487 #if !defined(OVR_STRINGIFY)
488 #define OVR_STRINGIZEIMPL(x) #x
489 #define OVR_STRINGIZE(x) OVR_STRINGIZEIMPL(x)
490 #endif
493 // -----------------------------------------------------------------------------------
494 // ***** OVR_JOIN
495 //
496 // Joins two preprocessing symbols together. Supports the case when either or the
497 // the symbols are macros themselves.
498 //
499 // Example usage:
500 // char OVR_JOIN(unique_, __LINE__); // Results in (e.g.) char unique_123;
501 //
502 #if !defined(OVR_JOIN)
503 #define OVR_JOIN(a, b) OVR_JOIN1(a, b)
504 #define OVR_JOIN1(a, b) OVR_JOIN2(a, b)
505 #define OVR_JOIN2(a, b) a##b
506 #endif
509 //-----------------------------------------------------------------------------------
510 // ***** OVR_OFFSETOF
511 //
512 // Portable implementation of offsetof for structs and classes. offsetof and GCC's
513 // __builtin_offsetof work only with POD types (standard-layout types under C++11),
514 // despite that it can safely work with a number of types that aren't POD. This
515 // version works with more types without generating compiler warnings or errors.
516 // Returns the offset as a size_t, as per offsetof.
517 //
518 // Example usage:
519 // struct Test{ int i; float f; };
520 // size_t fPos = OVR_OFFSETOF(Test, f);
522 #if defined(OVR_CC_GNU)
523 #define OVR_OFFSETOF(class_, member_) ((size_t)(((uintptr_t)&reinterpret_cast<const volatile char&>((((class_*)65536)->member_))) - 65536))
524 #else
525 #define OVR_OFFSETOF(class_, member_) offsetof(class_, member_)
526 #endif
529 //-----------------------------------------------------------------------------------
530 // ***** OVR_SIZEOF_MEMBER
531 //
532 // Implements a portable way to determine the size of struct or class data member.
533 // C++11 allows this directly via sizeof (see OVR_CPP_NO_EXTENDED_SIZEOF), and this
534 // macro exists to handle pre-C++11 compilers.
535 // Returns the offset as a size_t, as per sizeof.
536 //
537 // Example usage:
538 // struct Test{ int i; float f; };
539 // size_t fSize = OVR_SIZEOF_MEMBER(Test, f);
540 //
541 #if defined(OVR_CPP_NO_EXTENDED_SIZEOF)
542 #define OVR_SIZEOF_MEMBER(class_, member_) (sizeof(((class_*)0)->member_))
543 #else
544 #define OVR_SIZEOF_MEMBER(class_, member_) (sizeof(class_::member_))
545 #endif
548 //-----------------------------------------------------------------------------------
549 // ***** OVR_DEBUG_BREAK, OVR_DEBUG_CODE,
550 // OVR_ASSERT, OVR_ASSERT_M, OVR_ASSERT_AND_UNUSED
551 //
552 // Macros have effect only in debug builds.
553 //
554 // Example OVR_DEBUG_BREAK usage (note the lack of parentheses):
555 // #define MY_ASSERT(expression) do { if (!(expression)) { OVR_DEBUG_BREAK; } } while(0)
556 //
557 // Example OVR_DEBUG_CODE usage:
558 // OVR_DEBUG_CODE(printf("debug test\n");)
559 // or
560 // OVR_DEBUG_CODE(printf("debug test\n"));
561 //
562 // Example OVR_ASSERT usage:
563 // OVR_ASSERT(count < 100);
564 // OVR_ASSERT_M(count < 100, "count is too high");
565 //
566 #if defined(OVR_BUILD_DEBUG)
567 // Causes a debugger breakpoint in debug builds. Has no effect in release builds.
568 // Microsoft Win32 specific debugging support
569 #if defined(OVR_CC_MSVC)
570 #define OVR_DEBUG_BREAK __debugbreak()
571 #elif defined(OVR_CC_GNU) || defined(OVR_CC_CLANG)
572 #if defined(OVR_CPU_X86) || defined(OVR_CPU_X86_64)
573 #define OVR_DEBUG_BREAK do { OVR_ASM("int $3\n\t"); } while(0)
574 #else
575 #define OVR_DEBUG_BREAK __builtin_trap()
576 #endif
577 #else
578 #define OVR_DEBUG_BREAK do { *((int *) 0) = 1; } while(0)
579 #endif
581 // The expresssion is defined only in debug builds. It is defined away in release builds.
582 #define OVR_DEBUG_CODE(c) c
584 // In debug builds this tests the given expression; if false then executes OVR_DEBUG_BREAK,
585 // if true then no action. Has no effect in release builds.
586 #if defined(__clang_analyzer__) // During static analysis, make it so the analyzer thinks that failed asserts result in program exit. Reduced false positives.
587 #include <stdlib.h>
588 #define OVR_ASSERT_M(p, message) do { if (!(p)) { OVR_DEBUG_BREAK; exit(0); } } while(0)
589 #define OVR_ASSERT(p) do { if (!(p)) { OVR_DEBUG_BREAK; exit(0); } } while(0)
590 #else
591 // void OVR_ASSERT_M(bool expression, const char message);
592 // Note: The expresion below is expanded into all usage of this assertion macro.
593 // We should try to minimize the size of the expanded code to the extent possible.
594 #define OVR_ASSERT_M(p, message) do \
595 { \
596 if (!(p)) \
597 { \
598 intptr_t ovrAssertUserParam; \
599 OVRAssertionHandler ovrAssertUserHandler = OVR::GetAssertionHandler(&ovrAssertUserParam); \
600 \
601 if(ovrAssertUserHandler && !OVRIsDebuggerPresent()) \
602 { \
603 ovrAssertUserHandler(ovrAssertUserParam, "Assertion failure", message); \
604 } \
605 else \
606 { \
607 OVR_DEBUG_BREAK; \
608 } \
609 } \
610 } while(0)
612 // void OVR_ASSERT(bool expression);
613 #define OVR_ASSERT(p) OVR_ASSERT_M((p), (#p))
614 #endif
616 // Acts the same as OVR_ASSERT in debug builds. Acts the same as OVR_UNUSED in release builds.
617 // Example usage: OVR_ASSERT_AND_UNUSED(x < 30, x);
618 #define OVR_ASSERT_AND_UNUSED(expression, value) OVR_ASSERT(expression); OVR_UNUSED(value)
620 #else
622 // The expresssion is defined only in debug builds. It is defined away in release builds.
623 #define OVR_DEBUG_CODE(c)
625 // Causes a debugger breakpoint in debug builds. Has no effect in release builds.
626 #define OVR_DEBUG_BREAK ((void)0)
628 // In debug builds this tests the given expression; if false then executes OVR_DEBUG_BREAK,
629 // if true then no action. Has no effect in release builds.
630 #define OVR_ASSERT(p) ((void)0)
631 #define OVR_ASSERT_M(p, m) ((void)0)
633 // Acts the same as OVR_ASSERT in debug builds. Acts the same as OVR_UNUSED in release builds.
634 // Example usage: OVR_ASSERT_AND_UNUSED(x < 30, x);
635 #define OVR_ASSERT_AND_UNUSED(expression, value) OVR_UNUSED(value)
637 #endif // OVR_BUILD_DEBUG
641 // Assert handler
642 // The user of this library can override the default assertion handler and provide their own.
643 namespace OVR
644 {
645 // The return value meaning is reserved for future definition and currently has no effect.
646 typedef intptr_t (*OVRAssertionHandler)(intptr_t userParameter, const char* title, const char* message);
648 // Returns the current assertion handler.
649 OVRAssertionHandler GetAssertionHandler(intptr_t* userParameter = NULL);
651 // Sets the current assertion handler.
652 // The default assertion handler if none is set simply issues a debug break.
653 // Example usage:
654 // intptr_t CustomAssertionHandler(intptr_t /*userParameter*/, const char* title, const char* message)) {
655 // MessageBox(title, message);
656 // OVR_DEBUG_BREAK;
657 // }
658 void SetAssertionHandler(OVRAssertionHandler assertionHandler, intptr_t userParameter = 0);
660 // Implements the default assertion handler.
661 intptr_t DefaultAssertionHandler(intptr_t userParameter, const char* title, const char* message);
663 // Currently defined in OVR_DebugHelp.cpp
664 bool OVRIsDebuggerPresent();
665 }
668 // ------------------------------------------------------------------------
669 // ***** static_assert
670 //
671 // Portable support for C++11 static_assert.
672 // Acts as if the following were declared:
673 // void static_assert(bool const_expression, const char* msg);
674 //
675 // Example usage:
676 // static_assert(sizeof(int32_t) == 4, "int32_t expected to be 4 bytes.");
678 #if defined(OVR_CPP_NO_STATIC_ASSERT) // If the compiler doesn't provide it intrinsically...
679 #if !defined(OVR_SA_UNUSED)
680 #if defined(OVR_CC_GNU) || defined(OVR_CC_CLANG)
681 #define OVR_SA_UNUSED __attribute__((unused))
682 #else
683 #define OVR_SA_UNUSED
684 #endif
685 #define OVR_SA_PASTE(a,b) a##b
686 #define OVR_SA_HELP(a,b) OVR_SA_PASTE(a,b)
687 #endif
689 #if defined(__COUNTER__)
690 #define static_assert(expression, msg) typedef char OVR_SA_HELP(compileTimeAssert, __COUNTER__) [((expression) != 0) ? 1 : -1] OVR_SA_UNUSED
691 #else
692 #define static_assert(expression, msg) typedef char OVR_SA_HELP(compileTimeAssert, __LINE__) [((expression) != 0) ? 1 : -1] OVR_SA_UNUSED
693 #endif
694 #endif
697 // ------------------------------------------------------------------------
698 // ***** OVR_COMPILER_ASSERT
699 //
700 // Compile-time assert; produces compiler error if condition is false.
701 // The expression must be a compile-time constant expression.
702 // This macro is deprecated in favor of static_assert, which provides better
703 // compiler output and works in a broader range of contexts.
704 //
705 // Example usage:
706 // OVR_COMPILER_ASSERT(sizeof(int32_t == 4));
708 #if !defined(OVR_COMPILER_ASSERT)
709 #define OVR_COMPILER_ASSERT(expression) static_assert(expression, #expression)
710 #define OVR_COMPILER_ASSERT_M(expression, msg) static_assert(expression, msg)
711 #endif
714 // ***** OVR_PROCESSOR_PAUSE
715 //
716 // Yields the processor for other hyperthreads, usually for the purpose of implementing spins and spin locks.
717 //
718 // Example usage:
719 // while(!finished())
720 // OVR_PROCESSOR_PAUSE();
722 #if defined(OVR_CPU_X86) || defined(OVR_CPU_X86_64)
723 #if defined(OVR_CC_GNU) || defined(OVR_CC_CLANG)
724 #define OVR_PROCESSOR_PAUSE() asm volatile("pause" ::: "memory") // Consumes 38-40 clocks on current Intel x86 and x64 hardware.
725 #elif defined(OVR_CC_MSVC)
726 #include <emmintrin.h>
727 #pragma intrinsic(_mm_pause) // Maps to asm pause.
728 #define OVR_PROCESSOR_PAUSE _mm_pause
729 #else
730 #define OVR_PROCESSOR_PAUSE()
731 #endif
732 #else
733 #define OVR_PROCESSOR_PAUSE()
734 #endif
737 // ------------------------------------------------------------------------
738 // ***** OVR_ARRAY_COUNT
739 //
740 // Returns the element count of a C array.
741 //
742 // Example usage:
743 // float itemArray[16];
744 // for(size_t i = 0; i < OVR_ARRAY_COUNT(itemArray); i++) { ... }
746 #if defined(OVR_CPP_NO_CONSTEXPR)
747 #ifndef OVR_ARRAY_COUNT
748 #define OVR_ARRAY_COUNT(x) (sizeof(x) / sizeof(x[0]))
749 #endif
750 #else
751 // Smarter C++11 version which knows the difference between arrays and pointers.
752 template <typename T, size_t N>
753 char (&OVRArrayCountHelper(T (&x)[N]))[N];
754 #define OVR_ARRAY_COUNT(x) (sizeof(OVRArrayCountHelper(x)))
755 #endif
758 // ------------------------------------------------------------------------
759 // ***** OVR_CURRENT_FUNCTION
760 //
761 // Portable wrapper for __PRETTY_FUNCTION__, C99 __func__, __FUNCTION__.
762 // This represents the most expressive version available.
763 // Acts as if the following were declared:
764 // static const char OVR_CURRENT_FUNCTION[] = "function-name";
765 //
766 // Example usage:
767 // void Test() { printf("%s", OVR_CURRENT_FUNCTION); }
769 #if defined(OVR_CC_GNU) || defined(OVR_CC_CLANG) || (defined(__ICC) && (__ICC >= 600)) // GCC, clang, Intel
770 #define OVR_CURRENT_FUNCTION __PRETTY_FUNCTION__
771 #elif defined(__FUNCSIG__) // VC++
772 #define OVR_CURRENT_FUNCTION __FUNCSIG__
773 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901) // C99 compilers
774 #define OVR_CURRENT_FUNCTION __func__
775 #else
776 #define OVR_CURRENT_FUNCTION __FUNCTION__
777 #endif
780 //-----------------------------------------------------------------------------------
781 // ***** OVR_DEPRECATED / OVR_DEPRECATED_MSG
782 //
783 // Portably annotates a function or struct as deprecated.
784 // Note that clang supports __deprecated_enum_msg, which may be useful to support.
785 //
786 // Example usage:
787 // OVR_DEPRECATED void Test(); // Use on the function declaration, as opposed to definition.
788 //
789 // struct OVR_DEPRECATED Test{ ... };
790 //
791 // OVR_DEPRECATED_MSG("Test is deprecated")
792 // void Test();
794 #if !defined(OVR_DEPRECATED)
795 #if defined(OVR_CC_MSVC) && (OVR_CC_VERSION > 1400) // VS2005+
796 #define OVR_DEPRECATED __declspec(deprecated)
797 #define OVR_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
798 #elif defined(OVR_CC_CLANG) && OVR_CC_HAS_FEATURE(attribute_deprecated_with_message)
799 #define OVR_DEPRECATED __declspec(deprecated)
800 #define OVR_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
801 #elif defined(OVR_CC_GNU) && (OVR_CC_VERSION >= 405)
802 #define OVR_DEPRECATED __declspec(deprecated)
803 #define OVR_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
804 #elif !defined(OVR_CC_MSVC)
805 #define OVR_DEPRECATED __attribute__((deprecated))
806 #define OVR_DEPRECATED_MSG(msg) __attribute__((deprecated))
807 #else
808 #define OVR_DEPRECATED
809 #define OVR_DEPRECATED_MSG(msg)
810 #endif
811 #endif
814 //-----------------------------------------------------------------------------------
815 // ***** OVR_UNUSED - Unused Argument handling
816 // Macro to quiet compiler warnings about unused parameters/variables.
817 //
818 // Example usage:
819 // void Test() {
820 // int x = SomeFunction();
821 // OVR_UNUSED(x);
822 // }
823 //
825 #if defined(OVR_CC_GNU)
826 # define OVR_UNUSED(a) do {__typeof__ (&a) __attribute__ ((unused)) __tmp = &a; } while(0)
827 #else
828 # define OVR_UNUSED(a) (a)
829 #endif
831 #define OVR_UNUSED1(a1) OVR_UNUSED(a1)
832 #define OVR_UNUSED2(a1,a2) OVR_UNUSED(a1); OVR_UNUSED(a2)
833 #define OVR_UNUSED3(a1,a2,a3) OVR_UNUSED2(a1,a2); OVR_UNUSED(a3)
834 #define OVR_UNUSED4(a1,a2,a3,a4) OVR_UNUSED3(a1,a2,a3); OVR_UNUSED(a4)
835 #define OVR_UNUSED5(a1,a2,a3,a4,a5) OVR_UNUSED4(a1,a2,a3,a4); OVR_UNUSED(a5)
836 #define OVR_UNUSED6(a1,a2,a3,a4,a5,a6) OVR_UNUSED4(a1,a2,a3,a4); OVR_UNUSED2(a5,a6)
837 #define OVR_UNUSED7(a1,a2,a3,a4,a5,a6,a7) OVR_UNUSED4(a1,a2,a3,a4); OVR_UNUSED3(a5,a6,a7)
838 #define OVR_UNUSED8(a1,a2,a3,a4,a5,a6,a7,a8) OVR_UNUSED4(a1,a2,a3,a4); OVR_UNUSED4(a5,a6,a7,a8)
839 #define OVR_UNUSED9(a1,a2,a3,a4,a5,a6,a7,a8,a9) OVR_UNUSED4(a1,a2,a3,a4); OVR_UNUSED5(a5,a6,a7,a8,a9)
842 //-----------------------------------------------------------------------------------
843 // ***** Configuration Macros
844 //
845 // Expands to the current build type as a const char string literal.
846 // Acts as the following declaration: const char OVR_BUILD_STRING[];
848 #ifdef OVR_BUILD_DEBUG
849 # define OVR_BUILD_STRING "Debug"
850 #else
851 # define OVR_BUILD_STRING "Release"
852 #endif
855 //// Enables SF Debugging information
856 //# define OVR_BUILD_DEBUG
858 // OVR_DEBUG_STATEMENT injects a statement only in debug builds.
859 // OVR_DEBUG_SELECT injects first argument in debug builds, second argument otherwise.
860 #ifdef OVR_BUILD_DEBUG
861 #define OVR_DEBUG_STATEMENT(s) s
862 #define OVR_DEBUG_SELECT(d, nd) d
863 #else
864 #define OVR_DEBUG_STATEMENT(s)
865 #define OVR_DEBUG_SELECT(d, nd) nd
866 #endif
869 #define OVR_ENABLE_THREADS
870 //
871 // Prevents OVR from defining new within
872 // type macros, so developers can override
873 // new using the #define new new(...) trick
874 // - used with OVR_DEFINE_NEW macro
875 //# define OVR_BUILD_DEFINE_NEW
876 //
879 //-----------------------------------------------------------------------------------
880 // ***** Find normal allocations
881 //
882 // Our allocations are all supposed to go through the OVR System Allocator, so that
883 // they can be run through a game's own preferred allocator. Occasionally we will
884 // accidentally introduce new code that doesn't adhere to this contract. And it
885 // then becomes difficult to track down these normal allocations. This piece of
886 // code makes it easy to check for normal allocations by asserting whenever they
887 // happen in our code.
889 //#define OVR_FIND_NORMAL_ALLOCATIONS
890 #ifdef OVR_FIND_NORMAL_ALLOCATIONS
892 inline void* operator new (size_t size, const char* filename, int line)
893 {
894 void* ptr = new char[size];
895 OVR_ASSERT(false);
896 return ptr;
897 }
899 #define new new(__FILE__, __LINE__)
901 #endif // OVR_FIND_NORMAL_ALLOCATIONS
904 #include "OVR_Nullptr.h"
909 #endif // OVR_Types_h