]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_assert.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_assert.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_assert.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 const unsigned int VOGL_FAIL_EXCEPTION_CODE = 256U;
33 void vogl_enable_fail_exceptions(bool enabled);
34
35 // TODO: Add an optional message argument.
36 void vogl_assert(const char *pExp, const char *pFile, unsigned line);
37 void vogl_fail(const char *pExp, const char *pFile, unsigned line);
38
39 // Assertions are usually only enabled in debug builds.
40 #if defined(NDEBUG) && !VOGL_ENABLE_ASSERTIONS_IN_ALL_BUILDS
41 #define VOGL_ASSERT(x) ((void)0)
42 #undef VOGL_ASSERTS_ENABLED
43 #else
44 #define VOGL_ASSERT(_exp) (void)((!!(_exp)) || (vogl_assert(#_exp, __FILE__, __LINE__), 0))
45 #define VOGL_ASSERTS_ENABLED
46 #endif
47
48 // VOGL_ASSERT_ALWAYS is equivalent to VOGL_ASSERT(0) (not "always" does not imply release/optimized builds)
49 #define VOGL_ASSERT_ALWAYS VOGL_ASSERT(0)
50
51 // VOGL_VERIFY() is enabled in all builds.
52 #define VOGL_VERIFY(_exp) (void)((!!(_exp)) || (vogl_assert(#_exp, __FILE__, __LINE__), 0))
53
54 // VOGL_FAIL() is enabled in all builds and exits the process (unless vogl_enable_fail_exceptions(false) is called)
55 #define VOGL_FAIL(msg)                       \
56     do                                         \
57     {                                          \
58         vogl_fail(#msg, __FILE__, __LINE__); \
59     } while (0)
60
61 #define VOGL_ASSERT_OPEN_RANGE(x, l, h) VOGL_ASSERT((x >= l) && (x < h))
62 #define VOGL_ASSERT_CLOSED_RANGE(x, l, h) VOGL_ASSERT((x >= l) && (x <= h))
63
64 void trace(const char *pFmt, va_list args);
65 void trace(const char *pFmt, ...) VOGL_ATTRIBUTE_PRINTF(1, 2);
66
67 // Borrowed from boost libraries.
68 template <bool x>
69 struct vogl_assume_failure;
70 template <>
71 struct vogl_assume_failure<true>
72 {
73     enum
74     {
75         blah = 1
76     };
77 };
78 template <int x>
79 struct vogl_assume_try
80 {
81 };
82
83 #define VOGL_JOINER_FINAL(a, b) a##b
84 #define VOGL_JOINER(a, b) VOGL_JOINER_FINAL(a, b)
85 #define VOGL_JOIN(a, b) VOGL_JOINER(a, b)
86 #define VOGL_ASSUME(p) typedef vogl_assume_try<sizeof(vogl_assume_failure<(bool)(p)>)> VOGL_JOIN(vogl_assume_typedef, __COUNTER__)
87
88 #if defined(NDEBUG) && !VOGL_ENABLE_ASSERTIONS_IN_ALL_BUILDS
89 template <typename T>
90 inline T vogl_assert_range(T i, T m)
91 {
92     VOGL_NOTE_UNUSED(m);
93     return i;
94 }
95 template <typename T>
96 inline T vogl_assert_range_incl(T i, T m)
97 {
98     VOGL_NOTE_UNUSED(m);
99     return i;
100 }
101 #else
102 template <typename T>
103 inline T vogl_assert_range(T i, T m)
104 {
105     VOGL_ASSERT((i >= 0) && (i < m));
106     return i;
107 }
108
109 template <>
110 inline unsigned int vogl_assert_range<unsigned int>(unsigned int i, unsigned int m)
111 {
112     VOGL_ASSERT(i < m);
113     return i;
114 }
115
116 template <>
117 inline unsigned long long vogl_assert_range<unsigned long long>(unsigned long long i, unsigned long long m)
118 {
119     VOGL_ASSERT(i < m);
120     return i;
121 }
122
123 template <typename T>
124 inline T vogl_assert_range_incl(T i, T m)
125 {
126     VOGL_ASSERT((i >= 0) && (i <= m));
127     return i;
128 }
129
130 // These specializations are here to fix (useless) gcc compiler warnings
131 template <>
132 inline unsigned int vogl_assert_range_incl<uint>(unsigned int i, unsigned int m)
133 {
134     VOGL_ASSERT(i <= m);
135     return i;
136 }
137 template <>
138 inline unsigned long long vogl_assert_range_incl<unsigned long long>(unsigned long long i, unsigned long long m)
139 {
140     VOGL_ASSERT(i <= m);
141     return i;
142 }
143 #endif