New BOOST_ASSERT, including documentation.

[SVN r16240]
This commit is contained in:
Peter Dimov 2002-11-14 16:09:29 +00:00
parent 1d601aef4d
commit 45a6249668
4 changed files with 161 additions and 45 deletions

57
assert.html Normal file
View File

@ -0,0 +1,57 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Boost: assert.hpp documentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="white" style="MARGIN-LEFT: 5%; MARGIN-RIGHT: 5%">
<table border="0" width="100%">
<tr>
<td width="277">
<img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86">
</td>
<td align="middle">
<h1>assert.hpp</h1>
</td>
</tr>
<tr>
<td colspan="2" height="64">&nbsp;</td>
</tr>
</table>
<p>
The header <STRONG>&lt;boost/assert.hpp&gt;</STRONG> defines the macro <b>BOOST_ASSERT</b>,
which is similar to the standard <STRONG>assert</STRONG> macro defined in <STRONG>&lt;cassert&gt;</STRONG>.
The macro is intended to be used in Boost libraries.
</p>
<P>By default, <tt>BOOST_ASSERT(expr)</tt> is equivalent to <tt>assert(expr)</tt>.</P>
<P>When the macro <STRONG>BOOST_DISABLE_ASSERTS</STRONG> is defined when <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
is included, <tt>BOOST_ASSERT(expr)</tt> is defined as <tt>((void)0)</tt>. This
allows users to selectively disable <STRONG>BOOST_ASSERT</STRONG> without
affecting the definition of the standard <STRONG>assert</STRONG>.</P>
<P>When the macro <STRONG>BOOST_ENABLE_ASSERT_HANDLER</STRONG> is defined when <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
is included, <tt>BOOST_ASSERT(expr)</tt> evaluates <b>expr</b> and, if the
result is false, evaluates the expression</P>
<P><tt>::boost::assertion_failed(#expr, <a href="current_function.html">BOOST_CURRENT_FUNCTION</a>,
__FILE__, __LINE__)</tt></P>
<P><STRONG>assertion_failed</STRONG> is declared in <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
as</P>
<pre>
namespace boost
{
void assertion_failed(char const * expr, char const * function, char const * file, long line);
}
</pre>
<p>but it is never defined. The user is expected to supply an appropriate
definition.</p>
<P>As is the case with <STRONG>&lt;cassert&gt;</STRONG>, <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
can be included multiple times in a single translation unit. <STRONG>BOOST_ASSERT</STRONG>
will be redefined each time as specified above.</P>
<p><br>
<small>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.</small></p>
</body>
</html>

View File

@ -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 <boost/detail/lightweight_test.hpp>
#include <boost/assert.hpp>
void test_default()
{
int x = 1;
BOOST_ASSERT(1);
BOOST_ASSERT(x);
BOOST_ASSERT(x == 1);
BOOST_ASSERT(&x);
}
#define BOOST_DISABLE_ASSERTS
#include <boost/assert.hpp>
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 <boost/assert.hpp>
#include <cstdio>
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();
}

View File

@ -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 <assert.h>
# define BOOST_ASSERT(expr) ((void)0)
#ifndef BOOST_ASSERT
#elif defined(BOOST_ENABLE_ASSERT_HANDLER)
#include <boost/current_function.hpp>
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 <assert.h>
# define BOOST_ASSERT(expr) assert(expr)
#endif

View File

@ -13,14 +13,17 @@
collection for components too small to be called libraries in their own right.</p>
<p>But that doesn't mean there isn't useful stuff here. Take a look:</p>
<blockquote>
<p><a href="base_from_member.html">base_from_member</a><br>
<p>
<a href="assert.html">assert.html</a><br>
<a href="base_from_member.html">base_from_member</a><br>
<a href="call_traits.htm">call_traits.htm</a><br>
<a href="checked_delete.html">checked_delete.html</a><br>
<a href="compressed_pair.htm">compressed_pair.htm</a><br>
<a href="operators.htm">operators.htm</a><br>
<a href="tie.html">tie</a><br>
<a href="throw_exception.html">throw_exception.html</a><br>
<a href="utility.htm">utility.htm</a></p>
<a href="utility.htm">utility.htm</a>
</p>
</blockquote>
<hr>
<p>Revised