mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
New BOOST_ASSERT, including documentation.
[SVN r16240]
This commit is contained in:
parent
1d601aef4d
commit
45a6249668
57
assert.html
Normal file
57
assert.html
Normal 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"> </td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<p>
|
||||||
|
The header <STRONG><boost/assert.hpp></STRONG> defines the macro <b>BOOST_ASSERT</b>,
|
||||||
|
which is similar to the standard <STRONG>assert</STRONG> macro defined in <STRONG><cassert></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><boost/assert.hpp></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><boost/assert.hpp></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><boost/assert.hpp></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><cassert></STRONG>, <STRONG><boost/assert.hpp></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>
|
@ -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
|
// 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.
|
// 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 <boost/assert.hpp>
|
||||||
#include <cstdio>
|
#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);
|
std::printf("Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line);
|
||||||
return true; // fail w/ standard assert()
|
++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()
|
int main()
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(0 == 1);
|
test_default();
|
||||||
|
test_disabled();
|
||||||
|
test_handler();
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
@ -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.
|
// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
|
||||||
//
|
//
|
||||||
@ -15,38 +8,29 @@
|
|||||||
// This software is provided "as is" without express or implied
|
// This software is provided "as is" without express or implied
|
||||||
// warranty, and with no claim as to its suitability for any purpose.
|
// warranty, and with no claim as to its suitability for any purpose.
|
||||||
//
|
//
|
||||||
|
// Note: There are no include guards. This is intentional.
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef BOOST_DEBUG
|
#undef BOOST_ASSERT
|
||||||
#define BOOST_DEBUG 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#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>
|
#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
|
#else
|
||||||
#define BOOST_ASSERT(expr) ((void)0)
|
# include <assert.h>
|
||||||
|
# define BOOST_ASSERT(expr) assert(expr)
|
||||||
#endif // #if BOOST_DEBUG
|
#endif
|
||||||
|
|
||||||
#endif // #ifndef BOOST_ASSERT_HPP_INCLUDED
|
|
||||||
|
@ -13,14 +13,17 @@
|
|||||||
collection for components too small to be called libraries in their own right.</p>
|
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>
|
<p>But that doesn't mean there isn't useful stuff here. Take a look:</p>
|
||||||
<blockquote>
|
<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="call_traits.htm">call_traits.htm</a><br>
|
||||||
<a href="checked_delete.html">checked_delete.html</a><br>
|
<a href="checked_delete.html">checked_delete.html</a><br>
|
||||||
<a href="compressed_pair.htm">compressed_pair.htm</a><br>
|
<a href="compressed_pair.htm">compressed_pair.htm</a><br>
|
||||||
<a href="operators.htm">operators.htm</a><br>
|
<a href="operators.htm">operators.htm</a><br>
|
||||||
<a href="tie.html">tie</a><br>
|
<a href="tie.html">tie</a><br>
|
||||||
<a href="throw_exception.html">throw_exception.html</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>
|
</blockquote>
|
||||||
<hr>
|
<hr>
|
||||||
<p>Revised
|
<p>Revised
|
||||||
|
Loading…
x
Reference in New Issue
Block a user