HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dassert.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 #pragma once
33 
34 #include <cstdio>
35 #include <cstdlib>
36 
37 #include <OpenImageIO/platform.h>
38 
39 
40 /// \file
41 ///
42 /// Handy macros for debugging assertions.
43 ///
44 /// - ASSERT (if not already defined) is defined to check if a condition
45 /// is met, and if not, calls ABORT with an error message
46 /// indicating the module and line where it occurred.
47 /// - ASSERT_MSG: like ASSERT, but takes printf-like extra arguments
48 /// - DASSERT is the same as ASSERT when NDEBUG is not defined but a
49 /// no-op when not in debug mode.
50 /// - DASSERT_MSG: like DASSERT, but takes printf-like extra arguments
51 /// - OIIO_STATIC_ASSERT(cond) : static assertion
52 /// - OIIO_STATIC_ASSERT_MSG(cond,msg) : static assertion + message
53 ///
54 /// The presumed usage is that you want ASSERT for dire conditions that
55 /// must be checked at runtime even in an optimized build. DASSERT is
56 /// for checks we should do for debugging, but that we don't want to
57 /// bother with in a shipping optimized build.
58 ///
59 /// In both cases, these are NOT a substitute for actual error checking
60 /// and recovery! Never ASSERT or DASSERT to check invalid user input,
61 /// for example. They should be used only to verify that there aren't
62 /// errors in the *code* that are so severe that there is no point even
63 /// trying to recover gracefully.
64 
65 
66 /// ASSERT(condition) checks if the condition is met, and if not, prints
67 /// an error message indicating the module and line where the error
68 /// occurred and then aborts.
69 
70 #ifndef ASSERT
71 # define ASSERT(x) \
72  (OIIO_LIKELY(x) \
73  ? ((void)0) \
74  : (std::fprintf(stderr, "%s:%u: failed assertion '%s'\n", \
75  __FILE__, __LINE__, #x), \
76  abort()))
77 #endif
78 
79 /// ASSERT_MSG(condition,msg,...) is like ASSERT, but lets you add
80 /// formatted output (a la printf) to the failure message.
81 #ifndef ASSERT_MSG
82 # define ASSERT_MSG(x, msg, ...) \
83  (OIIO_LIKELY(x) \
84  ? ((void)0) \
85  : (std::fprintf(stderr, "%s:%u: failed assertion '%s': " msg "\n", \
86  __FILE__, __LINE__, #x, __VA_ARGS__), \
87  abort()))
88 #endif
89 
90 #ifndef ASSERTMSG
91 # define ASSERTMSG ASSERT_MSG
92 #endif
93 
94 
95 /// DASSERT(condition) is just like ASSERT, except that it only is
96 /// functional in DEBUG mode, but does nothing when in a non-DEBUG
97 /// (optimized, shipping) build.
98 #ifndef NDEBUG
99 # define DASSERT(x) ASSERT(x)
100 #else
101 /* DASSERT does nothing when not debugging; sizeof trick prevents warnings */
102 # define DASSERT(x) ((void)sizeof(x)) /*NOLINT*/
103 #endif
104 
105 /// DASSERT_MSG(condition,msg,...) is just like ASSERT_MSG, except that it
106 /// only is functional in DEBUG mode, but does nothing when in a
107 /// non-DEBUG (optimized, shipping) build.
108 #ifndef NDEBUG
109 # define DASSERT_MSG ASSERT_MSG
110 #else
111 /* does nothing when not debugging */
112 # define DASSERT_MSG(x, ...) ((void)sizeof(x)) /*NOLINT*/
113 #endif
114 
115 #ifndef DASSERTMSG
116 # define DASSERTMSG DASSERT_MSG
117 #endif
118 
119 
120 
121 /// Define OIIO_STATIC_ASSERT and OIIO_STATIC_ASSERT_MSG as wrappers around
122 /// static_assert and static_assert_msg, with appropriate fallbacks for
123 /// older C++ standards.
124 #if (__cplusplus >= 201700L) /* FIXME - guess the token, fix when C++17 */
125 # define OIIO_STATIC_ASSERT(cond) static_assert(cond)
126 # define OIIO_STATIC_ASSERT_MSG(cond, msg) static_assert(cond, msg)
127 #else /* (__cplusplus >= 201103L) */
128 # define OIIO_STATIC_ASSERT(cond) static_assert(cond, "")
129 # define OIIO_STATIC_ASSERT_MSG(cond, msg) static_assert(cond, msg)
130 #endif
Platform-related macros.