Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 1 | // |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 2 | // ndnboost/assert.hpp - NDNBOOST_ASSERT(expr) |
| 3 | // NDNBOOST_ASSERT_MSG(expr, msg) |
| 4 | // NDNBOOST_VERIFY(expr) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 5 | // |
| 6 | // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. |
| 7 | // Copyright (c) 2007 Peter Dimov |
| 8 | // Copyright (c) Beman Dawes 2011 |
| 9 | // |
| 10 | // Distributed under the Boost Software License, Version 1.0. (See |
| 11 | // accompanying file LICENSE_1_0.txt or copy at |
| 12 | // http://www.boost.org/LICENSE_1_0.txt) |
| 13 | // |
| 14 | // Note: There are no include guards. This is intentional. |
| 15 | // |
| 16 | // See http://www.boost.org/libs/utility/assert.html for documentation. |
| 17 | // |
| 18 | |
| 19 | // |
| 20 | // Stop inspect complaining about use of 'assert': |
| 21 | // |
| 22 | // boostinspect:naassert_macro |
| 23 | // |
| 24 | |
| 25 | //--------------------------------------------------------------------------------------// |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 26 | // NDNBOOST_ASSERT // |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 27 | //--------------------------------------------------------------------------------------// |
| 28 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 29 | #undef NDNBOOST_ASSERT |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 31 | #if defined(NDNBOOST_DISABLE_ASSERTS) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 32 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 33 | # define NDNBOOST_ASSERT(expr) ((void)0) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 35 | #elif defined(NDNBOOST_ENABLE_ASSERT_HANDLER) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 37 | #include <ndnboost/current_function.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ndnboost |
| 40 | { |
| 41 | void assertion_failed(char const * expr, |
| 42 | char const * function, char const * file, long line); // user defined |
| 43 | } // namespace ndnboost |
| 44 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 45 | #define NDNBOOST_ASSERT(expr) ((expr) \ |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 46 | ? ((void)0) \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 47 | : ::ndnboost::assertion_failed(#expr, NDNBOOST_CURRENT_FUNCTION, __FILE__, __LINE__)) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 48 | |
| 49 | #else |
| 50 | # include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 51 | # define NDNBOOST_ASSERT(expr) assert(expr) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | //--------------------------------------------------------------------------------------// |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 55 | // NDNBOOST_ASSERT_MSG // |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 56 | //--------------------------------------------------------------------------------------// |
| 57 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 58 | # undef NDNBOOST_ASSERT_MSG |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 60 | #if defined(NDNBOOST_DISABLE_ASSERTS) || defined(NDEBUG) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 62 | #define NDNBOOST_ASSERT_MSG(expr, msg) ((void)0) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 64 | #elif defined(NDNBOOST_ENABLE_ASSERT_HANDLER) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 65 | |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 66 | #include <ndnboost/current_function.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 67 | |
| 68 | namespace ndnboost |
| 69 | { |
| 70 | void assertion_failed_msg(char const * expr, char const * msg, |
| 71 | char const * function, char const * file, long line); // user defined |
| 72 | } // namespace ndnboost |
| 73 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 74 | #define NDNBOOST_ASSERT_MSG(expr, msg) ((expr) \ |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 75 | ? ((void)0) \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 76 | : ::ndnboost::assertion_failed_msg(#expr, msg, NDNBOOST_CURRENT_FUNCTION, __FILE__, __LINE__)) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 77 | |
| 78 | #else |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 79 | #ifndef NDNBOOST_ASSERT_HPP |
| 80 | #define NDNBOOST_ASSERT_HPP |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 81 | #include <cstdlib> |
| 82 | #include <iostream> |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 83 | #include <ndnboost/current_function.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 84 | |
| 85 | // IDE's like Visual Studio perform better if output goes to std::cout or |
| 86 | // some other stream, so allow user to configure output stream: |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 87 | #ifndef NDNBOOST_ASSERT_MSG_OSTREAM |
| 88 | # define NDNBOOST_ASSERT_MSG_OSTREAM std::cerr |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 89 | #endif |
| 90 | |
| 91 | namespace ndnboost |
| 92 | { |
| 93 | namespace assertion |
| 94 | { |
| 95 | namespace detail |
| 96 | { |
| 97 | inline void assertion_failed_msg(char const * expr, char const * msg, char const * function, |
| 98 | char const * file, long line) |
| 99 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 100 | NDNBOOST_ASSERT_MSG_OSTREAM |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 101 | << "***** Internal Program Error - assertion (" << expr << ") failed in " |
| 102 | << function << ":\n" |
| 103 | << file << '(' << line << "): " << msg << std::endl; |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 104 | #ifdef UNDER_CE |
| 105 | // The Windows CE CRT library does not have abort() so use exit(-1) instead. |
| 106 | std::exit(-1); |
| 107 | #else |
| 108 | std::abort(); |
| 109 | #endif |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 110 | } |
| 111 | } // detail |
| 112 | } // assertion |
| 113 | } // detail |
| 114 | #endif |
| 115 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 116 | #define NDNBOOST_ASSERT_MSG(expr, msg) ((expr) \ |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 117 | ? ((void)0) \ |
| 118 | : ::ndnboost::assertion::detail::assertion_failed_msg(#expr, msg, \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 119 | NDNBOOST_CURRENT_FUNCTION, __FILE__, __LINE__)) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | //--------------------------------------------------------------------------------------// |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 123 | // NDNBOOST_VERIFY // |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 124 | //--------------------------------------------------------------------------------------// |
| 125 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 126 | #undef NDNBOOST_VERIFY |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 127 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 128 | #if defined(NDNBOOST_DISABLE_ASSERTS) || ( !defined(NDNBOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) ) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 129 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 130 | # define NDNBOOST_VERIFY(expr) ((void)(expr)) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 131 | |
| 132 | #else |
| 133 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 134 | # define NDNBOOST_VERIFY(expr) NDNBOOST_ASSERT(expr) |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 135 | |
| 136 | #endif |