/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */ /* $Id: cmnAssert.h,v 1.13 2007/04/26 19:33:57 anton Exp $ Author(s): Ankur Kapoor Created on: 2003-06-25 (C) Copyright 2003-2007 Johns Hopkins University (JHU), All Rights Reserved. --- begin cisst license - do not edit --- This software is provided "as is" under an open source license, with no warranty. The complete license can be found in license.txt and http://www.cisst.org/cisst/license.txt. --- end cisst license --- */ /*! \file \brief Assert macros definitions. */ #ifndef _cmnAssert_h #define _cmnAssert_h #include #include #include #include #include #include #ifndef DOXYGEN // No __FUNCTION__ for g++ version < 2.6 __FUNCDNAME__ Valid only // within a function and returns the decorated name of the enclosing // function (as a string). __FUNCDNAME__ is not expanded if you use // the /EP or /P compiler option. __FUNCSIG__ Valid only within a // function and returns the signature of the enclosing function (as a // string). __FUNCSIG__ is not expanded if you use the /EP or /P // compiler option. __FUNCTION__ Valid only within a function and // returns the undecorated name of the enclosing function (as a // string). __FUNCTION__ is not expanded if you use the /EP or /P // compiler option. // Visual C++ #ifdef CISST_COMPILER_IS_MSVC #ifdef __FUNCSIG__ #define CMN_PRETTY_FUNCTION __FUNCSIG__ #else #warning "With Visual Studio, you need /EP or /P to have __FUNCSIG__" #endif // GNU CC and Intel CC #elif (CISST_COMPILER == CISST_GCC) || (CISST_COMPILER == CISST_INTEL_CC) #define CMN_PRETTY_FUNCTION __PRETTY_FUNCTION__ // Otherwise #else #warning "Visual C++, GNU C++ and Intel CC are supported so far" #endif // Set a default value #ifndef CMN_PRETTY_FUNCTION #define CMN_PRETTY_FUNCTION "" #endif #endif // DOXYGEN /*! \ingroup cisstCommon Assert a condition. This macro should be used whenever one needs to assert a condition. This macro has two main advantages over the system \c assert: \li The error message is log using #CMN_LOG (level of detail 1). \li CMN_ASSERT behavior can be modified using the defined variables CMN_ASSERT_DISABLED and CMN_ASSERT_THROWS_EXCEPTION. The first variable allows to not compile the assertion. It is similar to the \c NDEBUG for the standard \c assert .
The second variable allows to throw an exception (of type \c std::logic_error) instead of using the system \c abort. This can be convenient if the libraries are wrapped for an interpreted language such as Python. In this case, an \c abort() has the annoying effect of aborting the interpreter itself.
Both these variables can be modified using the CMake advanced mode. \note When compiling on linux make sure ulimit -c is unlimited! Otherwise, no core file will be generated with \c abort() . By default, Redhat and Debian are set to prevent any core dump. \note On windows \c abort() uses stderr in console apps, message box API with OK button for release builds and message box API with "Abort, Ignore, Retry" for debug builds. \sa cmnThrow */ #ifdef CMN_ASSERT_DISABLED #define CMN_ASSERT(expr) #else // CMN_ASSERT_DISABLED #ifdef CMN_ASSERT_THROWS_EXCEPTION #define CMN_ASSERT(expr) \ if (!(expr)) { \ std::stringstream messageBuffer; \ messageBuffer << __FILE__ << ": Assertion '" << #expr \ << "' failed in: " << CMN_PRETTY_FUNCTION \ << ", line #" << __LINE__; \ cmnThrow(std::logic_error(messageBuffer.str())); \ } #else // CMN_ASSERT_THROWS_EXCEPTION #define CMN_ASSERT(expr) \ if (!(expr)) { \ std::stringstream messageBuffer; \ messageBuffer << __FILE__ << ": Assertion '" << #expr \ << "' failed in: " << CMN_PRETTY_FUNCTION \ << ", line #" << __LINE__; \ std::cerr << messageBuffer.str() << std::endl; \ CMN_LOG(1) << messageBuffer.str() << std::endl; \ abort(); \ } #endif // CMN_ASSERT_THROWS_EXCEPTION #endif // CMN_ASSERT_DISABLED #endif // _cmnAssert_h // **************************************************************************** // Change History // **************************************************************************** // // $Log: cmnAssert.h,v $ // Revision 1.13 2007/04/26 19:33:57 anton // All files in libraries: Applied new license text, separate copyright and // updated dates, added standard header where missing. // // Revision 1.12 2006/11/20 20:33:19 anton // Licensing: Applied new license to cisstCommon, cisstVector, cisstNumerical, // cisstInteractive, cisstImage and cisstOSAbstraction. // // Revision 1.11 2006/10/06 14:29:33 anton // All libraries: Re-ordered #include to include cmnPortability.h before any // other file, including system header files. // // Revision 1.10 2006/09/26 21:43:13 anton // cisstCommon: Updated cmnPortability to support Visual Studio 8 2005 and // used CISST_COMPILER_IS_MSVC when possible. // // Revision 1.9 2005/12/23 21:25:32 anton // cisstCommon: Minor updates for Doxygen 1.4.5. // // Revision 1.8 2005/09/26 21:23:30 anton // cisstCommon: Updated CMN_ASSERT and cmnThrow. Variables are not defined in // cisstConfig.h anymore (user could not override). CMN_ASSERT can now be set // to throw an exception (user can set preference with CMake). // // Revision 1.7 2005/09/26 15:41:46 anton // cisst: Added modelines for emacs and vi. // // Revision 1.6 2005/09/23 23:58:41 anton // cmnAssert: Added support for intel CC which supports pretty function. // // Revision 1.5 2005/05/19 19:29:00 anton // cisst libs: Added the license to cisstCommon and cisstVector // // Revision 1.4 2004/10/25 13:52:05 anton // Doxygen documentation: Cleanup all the useless \ingroup. // // Revision 1.3 2004/10/07 20:13:15 anton // cmnAssert: Added conditional compilation flag for .net 2003. Examples were // not compiling with .net 2003. // // Revision 1.2 2004/01/30 18:45:39 ofri // Added a #else case to the definition of CMN_ASSERT to allow it to compile // without the assertion flag set. // // Revision 1.1 2003/11/14 22:05:36 anton // Added to repository for testing // // // ****************************************************************************