assertions.hpp

Go to the documentation of this file.
00001 // -*- mode: c++; indent-tabs-mode: nil; -*-
00002 //
00003 //The Biomolecule Toolkit (BTK) is a C++ library for use in the
00004 //modeling, analysis, and design of biological macromolecules.
00005 //Copyright (C) 2006, Tim Robertson <kid50@users.sourceforge.net>
00006 //
00007 //This program is free software; you can redistribute it and/or modify
00008 //it under the terms of the GNU Lesser General Public License as published
00009 //by the Free Software Foundation; either version 2.1 of the License, or (at
00010 //your option) any later version.
00011 //
00012 //This program is distributed in the hope that it will be useful,  but
00013 //WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //Lesser General Public License for more details.
00016 //
00017 //You should have received a copy of the GNU Lesser General Public License
00018 //along with this program; if not, write to the Free Software Foundation,
00019 //Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00023 
00024 #ifndef BTK_COMMON_ASSERTIONS_HPP
00025 #define BTK_COMMON_ASSERTIONS_HPP
00026 
00027 #include <string>
00028 #include <cstdlib>
00029 
00030 #include <btk/core/config/btk_config.hpp>
00031 #include <btk/core/io/logging.hpp>
00032 
00033 namespace BTK {
00034 namespace COMMON {
00035 namespace ASSERTIONS {
00036 
00037 typedef enum {EXIT, CONTINUE, IGNORE} assert_code;
00038 
00039 std::string make_assert_message(std::string const & msg,
00040                                 std::string const & file,
00041                                 unsigned line);
00042 
00043 typedef assert_code (*assert_handler)(std::string const &,
00044                                       std::string const &,
00045                                       unsigned);
00046 
00047 assert_code default_assert_handler(std::string const & msg,
00048                                    std::string const & file,
00049                                    unsigned line);
00050 
00051 extern assert_handler current_assert_handler;
00052 
00053 } // namespace ASSERTIONS
00054 } // namespace COMMON
00055 } // namespace BTK
00056 
00057 #ifndef BTK_NDEBUG                                                          
00058 # define BTK_ASSERT(expr,str)                                           \
00059   if (!(expr)) {                                                        \
00060     using namespace BTK::COMMON::ASSERTIONS;                           \
00061     static assert_code status = EXIT;                                   \
00062                                                                         \
00063     if (status != IGNORE) {                                             \
00064       status =                                                          \
00065         current_assert_handler((str),                                   \
00066                                __FILE__,                                \
00067                                __LINE__);                               \
00068       if (status == EXIT) std::exit(-1);                                \
00069     }                                                                   \
00070   };
00071 
00072 # define BTK_INVARIANT(expr) BTK_ASSERT(expr,"Invariant violated")
00073 #else
00074 # define BTK_ASSERT(expr,str)
00075 
00076 # define BTK_INVARIANT(expr)                                                   \
00077   if (!(expr)) {                                                               \
00078     BTK::COMMON::LOGGING::error_msg(make_assert_message("Invariant violated", \
00079                                                          __FILE__,__LINE__));  \
00080     std::exit(-1);                                                             \
00081   }
00082 #endif
00083 
00084 #endif //BTK_ASSERTIONS_H

Generated on Sun Jul 15 20:46:24 2007 for BTK Core by  doxygen 1.5.1