Home | History | Annotate | Download | only in boost
      1 //
      2 //  boost/assert.hpp - BOOST_ASSERT(expr)
      3 //                     BOOST_ASSERT_MSG(expr, msg)
      4 //                     BOOST_VERIFY(expr)
      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 //--------------------------------------------------------------------------------------//
     26 //                                     BOOST_ASSERT                                     //
     27 //--------------------------------------------------------------------------------------//
     28 
     29 #undef BOOST_ASSERT
     30 
     31 #if defined(BOOST_DISABLE_ASSERTS)
     32 
     33 # define BOOST_ASSERT(expr) ((void)0)
     34 
     35 #elif defined(BOOST_ENABLE_ASSERT_HANDLER)
     36 
     37 #include <boost/current_function.hpp>
     38 
     39 namespace boost
     40 {
     41   void assertion_failed(char const * expr,
     42                         char const * function, char const * file, long line); // user defined
     43 } // namespace boost
     44 
     45 #define BOOST_ASSERT(expr) ((expr) \
     46   ? ((void)0) \
     47   : ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
     48 
     49 #else
     50 # include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
     51 # define BOOST_ASSERT(expr) assert(expr)
     52 #endif
     53 
     54 //--------------------------------------------------------------------------------------//
     55 //                                   BOOST_ASSERT_MSG                                   //
     56 //--------------------------------------------------------------------------------------//
     57 
     58 # undef BOOST_ASSERT_MSG
     59 
     60 #if defined(BOOST_DISABLE_ASSERTS) || defined(NDEBUG)
     61 
     62   #define BOOST_ASSERT_MSG(expr, msg) ((void)0)
     63 
     64 #elif defined(BOOST_ENABLE_ASSERT_HANDLER)
     65 
     66   #include <boost/current_function.hpp>
     67 
     68   namespace boost
     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 boost
     73 
     74   #define BOOST_ASSERT_MSG(expr, msg) ((expr) \
     75     ? ((void)0) \
     76     : ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
     77 
     78 #else
     79   #ifndef BOOST_ASSERT_HPP
     80     #define BOOST_ASSERT_HPP
     81     #include <cstdlib>
     82     #include <iostream>
     83     #include <boost/current_function.hpp>
     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:
     87     #ifndef BOOST_ASSERT_MSG_OSTREAM
     88     # define BOOST_ASSERT_MSG_OSTREAM std::cerr
     89     #endif
     90 
     91     namespace boost
     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           {
    100             BOOST_ASSERT_MSG_OSTREAM
    101               << "***** Internal Program Error - assertion (" << expr << ") failed in "
    102               << function << ":\n"
    103               << file << '(' << line << "): " << msg << std::endl;
    104             std::abort();
    105           }
    106         } // detail
    107       } // assertion
    108     } // detail
    109   #endif
    110 
    111   #define BOOST_ASSERT_MSG(expr, msg) ((expr) \
    112     ? ((void)0) \
    113     : ::boost::assertion::detail::assertion_failed_msg(#expr, msg, \
    114           BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
    115 #endif
    116 
    117 //--------------------------------------------------------------------------------------//
    118 //                                     BOOST_VERIFY                                     //
    119 //--------------------------------------------------------------------------------------//
    120 
    121 #undef BOOST_VERIFY
    122 
    123 #if defined(BOOST_DISABLE_ASSERTS) || ( !defined(BOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) )
    124 
    125 # define BOOST_VERIFY(expr) ((void)(expr))
    126 
    127 #else
    128 
    129 # define BOOST_VERIFY(expr) BOOST_ASSERT(expr)
    130 
    131 #endif
    132