Peter,

I like the ability to map BOOST_ASSERT into our own error reporting system.

A few comments.

Since assert is only used if BOOST_ASSERT isn't defined, shouldn't the #include be moved inside the #ifndef BOOST_ASSERT?

Why do we need BOOST_DEBUG instead of just using NDEBUG?  Are you assuming that people might want use BOOST_ASSERTS in release builds?  Also BOOST_DEBUG doesn't do anything if BOOST_ASSERT is defined.

 
Another possible variant of BOOST_ASSERT is to have a macro that supplies a message that is display instead of the expression.  It would look like:

BOOST_ASSERT_MSG( IsOpen(), "The caller should open the file." ) ;

We've found it helpful to sometimes give an explanation for the assert instead of just the expression.

Chris

----------
From:   Peter Dimov
Reply To:       boost@yahoogroups.com
Sent:   Monday, November 12, 2001 8:19 AM
To:     Boost List
Subject:        [boost] Boost.Assert

See the code at the end for my proposed implementation of BOOST_ASSERT.

Design goals:

* Simple;
* Off by default;
* User customizable.

Modes of operation include:

1. off; when BOOST_DEBUG is not defined.
2. simply use assert(); with -DBOOST_DEBUG and -DBOOST_ASSERT=assert.
3. invoke user error handler (boost_error), then assert();
with -DBOOST_DEBUG.
3a. log error and throw.
3b. log error and continue.
3c. log error and assert().
3d. log error and terminate (whole process or current thread only.)
3e. perform cleanup and assert().

etc.

Comments?

--
Peter Dimov
Multi Media Ltd.

#define BOOST_DEBUG

// boost/assert.hpp

#ifdef BOOST_DEBUG
#include <assert.h>
#endif

#ifndef BOOST_ASSERT

#ifdef BOOST_DEBUG

bool boost_error(char const * expr, char const * file, long line);

#ifndef BOOST_ERROR
#define BOOST_ERROR(expr) (expr || !boost_error(#expr, __FILE__, __LINE__))
#endif

#define BOOST_ASSERT(expr) assert(BOOST_ERROR(expr))

#else

#define BOOST_ASSERT ((void)0)

#endif

#endif

//

// user error handler

#include <cstdio>

bool boost_error(char const * expr, char const * file, long line)
{
 std::printf("boost_error(%s, %s, %ld)\n", expr, file, line);
 return true;
}

//

// boost or user code

int main()
{
 BOOST_ASSERT(0 == 1);
}




Info: http://www.boost.org  Unsubscribe: <mailto:boost-unsubscribe@yahoogroups.com>

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/