| [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 | 
|---|
| [3196] | 32 | Declaration of facilities to handle exceptions. | 
|---|
| [1638] | 33 | */ | 
|---|
|  | 34 |  | 
|---|
|  | 35 | #ifndef _Exception_H__ | 
|---|
|  | 36 | #define _Exception_H__ | 
|---|
|  | 37 |  | 
|---|
| [1764] | 38 | #include "UtilPrereqs.h" | 
|---|
| [1638] | 39 |  | 
|---|
| [3196] | 40 | #include <exception> | 
|---|
|  | 41 | #include <sstream> | 
|---|
| [1638] | 42 | #include <string> | 
|---|
| [1764] | 43 | #include "Debug.h" | 
|---|
| [1638] | 44 |  | 
|---|
|  | 45 | namespace orxonox | 
|---|
|  | 46 | { | 
|---|
| [3196] | 47 | /** | 
|---|
|  | 48 | @brief | 
|---|
|  | 49 | Base class for all exceptions (derived from std::exception). | 
|---|
|  | 50 | @details | 
|---|
|  | 51 | This class provides support for information about the file, the line | 
|---|
|  | 52 | and the function the error occured. | 
|---|
|  | 53 | */ | 
|---|
| [1764] | 54 | class _UtilExport Exception : public std::exception | 
|---|
| [1638] | 55 | { | 
|---|
|  | 56 | public: | 
|---|
| [3196] | 57 | /** | 
|---|
|  | 58 | @brief | 
|---|
|  | 59 | Creates the exception but doesn't yet compose the full descrption (because of the virtual functions) | 
|---|
|  | 60 | @param description | 
|---|
|  | 61 | Exception description as string. This message is supposed to help developers! | 
|---|
|  | 62 | */ | 
|---|
|  | 63 | Exception(const std::string& description, unsigned int lineNumber, | 
|---|
| [2103] | 64 | const char* filename, const char* functionName); | 
|---|
| [3196] | 65 | //! Simplified constructor with just a description. If you need more, use the other one. | 
|---|
| [1638] | 66 | Exception(const std::string& description); | 
|---|
|  | 67 |  | 
|---|
| [3196] | 68 | //! Needed for compatibility with std::exception | 
|---|
| [1638] | 69 | virtual ~Exception() throw() { } | 
|---|
| [3280] | 70 | const char* what() const throw(); | 
|---|
| [1638] | 71 |  | 
|---|
| [3196] | 72 | //! Returns a full description with type, line, file and function | 
|---|
| [1664] | 73 | virtual const std::string& getFullDescription() const; | 
|---|
| [3196] | 74 | //! Returns the string name of the exception type | 
|---|
| [1638] | 75 | virtual std::string        getTypeName()        const = 0; | 
|---|
| [3196] | 76 | //! Returns the short developer written exception | 
|---|
| [1638] | 77 | virtual const std::string& getDescription()     const { return this->description_; } | 
|---|
| [3196] | 78 | //! Returns the line number on which the exception occurred. | 
|---|
|  | 79 | virtual const unsigned int getLineNumber()      const { return this->lineNumber_; } | 
|---|
|  | 80 | //! Returns the function in which the exception occurred. | 
|---|
| [1638] | 81 | virtual const std::string& getFunctionName()    const { return this->functionName_; } | 
|---|
| [3196] | 82 | //! Returns the filename in which the exception occurred. | 
|---|
|  | 83 | virtual const std::string& getFilename()        const { return this->filename_; } | 
|---|
| [1638] | 84 |  | 
|---|
| [5747] | 85 | /** | 
|---|
|  | 86 | @brief | 
|---|
|  | 87 | Retrieves information from an exception caught with "..." | 
|---|
|  | 88 | Works for std::exception and CEGUI::Exception | 
|---|
|  | 89 | @remarks | 
|---|
|  | 90 | Never ever call this function without an exception in the stack! | 
|---|
|  | 91 | */ | 
|---|
|  | 92 | static std::string handleMessage(); | 
|---|
|  | 93 |  | 
|---|
| [1638] | 94 | protected: | 
|---|
| [3196] | 95 | std::string description_;             //!< User typed text about why the exception occurred | 
|---|
|  | 96 | unsigned int lineNumber_;             //!< Line on which the exception occurred | 
|---|
|  | 97 | std::string functionName_;            //!< Function (including namespace and class) where the exception occurred | 
|---|
|  | 98 | std::string filename_;                //!< File where the exception occurred | 
|---|
| [1670] | 99 | // mutable because "what()" is a const method | 
|---|
| [3196] | 100 | mutable std::string fullDescription_; //!< Full description with line, file and function | 
|---|
| [1638] | 101 | }; | 
|---|
|  | 102 |  | 
|---|
| [3196] | 103 | //! Creates a new type of exception that inherits from tracker::Exception | 
|---|
| [2171] | 104 | #define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \ | 
|---|
|  | 105 | class ExceptionName##Exception : public Exception                               \ | 
|---|
|  | 106 | {                                                                               \ | 
|---|
|  | 107 | public:                                                                         \ | 
|---|
| [3196] | 108 | ExceptionName##Exception(const std::string& description,                    \ | 
|---|
|  | 109 | unsigned int lineNumber, const char* filename,                      \ | 
|---|
|  | 110 | const char* functionName)                                           \ | 
|---|
|  | 111 | : Exception(description, lineNumber, filename, functionName)            \ | 
|---|
| [2662] | 112 | { }                                                                         \ | 
|---|
| [2171] | 113 | \ | 
|---|
|  | 114 | ExceptionName##Exception(const std::string& description)                    \ | 
|---|
| [3196] | 115 | : Exception(description)                                                \ | 
|---|
| [2662] | 116 | { }                                                                         \ | 
|---|
| [2171] | 117 | \ | 
|---|
|  | 118 | ~ExceptionName##Exception() throw() { }                                     \ | 
|---|
|  | 119 | \ | 
|---|
|  | 120 | std::string getTypeName() const { return #ExceptionName; }                  \ | 
|---|
| [3196] | 121 | } | 
|---|
| [1638] | 122 |  | 
|---|
| [2171] | 123 | // Creates all possible exception types. | 
|---|
|  | 124 | // If you want to add a new type, simply copy and adjust a new line here. | 
|---|
| [3196] | 125 | #ifndef DOXYGEN_SHOULD_SKIP_THIS | 
|---|
| [1660] | 126 | CREATE_ORXONOX_EXCEPTION(General); | 
|---|
|  | 127 | CREATE_ORXONOX_EXCEPTION(FileNotFound); | 
|---|
| [1663] | 128 | CREATE_ORXONOX_EXCEPTION(Argument); | 
|---|
| [2662] | 129 | CREATE_ORXONOX_EXCEPTION(PhysicsViolation); | 
|---|
|  | 130 | CREATE_ORXONOX_EXCEPTION(ParseError); | 
|---|
| [1660] | 131 | CREATE_ORXONOX_EXCEPTION(PluginsNotFound); | 
|---|
|  | 132 | CREATE_ORXONOX_EXCEPTION(InitialisationFailed); | 
|---|
|  | 133 | CREATE_ORXONOX_EXCEPTION(NotImplemented); | 
|---|
|  | 134 | CREATE_ORXONOX_EXCEPTION(GameState); | 
|---|
| [2171] | 135 | CREATE_ORXONOX_EXCEPTION(NoGraphics); | 
|---|
|  | 136 | CREATE_ORXONOX_EXCEPTION(AbortLoading); | 
|---|
| [3196] | 137 | #endif | 
|---|
| [1638] | 138 |  | 
|---|
| [2662] | 139 | /** | 
|---|
|  | 140 | @brief | 
|---|
| [3196] | 141 | Helper function that forwards an exception and displays the message. | 
|---|
|  | 142 | @details | 
|---|
|  | 143 | This is necessary because only when using 'throw' the objects storage is managed. | 
|---|
| [2662] | 144 | */ | 
|---|
|  | 145 | template <class T> | 
|---|
| [3196] | 146 | inline const T& exceptionThrowerHelper(const T& exception) | 
|---|
| [2662] | 147 | { | 
|---|
|  | 148 | // let the catcher decide whether to display the message below level 4 | 
|---|
|  | 149 | COUT(4) << exception.getFullDescription() << std::endl; | 
|---|
|  | 150 | return exception; | 
|---|
|  | 151 | } | 
|---|
| [1638] | 152 |  | 
|---|
| [3196] | 153 | /** | 
|---|
|  | 154 | @brief | 
|---|
|  | 155 | Throws an exception and logs a message beforehand. | 
|---|
|  | 156 | @param type | 
|---|
|  | 157 | Type of the exception as literal (General, Initialisation, etc.) | 
|---|
|  | 158 | @param description | 
|---|
|  | 159 | Exception description as string | 
|---|
|  | 160 | */ | 
|---|
| [3280] | 161 | #define ThrowException(type, description) \ | 
|---|
| [3196] | 162 | throw orxonox::exceptionThrowerHelper(type##Exception(static_cast<std::ostringstream&>(std::ostringstream().flush() << description).str(), __LINE__, __FILE__, __FUNCTIONNAME__)) | 
|---|
| [2662] | 163 |  | 
|---|
| [3196] | 164 | } /* namespace orxonox */ | 
|---|
| [1642] | 165 |  | 
|---|
| [1638] | 166 | #endif /* _Exception_H__ */ | 
|---|