Exception.hpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #ifndef _EXCEPTION_HPP_
00031 #define _EXCEPTION_HPP_
00032 
00038 #include <string>
00039 #include <sstream>
00040 #include <cfloat>  // For DBL_MAX
00041 #include <climits> // For UINT_MAX & INT_MAX, necessary in gcc-4.3
00042 #include <cstdlib> // For system() etc., necessary in gcc-4.3
00043 
00045 const unsigned UNSIGNED_UNSET = UINT_MAX;
00047 const int INT_UNSET = INT_MAX;
00049 const double DOUBLE_UNSET = DBL_MAX;
00050 
00056 class Exception
00057 {
00058 public:
00066     Exception(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00067 
00073     std::string GetMessage() const;
00074 
00080     std::string GetShortMessage() const;
00081 
00091     std::string CheckShortMessage(std::string expected) const;
00092 
00102     std::string CheckShortMessageContains(std::string expected) const;
00103 
00112     static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00113 
00114 protected:
00123     void SetMessage(const std::string& rMessage,
00124                     const std::string& rFilename, unsigned lineNumber);
00125 
00126 private:
00127     std::string mMessage; 
00128     std::string mShortMessage; 
00129 };
00130 
00136 #define EXCEPTION(message)                           \
00137     do {                                             \
00138         std::stringstream msg_stream;                \
00139         msg_stream << message;                       \
00140         throw Exception(msg_stream.str(), __FILE__, __LINE__); \
00141     } while (false)
00142 
00143 #include <boost/preprocessor/stringize.hpp>
00144 
00151 #define EXCEPT_IF_NOT(test) \
00152     if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
00153 
00154 
00161 #define TERMINATE(message) Exception::Terminate(message, __FILE__, __LINE__)
00162 
00194 #define NEVER_REACHED  TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
00195 
00201 #ifdef NDEBUG
00202 #define UNUSED_OPT(var) var=var
00203 #else
00204 #define UNUSED_OPT(var)
00205 #endif
00206 
00219 #define EXPECT0(cmd, arg) {      \
00220     std::string _arg(arg);       \
00221     int ret = cmd(_arg.c_str()); \
00222     if (ret != 0) {              \
00223         EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
00224     } }
00225 
00226 
00233 #define ABORT_IF_NON0_WITH_MSG(retcode, msg)     \
00234     if (retcode != 0) {                          \
00235         TERMINATE(msg);                          \
00236     }
00237 
00248 #define ABORT_IF_NON0(cmd, arg) { \
00249     std::string _arg(arg);        \
00250     int ret = cmd(_arg.c_str());  \
00251     ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
00252     }
00253 
00260 #define EXPECT_NON0(cmd, arg) {   \
00261     std::string _arg = (arg);     \
00262     int ret = cmd(_arg.c_str());  \
00263     if (ret == 0) {               \
00264         EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
00265     } }
00266 
00273 #define IGNORE_RET(cmd, arg) {   \
00274     std::string _arg = (arg);    \
00275     int ret = cmd(_arg.c_str()); \
00276     ret = ret;                   \
00277     }
00278 
00279 #endif // _EXCEPTION_HPP_
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3