Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CinderAssert.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, The Cinder Project
3 
4  This code is intended to be used with the Cinder C++ library, http://libcinder.org
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7  the following conditions are met:
8 
9  * Redistributions of source code must retain the above copyright notice, this list of conditions and
10  the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12  the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 // Included macros:
25 // - CI_ASSERT( expr ): asserts that \a expr evaluates to true. By default it is equivalent to assert( expr )
26 // - CI_ASSERT_MSG( expr, msg ): same as CI_ASSERT but takes an additional, human readable \a const char* message parameter
27 // - CI_VERIFY( expr ): same as CI_ASSERT, but still evaluates \a expr in release mode.
28 // - CI_VERIFY_MSG( expr ): same as CI_ASSERT_MSG, but still evaluates \a expr in release mode.
29 // - CI_ASSERT_NOT_REACHABLE(): utility to place at an unreachable location in code, which will cause a failed assertion.
30 //
31 // There are some user-definable variables that you can use to customize how failed assertions are handled, though
32 // you must rebuild cinder in order to see the changes.
33 //
34 // User-definable parameters:
35 // - CI_DISABLE_ASSERTS: disables all asserts, they become no-ops (VERIFY variants still evaluate \a expr).
36 // - CI_ENABLE_ASSERT_HANDLER: if this is set, users must define assertionFailed() and assertionFailedMessage()
37 // to handle a failed assertion.
38 // - CI_ASSERT_DEBUG_BREAK: overrides default assertion behavior to break into the debugger instead of
39 // aborting. Cannot be used in conjunction with CI_ENABLE_ASSERT_HANDLER.
40 
41 #pragma once
42 
43 #if ! defined( NDEBUG ) && ! defined( CI_DISABLE_ASSERTS )
44 
45  #include "cinder/CurrentFunction.h"
46  #include <cassert>
47 
48  // defined in CinderAssert.cpp
49  namespace cinder { namespace detail {
50  void assertionFailedBreak( char const *expr, char const *function, char const *file, long line );
51  void assertionFailedMessageBreak( char const *expr, char const *msg, char const *function, char const *file, long line );
52  void assertionFailedMessageAbort( char const *expr, char const *msg, char const *function, char const *file, long line );
53  } } // namespace cinder::detail
54 
55  #if defined( CI_ASSERT_DEBUG_BREAK )
56 
57  #define CI_ASSERT( expr ) ( (expr) ? ( (void)0) : ::cinder::detail::assertionFailedBreak( #expr, CINDER_CURRENT_FUNCTION, __FILE__, __LINE__ ) )
58  #define CI_ASSERT_MSG( expr, msg ) ( (expr) ? ( (void)0) : ::cinder::detail::assertionFailedMessageBreak( #expr, msg, CINDER_CURRENT_FUNCTION, __FILE__, __LINE__ ) )
59 
60  #elif defined( CI_ENABLE_ASSERT_HANDLER )
61 
62  // User opts to define these assertion handlers
63  namespace cinder {
65  void assertionFailed( char const *expr, char const *function, char const *file, long line );
67  void assertionFailedMessage( char const *expr, char const *msg, char const *function, char const *file, long line );
68  }
69 
70  #define CI_ASSERT( expr ) ( (expr) ? ( (void)0) : ::cinder::assertionFailed( #expr, CINDER_CURRENT_FUNCTION, __FILE__, __LINE__ ) )
71  #define CI_ASSERT_MSG( expr, msg ) ( (expr) ? ( (void)0) : ::cinder::assertionFailedMessage( #expr, msg, CINDER_CURRENT_FUNCTION, __FILE__, __LINE__ ) )
72 
73  #else // defined( CI_ENABLE_ASSERT_HANDLER )
74 
75  #define CI_ASSERT( expr ) assert( expr )
76  #define CI_ASSERT_MSG( expr, msg ) ( (expr) ? ( (void)0) : ::cinder::detail::assertionFailedMessageAbort( #expr, msg, CINDER_CURRENT_FUNCTION, __FILE__, __LINE__ ) )
77 
78  #endif // defined( CI_ASSERT_DEBUG_BREAK )
79 
80  #define CI_VERIFY( expr ) CI_ASSERT( expr )
81  #define CI_VERIFY_MSG( expr, msg ) CI_ASSERT_MSG( expr, msg )
82 
83  #define CI_ASSERT_NOT_REACHABLE() CI_ASSERT_MSG( 0, "not reachable" )
84 
85 #else
86 
87  #define CI_ASSERT( expr ) ( (void)0 )
88  #define CI_ASSERT_MSG( expr, msg ) ( (void)0 )
89  #define CI_VERIFY( expr ) ( (void)(expr) )
90  #define CI_VERIFY_MSG( expr, msg ) ( (void)(expr) )
91 
92  #define CI_ASSERT_NOT_REACHABLE() ( (void)0 )
93 
94 #endif
void assertionFailedMessageBreak(char const *expr, char const *msg, char const *function, char const *file, long line)
Definition: CinderAssert.cpp:42
void assertionFailedBreak(char const *expr, char const *function, char const *file, long line)
Definition: CinderAssert.cpp:31
void assertionFailedMessageAbort(char const *expr, char const *msg, char const *function, char const *file, long line)
Definition: CinderAssert.cpp:53