HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dassert.h
Go to the documentation of this file.
1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio
4 
5 
6 #pragma once
7 
8 #include <cassert>
9 #include <cstdio>
10 #include <cstdlib>
11 
12 #include <OpenImageIO/platform.h>
13 
14 
15 /// OIIO_ABORT_IF_DEBUG is a call to abort() for debug builds, but does
16 /// nothing for release builds.
17 #ifndef NDEBUG
18 # define OIIO_ABORT_IF_DEBUG abort()
19 #else
20 # define OIIO_ABORT_IF_DEBUG (void)0
21 #endif
22 
23 
24 /// OIIO_ASSERT(condition) checks if the condition is met, and if not,
25 /// prints an error message indicating the module and line where the error
26 /// occurred, and additionally aborts if in debug mode. When in release
27 /// mode, it prints the error message if the condition fails, but does not
28 /// abort.
29 ///
30 /// OIIO_ASSERT_MSG(condition,msg,...) lets you add formatted output (a la
31 /// printf) to the failure message.
32 #define OIIO_ASSERT(x) \
33  (OIIO_LIKELY(x) \
34  ? ((void)0) \
35  : (std::fprintf(stderr, "%s:%u: %s: Assertion '%s' failed.\n", \
36  __FILE__, __LINE__, OIIO_PRETTY_FUNCTION, #x), \
37  OIIO_ABORT_IF_DEBUG))
38 #define OIIO_ASSERT_MSG(x, msg, ...) \
39  (OIIO_LIKELY(x) \
40  ? ((void)0) \
41  : (std::fprintf(stderr, "%s:%u: %s: Assertion '%s' failed: " msg "\n", \
42  __FILE__, __LINE__, OIIO_PRETTY_FUNCTION, #x, \
43  __VA_ARGS__), \
44  OIIO_ABORT_IF_DEBUG))
45 
46 
47 /// OIIO_DASSERT and OIIO_DASSERT_MSG are the same as OIIO_ASSERT for debug
48 /// builds (test, print error, abort), but do nothing at all in release
49 /// builds (not even perform the test). This is similar to C/C++ assert(),
50 /// but gives us flexibility in improving our error messages. It is also ok
51 /// to use regular assert() for this purpose if you need to eliminate the
52 /// dependency on this header from a particular place (and don't mind that
53 /// assert won't format identically on all platforms).
54 #ifndef NDEBUG
55 # define OIIO_DASSERT OIIO_ASSERT
56 # define OIIO_DASSERT_MSG OIIO_ASSERT_MSG
57 #else
58 # define OIIO_DASSERT(x) ((void)sizeof(x)) /*NOLINT*/
59 # define OIIO_DASSERT_MSG(x, ...) ((void)sizeof(x)) /*NOLINT*/
60 #endif
61 
62 
63 /// ASSERT and ASSERT_MSG (and, ugh, ASSERTMSG) are deprecated assertion
64 /// macros. They are deprecated for two reasons: (1) terrible names pollute
65 /// the global namespace and might conflict with other packages; and (2)
66 /// the fully unconditional nature of aborting even for release builds is
67 /// unkind to applications. Consider these all to be deprecated and avoid
68 /// using these macros.
69 ///
70 /// DASSERT and DASSERT_MSG also are considered deprecated for the namespace
71 /// reasons.
72 #ifndef ASSERT
73 # define ASSERT(x) \
74  (OIIO_LIKELY(x) \
75  ? ((void)0) \
76  : (std::fprintf(stderr, "%s:%u: %s: Assertion '%s' failed.\n", \
77  __FILE__, __LINE__, OIIO_PRETTY_FUNCTION, #x), \
78  abort()))
79 #endif
80 
81 #ifndef ASSERT_MSG
82 # define ASSERT_MSG(x, msg, ...) \
83  (OIIO_LIKELY(x) \
84  ? ((void)0) \
85  : (std::fprintf(stderr, \
86  "%s:%u: %s: Assertion '%s' failed: " msg "\n", \
87  __FILE__, __LINE__, OIIO_PRETTY_FUNCTION, #x, \
88  __VA_ARGS__), \
89  abort()))
90 #endif
91 
92 #ifndef ASSERTMSG
93 # define ASSERTMSG ASSERT_MSG
94 #endif
95 
96 
97 /// DASSERT(condition) is just an alias for the usual assert() macro.
98 /// It does nothing when in a non-DEBUG (optimized, shipping) build.
99 #ifndef NDEBUG
100 # define DASSERT(x) assert(x)
101 #else
102 /* DASSERT does nothing when not debugging; sizeof trick prevents warnings */
103 # define DASSERT(x) ((void)sizeof(x)) /*NOLINT*/
104 #endif
105 
106 /// DASSERT_MSG(condition,msg,...) is just like ASSERT_MSG, except that it
107 /// only is functional in DEBUG mode, but does nothing when in a
108 /// non-DEBUG (optimized, shipping) build.
109 #ifndef NDEBUG
110 # define DASSERT_MSG ASSERT_MSG
111 #else
112 /* does nothing when not debugging */
113 # define DASSERT_MSG(x, ...) ((void)sizeof(x)) /*NOLINT*/
114 #endif
115 
116 #ifndef DASSERTMSG
117 # define DASSERTMSG DASSERT_MSG
118 #endif
119 
120 
121 /// Define OIIO_STATIC_ASSERT(cond) as a wrapper around static_assert(cond),
122 /// with appropriate fallbacks for older C++ standards.
123 #if (__cplusplus >= 201700L) /* FIXME - guess the token, fix when C++17 */
124 # define OIIO_STATIC_ASSERT(cond) static_assert(cond)
125 #else
126 # define OIIO_STATIC_ASSERT(cond) static_assert(cond, "")
127 #endif
128 
129 /// Deprecated synonym:
130 #define OIIO_STATIC_ASSERT_MSG(cond, msg) static_assert(cond, msg)