blob: 7c7a672f542e7e0d5182809205f965146d75f95c [file] [log] [blame]
Jeff Thompsonf7d49942013-08-01 16:47:40 -07001//
Jeff Thompson3d613fd2013-10-15 15:39:04 -07002// ndnboost/assert.hpp - NDNBOOST_ASSERT(expr)
3// NDNBOOST_ASSERT_MSG(expr, msg)
4// NDNBOOST_VERIFY(expr)
Jeff Thompsonf7d49942013-08-01 16:47:40 -07005//
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 Thompson3d613fd2013-10-15 15:39:04 -070026// NDNBOOST_ASSERT //
Jeff Thompsonf7d49942013-08-01 16:47:40 -070027//--------------------------------------------------------------------------------------//
28
Jeff Thompson3d613fd2013-10-15 15:39:04 -070029#undef NDNBOOST_ASSERT
Jeff Thompsonf7d49942013-08-01 16:47:40 -070030
Jeff Thompson3d613fd2013-10-15 15:39:04 -070031#if defined(NDNBOOST_DISABLE_ASSERTS)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070032
Jeff Thompson3d613fd2013-10-15 15:39:04 -070033# define NDNBOOST_ASSERT(expr) ((void)0)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070034
Jeff Thompson3d613fd2013-10-15 15:39:04 -070035#elif defined(NDNBOOST_ENABLE_ASSERT_HANDLER)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070036
Jeff Thompson2277ce52013-08-01 17:34:11 -070037#include <ndnboost/current_function.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070038
39namespace 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 Thompson3d613fd2013-10-15 15:39:04 -070045#define NDNBOOST_ASSERT(expr) ((expr) \
Jeff Thompsonf7d49942013-08-01 16:47:40 -070046 ? ((void)0) \
Jeff Thompson3d613fd2013-10-15 15:39:04 -070047 : ::ndnboost::assertion_failed(#expr, NDNBOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
Jeff Thompsonf7d49942013-08-01 16:47:40 -070048
49#else
50# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
Jeff Thompson3d613fd2013-10-15 15:39:04 -070051# define NDNBOOST_ASSERT(expr) assert(expr)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070052#endif
53
54//--------------------------------------------------------------------------------------//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070055// NDNBOOST_ASSERT_MSG //
Jeff Thompsonf7d49942013-08-01 16:47:40 -070056//--------------------------------------------------------------------------------------//
57
Jeff Thompson3d613fd2013-10-15 15:39:04 -070058# undef NDNBOOST_ASSERT_MSG
Jeff Thompsonf7d49942013-08-01 16:47:40 -070059
Jeff Thompson3d613fd2013-10-15 15:39:04 -070060#if defined(NDNBOOST_DISABLE_ASSERTS) || defined(NDEBUG)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070061
Jeff Thompson3d613fd2013-10-15 15:39:04 -070062 #define NDNBOOST_ASSERT_MSG(expr, msg) ((void)0)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070063
Jeff Thompson3d613fd2013-10-15 15:39:04 -070064#elif defined(NDNBOOST_ENABLE_ASSERT_HANDLER)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070065
Jeff Thompson2277ce52013-08-01 17:34:11 -070066 #include <ndnboost/current_function.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070067
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 Thompson3d613fd2013-10-15 15:39:04 -070074 #define NDNBOOST_ASSERT_MSG(expr, msg) ((expr) \
Jeff Thompsonf7d49942013-08-01 16:47:40 -070075 ? ((void)0) \
Jeff Thompson3d613fd2013-10-15 15:39:04 -070076 : ::ndnboost::assertion_failed_msg(#expr, msg, NDNBOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
Jeff Thompsonf7d49942013-08-01 16:47:40 -070077
78#else
Jeff Thompson3d613fd2013-10-15 15:39:04 -070079 #ifndef NDNBOOST_ASSERT_HPP
80 #define NDNBOOST_ASSERT_HPP
Jeff Thompsonf7d49942013-08-01 16:47:40 -070081 #include <cstdlib>
82 #include <iostream>
Jeff Thompson2277ce52013-08-01 17:34:11 -070083 #include <ndnboost/current_function.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070084
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 Thompson3d613fd2013-10-15 15:39:04 -070087 #ifndef NDNBOOST_ASSERT_MSG_OSTREAM
88 # define NDNBOOST_ASSERT_MSG_OSTREAM std::cerr
Jeff Thompsonf7d49942013-08-01 16:47:40 -070089 #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 Thompson3d613fd2013-10-15 15:39:04 -0700100 NDNBOOST_ASSERT_MSG_OSTREAM
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700101 << "***** Internal Program Error - assertion (" << expr << ") failed in "
102 << function << ":\n"
103 << file << '(' << line << "): " << msg << std::endl;
Jeff Thompsona28eed82013-08-22 16:21:10 -0700104 #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 Thompsonf7d49942013-08-01 16:47:40 -0700110 }
111 } // detail
112 } // assertion
113 } // detail
114 #endif
115
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700116 #define NDNBOOST_ASSERT_MSG(expr, msg) ((expr) \
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700117 ? ((void)0) \
118 : ::ndnboost::assertion::detail::assertion_failed_msg(#expr, msg, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700119 NDNBOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700120#endif
121
122//--------------------------------------------------------------------------------------//
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700123// NDNBOOST_VERIFY //
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700124//--------------------------------------------------------------------------------------//
125
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700126#undef NDNBOOST_VERIFY
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700127
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700128#if defined(NDNBOOST_DISABLE_ASSERTS) || ( !defined(NDNBOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) )
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700129
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700130# define NDNBOOST_VERIFY(expr) ((void)(expr))
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700131
132#else
133
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700134# define NDNBOOST_VERIFY(expr) NDNBOOST_ASSERT(expr)
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700135
136#endif