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 <iostream> // For std::cout in MPIABORTIFNON0
00040 #include <cfloat>
00041 #include <climits> //For UINT_MAX etc., 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 
00057 class Exception
00058 {
00059 private:
00060     std::string mMessage; 
00061     std::string mShortMessage; 
00063 public:
00071     Exception(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00072 
00078     std::string GetMessage() const;
00079 
00085     std::string GetShortMessage() const;
00086 
00096     std::string CheckShortMessage(std::string expected) const;
00097 
00107     std::string CheckShortMessageContains(std::string expected) const;
00108 };
00109 
00115 #define EXCEPTION(message) throw Exception(message, __FILE__, __LINE__)
00116 
00117 
00118 #include <boost/preprocessor/stringize.hpp>
00119 
00126 #define EXCEPT_IF_NOT(test) \
00127     if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
00128 
00134 #ifdef NDEBUG
00135 #define UNUSED_OPT(var) var=var
00136 #else
00137 #define UNUSED_OPT(var)
00138 #endif
00139 
00152 #define EXPECT0(cmd, arg) { \
00153     std::string _arg(arg); \
00154     int ret = cmd(_arg.c_str()); \
00155     if (ret != 0) { \
00156         EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
00157     } }
00158 
00159 
00166 #define MPI_ABORT_IF_NON0_WITH_MSG(retcode, msg) \
00167     if (retcode != 0) { \
00168         std::cerr << msg << std::endl << std::flush; \
00169         MPI_Abort(PETSC_COMM_WORLD, -1); \
00170     }
00171 
00178 #define MPIABORTIFNON0(cmd, arg) { \
00179     std::string _arg(arg); \
00180     int ret = cmd(_arg.c_str()); \
00181     MPI_ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
00182     }
00183 
00190 #define EXPECTNON0(cmd, arg) { \
00191     std::string _arg = (arg); \
00192     int ret = cmd(_arg.c_str()); \
00193     if (ret == 0) { \
00194         EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
00195     } }
00196 
00203 #define IGNORE_RET(cmd, arg) { \
00204     std::string _arg = (arg); \
00205     int ret = cmd(_arg.c_str()); \
00206     ret = ret; \
00207     }
00208 
00209 #endif // _EXCEPTION_HPP_

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5