diff --git a/assert.html b/assert.html new file mode 100644 index 0000000..d34ce36 --- /dev/null +++ b/assert.html @@ -0,0 +1,57 @@ + + + + Boost: assert.hpp documentation + + + + + + + + + + + +
+ c++boost.gif (8819 bytes) + +

assert.hpp

+
 
+

+ The header <boost/assert.hpp> defines the macro BOOST_ASSERT, + which is similar to the standard assert macro defined in <cassert>. + The macro is intended to be used in Boost libraries. +

+

By default, BOOST_ASSERT(expr) is equivalent to assert(expr).

+

When the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> + is included, BOOST_ASSERT(expr) is defined as ((void)0). This + allows users to selectively disable BOOST_ASSERT without + affecting the definition of the standard assert.

+

When the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> + is included, BOOST_ASSERT(expr) evaluates expr and, if the + result is false, evaluates the expression

+

::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, + __FILE__, __LINE__)

+

assertion_failed is declared in <boost/assert.hpp> + as

+
+namespace boost
+{
+
+void assertion_failed(char const * expr, char const * function, char const * file, long line);
+
+}
+
+

but it is never defined. The user is expected to supply an appropriate + definition.

+

As is the case with <cassert>, <boost/assert.hpp> + can be included multiple times in a single translation unit. BOOST_ASSERT + will be redefined each time as specified above.

+


+ Copyright © 2002 by Peter Dimov. Permission to copy, use, modify, sell and + distribute this document is granted provided this copyright notice appears in + all copies. This document is provided "as is" without express or implied + warranty, and with no claim as to its suitability for any purpose.

+ + diff --git a/assert_test.cpp b/assert_test.cpp index 5813e25..3a7f892 100644 --- a/assert_test.cpp +++ b/assert_test.cpp @@ -1,10 +1,3 @@ -#if defined(_MSC_VER) && !defined(__ICL) -#pragma warning(disable: 4786) // identifier truncated in debug info -#pragma warning(disable: 4710) // function not inlined -#pragma warning(disable: 4711) // function selected for automatic inline expansion -#pragma warning(disable: 4514) // unreferenced inline removed -#endif - // // assert_test.cpp - a test for boost/assert.hpp // @@ -16,18 +9,97 @@ // warranty, and with no claim as to its suitability for any purpose. // -#define BOOST_DEBUG 1 +#include +#include + +void test_default() +{ + int x = 1; + + BOOST_ASSERT(1); + BOOST_ASSERT(x); + BOOST_ASSERT(x == 1); + BOOST_ASSERT(&x); +} + +#define BOOST_DISABLE_ASSERTS +#include + +void test_disabled() +{ + int x = 1; + + BOOST_ASSERT(1); + BOOST_ASSERT(x); + BOOST_ASSERT(x == 1); + BOOST_ASSERT(&x); + + BOOST_ASSERT(0); + BOOST_ASSERT(!x); + BOOST_ASSERT(x == 0); + + void * p = 0; + + BOOST_ASSERT(p); + + // supress warnings + p = &x; + p = &p; +} + +#undef BOOST_DISABLE_ASSERTS + +#define BOOST_ENABLE_ASSERT_HANDLER #include #include -bool boost_error(char const * expr, char const * func, char const * file, long line) +int handler_invoked = 0; + +void boost::assertion_failed(char const * expr, char const * function, char const * file, long line) { - std::printf("%s(%ld): Assertion '%s' failed in function '%s'\n", file, line, expr, func); - return true; // fail w/ standard assert() + std::printf("Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line); + ++handler_invoked; } +struct X +{ + static void f() + { + BOOST_ASSERT(0); + } +}; + +void test_handler() +{ + int x = 1; + + BOOST_ASSERT(1); + BOOST_ASSERT(x); + BOOST_ASSERT(x == 1); + BOOST_ASSERT(&x); + + BOOST_ASSERT(0); + BOOST_ASSERT(!x); + BOOST_ASSERT(x == 0); + + void * p = 0; + + BOOST_ASSERT(p); + + X::f(); + + BOOST_ASSERT(handler_invoked == 5); + BOOST_TEST(handler_invoked == 5); +} + +#undef BOOST_ENABLE_ASSERT_HANDLER + int main() { - BOOST_ASSERT(0 == 1); + test_default(); + test_disabled(); + test_handler(); + + return boost::report_errors(); } diff --git a/include/boost/assert.hpp b/include/boost/assert.hpp index 645404d..c288a1f 100644 --- a/include/boost/assert.hpp +++ b/include/boost/assert.hpp @@ -1,12 +1,5 @@ -#ifndef BOOST_ASSERT_HPP_INCLUDED -#define BOOST_ASSERT_HPP_INCLUDED - -#if _MSC_VER >= 1020 -#pragma once -#endif - // -// boost/assert.hpp +// boost/assert.hpp - BOOST_ASSERT(expr) // // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. // @@ -15,38 +8,29 @@ // This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // - -// -// When BOOST_DEBUG is not defined, it defaults to 0 (off) -// for compatibility with programs that do not expect asserts -// in the smart pointer class templates. -// -// This default may be changed after an initial transition period. +// Note: There are no include guards. This is intentional. // -#ifndef BOOST_DEBUG -#define BOOST_DEBUG 0 -#endif +#undef BOOST_ASSERT -#if BOOST_DEBUG +#if defined(BOOST_DISABLE_ASSERTS) -#include +# define BOOST_ASSERT(expr) ((void)0) -#ifndef BOOST_ASSERT +#elif defined(BOOST_ENABLE_ASSERT_HANDLER) #include -bool boost_error(char const * expr, char const * func, char const * file, long line); +namespace boost +{ -# define BOOST_ASSERT(expr) ((expr) || !boost_error(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__) || (assert(expr), true)) +void assertion_failed(char const * expr, char const * function, char const * file, long line); // user defined -#endif // #ifndef BOOST_ASSERT +} // namespace boost -#else // #if BOOST_DEBUG +#define BOOST_ASSERT(expr) ((expr)? ((void)0): ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)) -#undef BOOST_ASSERT -#define BOOST_ASSERT(expr) ((void)0) - -#endif // #if BOOST_DEBUG - -#endif // #ifndef BOOST_ASSERT_HPP_INCLUDED +#else +# include +# define BOOST_ASSERT(expr) assert(expr) +#endif diff --git a/index.html b/index.html index 370b167..def3326 100644 --- a/index.html +++ b/index.html @@ -13,14 +13,17 @@ collection for components too small to be called libraries in their own right.

But that doesn't mean there isn't useful stuff here. Take a look:

-

base_from_member
+

+ assert.html
+ base_from_member
call_traits.htm
checked_delete.html
compressed_pair.htm
operators.htm
tie
throw_exception.html
- utility.htm

+ utility.htm +


Revised