exceptions.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_EXCEPTIONS_HPP
00025 #define BTK_COMMON_EXCEPTIONS_HPP
00026 
00027 #include <string>
00028 #include <sstream>
00029 #include <stdexcept>
00030 
00031 namespace BTK {
00032 namespace EXCEPTIONS {
00033 
00034 struct BTKException
00035 {
00036   BTKException(std::string const & msg,
00037                std::string const & file,
00038                unsigned line,
00039                std::string const & function)
00040   {
00041     std::ostringstream mstr;
00042     mstr << msg << " thrown from "
00043          << file << ", line " << line;
00044 
00045     if (!function.empty()) 
00046       mstr << ", fxn = " << function;
00047 
00048     _msg = mstr.str();
00049   }
00050 
00051   virtual ~BTKException() {}
00052 
00053   virtual char const * what() const throw() 
00054   { return _msg.c_str(); }
00055 
00056 private:
00057   std::string _msg;
00058 };
00059 
00060 // This macro is for convenience in defining the basic BTK exception
00061 // classes, and is undefined outside of this file.  To create exceptions
00062 // that derive from the basic BTK exception classes, see the 
00063 // NEW_BTK_EXCEPTION_TYPE macro, below.
00064 #define BASE_BTK_EXCEPTION_TYPE(exception_name,base)                    \
00065   struct exception_name : public base, public BTKException              \
00066   {                                                                     \
00067     exception_name(std::string const & msg = #exception_name,           \
00068                    std::string const & file = "",                       \
00069                    unsigned line = 0,                                   \
00070                    std::string const & func = "") :                     \
00071       base(msg), BTKException(msg,file,line,func) {}                    \
00072                                                                         \
00073     virtual char const * what() const throw()                           \
00074     { return BTKException::what(); }                                    \
00075                                                                         \
00076     virtual ~exception_name() throw() {}                                \
00077   };
00078 
00079 // Exceptions corresponding to the std::logic_error hierarchy
00080 BASE_BTK_EXCEPTION_TYPE(BTKLogicError,std::logic_error);
00081 BASE_BTK_EXCEPTION_TYPE(BTKDomainError,std::domain_error);
00082 BASE_BTK_EXCEPTION_TYPE(BTKInvalidArgument,std::invalid_argument);
00083 BASE_BTK_EXCEPTION_TYPE(BTKLengthError,std::length_error);
00084 BASE_BTK_EXCEPTION_TYPE(BTKOutOfRange,std::out_of_range);
00085 
00086 // Exceptions corresponding to the std::runtime_error hierarchy
00087 BASE_BTK_EXCEPTION_TYPE(BTKRuntimeError,std::runtime_error);
00088 BASE_BTK_EXCEPTION_TYPE(BTKRangeError,std::range_error);
00089 BASE_BTK_EXCEPTION_TYPE(BTKUnderflowError,std::underflow_error);
00090 BASE_BTK_EXCEPTION_TYPE(BTKOverflowError,std::overflow_error);
00091 
00092 #undef BASE_BTK_EXCEPTION_TYPE
00093 
00094 #define NEW_BTK_EXCEPTION_TYPE(exception_name,base_btk_exception)       \
00095   struct exception_name : public base_btk_exception                     \
00096   {                                                                     \
00097     explicit exception_name(char const *msg = #exception_name,          \
00098                             char const *file = "",                      \
00099                             unsigned line = 0,                          \
00100                             char const *func = "") :                    \
00101       base_btk_exception(msg,file,line,func) {}                         \
00102                                                                         \
00103     virtual ~exception_name() throw() {}                                \
00104   }
00105 
00106 #define BTK_THROW(btk_exception)                        \
00107   throw BTK::EXCEPTIONS::btk_exception(#btk_exception,__FILE__,__LINE__)
00108 
00109 #define BTK_THROW_MSG(btk_exception,msg)        \
00110   throw BTK::EXCEPTIONS::btk_exception(msg,__FILE__,__LINE__)
00111 
00112 } // EXCEPTIONS
00113 } // BTK
00114 
00115 #endif 

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