]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_platform.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_platform.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_platform.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 bool vogl_is_debugger_present(bool force_check = false);
33 void vogl_debug_break(void);
34 void vogl_debug_break_if_debugging(void);
35 void vogl_output_debug_string(const char *p);
36
37 // actually in vogl_assert.cpp
38 void vogl_assert(const char *pExp, const char *pFile, unsigned line);
39 void vogl_fail(const char *pExp, const char *pFile, unsigned line);
40
41 #if VOGL_LITTLE_ENDIAN_CPU
42 const bool c_vogl_little_endian_platform = true;
43 #else
44 const bool c_vogl_little_endian_platform = false;
45 #endif
46
47 const bool c_vogl_big_endian_platform = !c_vogl_little_endian_platform;
48
49 #ifdef __GNUC__
50 #define vogl_fopen(f, m) fopen64(f, m)
51 #define vogl_fopen_s(pDstFile, f, m) *(pDstFile) = fopen64(f, m)
52 #define vogl_fseek fseeko64
53 #define vogl_ftell ftello64
54 #elif defined(_MSC_VER)
55 inline FILE *vogl_fopen(const char *pFilename, const char *pMode)
56 {
57     FILE *pFile = NULL;
58     fopen_s(&pFile, pFilename, pMode);
59     return pFile;
60 }
61 #define vogl_fopen_s(pDstFile, f, m) fopen_s(pDstFile, f, m)
62 #define vogl_fseek _fseeki64
63 #define vogl_ftell _ftelli64
64 #else
65 #define vogl_fopen(f, m) fopen(f, m)
66 #define vogl_fopen_s(pDstFile, f, m) *(pDstFile) = fopen(f, m)
67 #define vogl_fseek(s, o, w) fseek(s, static_cast<long>(o), w)
68 #define vogl_ftell ftell
69 #endif
70
71 #define vogl_fread fread
72 #define vogl_fwrite fwrite
73 #define vogl_fputc fputc
74 #define vogl_fgetc fgetc
75 #define vogl_fputs fputs
76 #define vogl_fgets fgets
77 #define vogl_fprintf fprintf
78 #define vogl_feof feof
79 #define vogl_fflush fflush
80 #define vogl_fclose fclose
81
82 #ifdef VOGL_USE_WIN32_API
83 #define VOGL_BREAKPOINT DebugBreak();
84 #define VOGL_BUILTIN_EXPECT(c, v) c
85 #elif defined(__GNUC__)
86 #define VOGL_BREAKPOINT asm("int $3");
87 #define VOGL_BUILTIN_EXPECT(c, v) __builtin_expect(c, v)
88 #else
89 #define VOGL_BREAKPOINT
90 #define VOGL_BUILTIN_EXPECT(c, v) c
91 #endif
92
93 #if defined(__GNUC__)
94 #define VOGL_ALIGNED(x) __attribute__((aligned(x)))
95 #define VOGL_ALIGNED_BEGIN(x)
96 #define VOGL_ALIGNED_END(x) __attribute__((aligned(x)))
97 #define VOGL_NOINLINE __attribute__((noinline))
98 #elif defined(_MSC_VER)
99 #define VOGL_ALIGNED(x) __declspec(align(x))
100 #define VOGL_ALIGNED_BEGIN(x) __declspec(align(x))
101 #define VOGL_ALIGNED_END(x)
102 #define VOGL_NOINLINE __declspec(noinline)
103 #else
104 #define VOGL_ALIGNED(x)
105 #define VOGL_NOINLINE
106 #endif
107
108 #define VOGL_GET_ALIGNMENT(v) ((!sizeof(v)) ? 1 : (__alignof(v) ? __alignof(v) : sizeof(uint32)))
109
110 inline bool vogl_is_little_endian()
111 {
112     return c_vogl_little_endian_platform;
113 }
114 inline bool vogl_is_big_endian()
115 {
116     return c_vogl_big_endian_platform;
117 }
118
119 inline bool vogl_is_pc()
120 {
121 #ifdef VOGL_PLATFORM_PC
122     return true;
123 #else
124     return false;
125 #endif
126 }
127
128 inline bool vogl_is_x86()
129 {
130 #ifdef VOGL_PLATFORM_PC_X86
131     return true;
132 #else
133     return false;
134 #endif
135 }
136
137 inline bool vogl_is_x64()
138 {
139 #ifdef VOGL_PLATFORM_PC_X64
140     return true;
141 #else
142     return false;
143 #endif
144 }
145
146 inline bool vogl_is_debug_build()
147 {
148 #ifdef VOGL_BUILD_DEBUG
149     return true;
150 #else
151     return false;
152 #endif
153 }
154
155 typedef void (*vogl_exception_callback_t)();
156 vogl_exception_callback_t vogl_set_exception_callback(vogl_exception_callback_t callback);
157 void vogl_reset_exception_callback();
158
159 namespace vogl
160 {
161     class dynamic_string;
162     dynamic_string demangle(const char *pMangled);
163 } // namespace vogl
164
165 #ifdef __GNUC__
166 // FIXME: Is there a better way on GCC to do this?
167 // Obviously this isn't fast, it's intended for warning/error messages and such.
168 #define VOGL_METHOD_NAME vogl::dynamic_string(vogl::cVarArg, "%s::%s (%u)", vogl::demangle(vogl::type_name(*this)).get_ptr(), VOGL_FUNCTION_NAME, __LINE__).get_ptr()
169 #else
170 #define VOGL_METHOD_NAME VOGL_FUNCTION_NAME
171 #endif