#ifndef MDC_ASSERTION_FAILED_H
#define MDC_ASSERTION_FAILED_H
// Copyright 2005 GoingWare Inc.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// libmdc - http://www.goingware.com/libmdc/
/* It is a fatal error for one of these exceptions to be
* thrown. If you should happen to catch one in production code,
* you should terminate the application.
*
* This is thrown if mdc_assert fails and if MDC_ASSERTION_EXCEPTION is 1
* You don't want to define MDC_ASSERTION_EXCEPTION in production code;
* just use mdc_assert's default definition. The reason this is here
* is so that we can verify that an assertion that is supposed to fail
* DOES fail when testing under CPPUnit. Use it like this:
*
* #define MDC_ASSERTION_EXCEPTION 1
* #include "mdc_assert.h"
*
* CPPUNIT_TEST_EXCEPTION( testFail, mdc::assertion_failed );
*
* void Test::testFail()
* {
* mdc_assert( false );
* }
*
* CPPUnit will report that the above test passed.
*/
#include
namespace mdc{
class assertion_failed: public std::exception
{
public:
assertion_failed( char const *assertion, char const *file, int line ) throw();
assertion_failed( assertion_failed const &inOrig ) throw();
~assertion_failed() throw();
assertion_failed const &operator=( assertion_failed const &inRHS ) throw();
char const *what() const throw();
void assertInvariant() const throw();
private:
enum{
kMessageBufLength = 256
};
char mMessage[ kMessageBufLength ];
static char const *sAssertionFailedStr;
static char const *sLineStr;
bool invariantValid() const throw();
};
}//namespace mdc
/* mdc_exceptionAssert is not guarded by #if so it will always
* be available if you define your own kind of assertion.
*
* HOWEVER: An assertion failure is ALWAYS a FATAL error. That means
* that it is wrong to handle one in a production application in any
* other way than immediate termination.
*
* I can see only one way you would want to leave such an assertion
* in a product delivered to end-users: that's if there is a bug that
* you can detect, but just can't figure out how to fix, so you are
* forced to ship it to customers with the bug still present in the product.
* Using assertions that throw exceptions allow you to catch them at
* some higher level in the call chain where you can attempt to save
* the user's document before terminating.
*/
#define mdc_exception_assert( assertion )\
static_cast< void >( (assertion) ? 0 : \
(throw mdc::assertion_failed( #assertion, \
__FILE__, __LINE__ ) ) )
#endif