1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
27 // File: vogl_assert.h
30 #include "vogl_core.h"
32 const unsigned int VOGL_FAIL_EXCEPTION_CODE = 256U;
33 void vogl_enable_fail_exceptions(bool enabled);
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);
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
44 #define VOGL_ASSERT(_exp) (void)((!!(_exp)) || (vogl_assert(#_exp, __FILE__, __LINE__), 0))
45 #define VOGL_ASSERTS_ENABLED
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)
51 // VOGL_VERIFY() is enabled in all builds.
52 #define VOGL_VERIFY(_exp) (void)((!!(_exp)) || (vogl_assert(#_exp, __FILE__, __LINE__), 0))
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) \
58 vogl_fail(#msg, __FILE__, __LINE__); \
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))
64 void trace(const char *pFmt, va_list args);
65 void trace(const char *pFmt, ...) VOGL_ATTRIBUTE_PRINTF(1, 2);
67 // Borrowed from boost libraries.
69 struct vogl_assume_failure;
71 struct vogl_assume_failure<true>
79 struct vogl_assume_try
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__)
88 #if defined(NDEBUG) && !VOGL_ENABLE_ASSERTIONS_IN_ALL_BUILDS
90 inline T vogl_assert_range(T i, T m)
96 inline T vogl_assert_range_incl(T i, T m)
102 template <typename T>
103 inline T vogl_assert_range(T i, T m)
105 VOGL_ASSERT((i >= 0) && (i < m));
110 inline unsigned int vogl_assert_range<unsigned int>(unsigned int i, unsigned int m)
117 inline unsigned long long vogl_assert_range<unsigned long long>(unsigned long long i, unsigned long long m)
123 template <typename T>
124 inline T vogl_assert_range_incl(T i, T m)
126 VOGL_ASSERT((i >= 0) && (i <= m));
130 // These specializations are here to fix (useless) gcc compiler warnings
132 inline unsigned int vogl_assert_range_incl<uint>(unsigned int i, unsigned int m)
138 inline unsigned long long vogl_assert_range_incl<unsigned long long>(unsigned long long i, unsigned long long m)