[1638] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief |
---|
| 32 | Declaration of the Exception class. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #ifndef _Exception_H__ |
---|
| 36 | #define _Exception_H__ |
---|
| 37 | |
---|
[1764] | 38 | #include "UtilPrereqs.h" |
---|
[1638] | 39 | |
---|
| 40 | #include <string> |
---|
| 41 | #include <exception> |
---|
[1642] | 42 | #include <cassert> |
---|
[1764] | 43 | #include "Debug.h" |
---|
[1638] | 44 | |
---|
[1660] | 45 | // Define some ugly macros to make things more clear |
---|
| 46 | #define CREATE_ORXONOX_EXCEPTION(name) typedef SpecificException<Exception::name> name##Exception; |
---|
| 47 | #define RETURN_EXCEPTION_CODE(name) \ |
---|
| 48 | case Exception::name: \ |
---|
| 49 | return #name; |
---|
| 50 | |
---|
| 51 | |
---|
[1638] | 52 | namespace orxonox |
---|
| 53 | { |
---|
[1764] | 54 | class _UtilExport Exception : public std::exception |
---|
[1638] | 55 | { |
---|
| 56 | public: |
---|
| 57 | enum ExceptionType |
---|
| 58 | { |
---|
| 59 | General, |
---|
| 60 | FileNotFound, |
---|
[1663] | 61 | Argument, |
---|
[2298] | 62 | PhysicsViolation, |
---|
| 63 | ParseError, |
---|
[1638] | 64 | PluginsNotFound, |
---|
[1652] | 65 | InitialisationFailed, |
---|
[1660] | 66 | NotImplemented, |
---|
| 67 | GameState |
---|
[1638] | 68 | }; |
---|
| 69 | |
---|
| 70 | Exception(const std::string& description, int lineNumber, |
---|
[2103] | 71 | const char* filename, const char* functionName); |
---|
[1638] | 72 | Exception(const std::string& description); |
---|
| 73 | |
---|
| 74 | /// Needed for compatibility with std::exception (from Ogre::Exception) |
---|
| 75 | virtual ~Exception() throw() { } |
---|
| 76 | |
---|
[1664] | 77 | virtual const std::string& getFullDescription() const; |
---|
[1638] | 78 | virtual ExceptionType getType() const = 0; |
---|
| 79 | virtual std::string getTypeName() const = 0; |
---|
| 80 | virtual const std::string& getDescription() const { return this->description_; } |
---|
| 81 | virtual const int getLineNumber() const { return this->lineNumber_; } |
---|
| 82 | virtual const std::string& getFunctionName() const { return this->functionName_; } |
---|
| 83 | |
---|
| 84 | /// Override std::exception::what (from Ogre::Exception) |
---|
| 85 | const char* what() const throw() { return getFullDescription().c_str(); } |
---|
| 86 | |
---|
| 87 | protected: |
---|
| 88 | std::string description_; |
---|
| 89 | int lineNumber_; |
---|
| 90 | std::string functionName_; |
---|
[2103] | 91 | std::string filename_; |
---|
[1670] | 92 | // mutable because "what()" is a const method |
---|
[1664] | 93 | mutable std::string fullDescription_; |
---|
[1638] | 94 | }; |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | template <Exception::ExceptionType Type> |
---|
| 98 | class SpecificException : public Exception |
---|
| 99 | { |
---|
| 100 | public: |
---|
| 101 | SpecificException(const std::string& description, int lineNumber, |
---|
[2103] | 102 | const char* filename, const char* functionName) |
---|
| 103 | : Exception(description, lineNumber, filename, functionName) |
---|
[1638] | 104 | { |
---|
[1704] | 105 | // let the catcher decide whether to display the message below level 4 |
---|
| 106 | COUT(4) << this->getFullDescription() << std::endl; |
---|
[1638] | 107 | } |
---|
| 108 | |
---|
| 109 | SpecificException(const std::string& description) |
---|
| 110 | : Exception(description) |
---|
| 111 | { |
---|
[1704] | 112 | // let the catcher decide whether to display the message below level 4 |
---|
| 113 | COUT(4) << this->getFullDescription() << std::endl; |
---|
[1638] | 114 | } |
---|
| 115 | |
---|
[1645] | 116 | ~SpecificException() throw() { } |
---|
[1638] | 117 | |
---|
| 118 | ExceptionType getType() const { return Type; } |
---|
| 119 | std::string getTypeName() const |
---|
| 120 | { |
---|
[1660] | 121 | // note: break is not necessary due to the return in the macros. |
---|
[1638] | 122 | switch (Type) |
---|
| 123 | { |
---|
[1660] | 124 | RETURN_EXCEPTION_CODE(General) |
---|
| 125 | RETURN_EXCEPTION_CODE(FileNotFound); |
---|
[1663] | 126 | RETURN_EXCEPTION_CODE(Argument); |
---|
[2298] | 127 | RETURN_EXCEPTION_CODE(PhysicsViolation); |
---|
| 128 | RETURN_EXCEPTION_CODE(ParseError); |
---|
[1660] | 129 | RETURN_EXCEPTION_CODE(PluginsNotFound); |
---|
| 130 | RETURN_EXCEPTION_CODE(InitialisationFailed); |
---|
| 131 | RETURN_EXCEPTION_CODE(NotImplemented); |
---|
| 132 | RETURN_EXCEPTION_CODE(GameState); |
---|
[1638] | 133 | default: |
---|
| 134 | return ""; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | }; |
---|
| 138 | |
---|
[1660] | 139 | // define the template spcialisations |
---|
| 140 | CREATE_ORXONOX_EXCEPTION(General); |
---|
| 141 | CREATE_ORXONOX_EXCEPTION(FileNotFound); |
---|
[1663] | 142 | CREATE_ORXONOX_EXCEPTION(Argument); |
---|
[2298] | 143 | CREATE_ORXONOX_EXCEPTION(PhysicsViolation); |
---|
| 144 | CREATE_ORXONOX_EXCEPTION(ParseError); |
---|
[1660] | 145 | CREATE_ORXONOX_EXCEPTION(PluginsNotFound); |
---|
| 146 | CREATE_ORXONOX_EXCEPTION(InitialisationFailed); |
---|
| 147 | CREATE_ORXONOX_EXCEPTION(NotImplemented); |
---|
| 148 | CREATE_ORXONOX_EXCEPTION(GameState); |
---|
[1638] | 149 | |
---|
| 150 | #define ThrowException(type, description) \ |
---|
| 151 | throw SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__) |
---|
| 152 | |
---|
[1642] | 153 | // define an assert macro that can display a message |
---|
| 154 | #ifndef NDEBUG |
---|
[1663] | 155 | #define OrxAssert(assertion, errorMessage) \ |
---|
| 156 | assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \ |
---|
| 157 | assert(assertion) |
---|
[1642] | 158 | #else |
---|
| 159 | #define OrxAssert(condition, errorMessage) ((void)0) |
---|
| 160 | #endif |
---|
| 161 | |
---|
[1638] | 162 | } |
---|
| 163 | |
---|
| 164 | #endif /* _Exception_H__ */ |
---|