Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 20, 2009, 9:20:47 AM (16 years ago)
Author:
rgrieder
Message:

Merged pch branch back to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/util/Exception.h

    r3068 r3196  
    3030@file
    3131@brief
    32     Declaration of the Exception class.
     32    Declaration of facilities to handle exceptions.
    3333*/
    3434
     
    3838#include "UtilPrereqs.h"
    3939
     40#include <exception>
     41#include <sstream>
    4042#include <string>
    41 #include <exception>
    42 #include <cassert>
    4343#include "Debug.h"
    4444
    4545namespace orxonox
    4646{
     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    */
    4754    class _UtilExport Exception : public std::exception
    4855    {
    4956    public:
    50 
    51         Exception(const std::string& description, int lineNumber,
     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,
    5264                  const char* filename, const char* functionName);
     65        //! Simplified constructor with just a description. If you need more, use the other one.
    5366        Exception(const std::string& description);
    5467
    55         /// Needed for  compatibility with std::exception (from Ogre::Exception)
     68        //! Needed for compatibility with std::exception
    5669        virtual ~Exception() throw() { }
    5770
     71        //! Returns a full description with type, line, file and function
    5872        virtual const std::string& getFullDescription() const;
     73        //! Returns the string name of the exception type
    5974        virtual std::string        getTypeName()        const = 0;
     75        //! Returns the short developer written exception
    6076        virtual const std::string& getDescription()     const { return this->description_; }
    61         virtual const int          getLineNumber()      const { return this->lineNumber_; }
     77        //! Returns the line number on which the exception occurred.
     78        virtual const unsigned int getLineNumber()      const { return this->lineNumber_; }
     79        //! Returns the function in which the exception occurred.
    6280        virtual const std::string& getFunctionName()    const { return this->functionName_; }
     81        //! Returns the filename in which the exception occurred.
     82        virtual const std::string& getFilename()        const { return this->filename_; }
    6383
    64         /// Override std::exception::what (from Ogre::Exception)
     84        //! Returns a full description of the error.
    6585        const char* what() const throw() { return getFullDescription().c_str(); }
    6686
    6787    protected:
    68         std::string description_;
    69         int lineNumber_;
    70         std::string functionName_;
    71         std::string filename_;
     88        std::string description_;             //!< User typed text about why the exception occurred
     89        unsigned int lineNumber_;             //!< Line on which the exception occurred
     90        std::string functionName_;            //!< Function (including namespace and class) where the exception occurred
     91        std::string filename_;                //!< File where the exception occurred
    7292        // mutable because "what()" is a const method
    73         mutable std::string fullDescription_;
     93        mutable std::string fullDescription_; //!< Full description with line, file and function
    7494    };
    7595
    76 
     96//! Creates a new type of exception that inherits from tracker::Exception
    7797#define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \
    7898    class ExceptionName##Exception : public Exception                               \
    7999    {                                                                               \
    80100    public:                                                                         \
    81         ExceptionName##Exception(const std::string& description, int lineNumber,    \
    82                   const char* filename, const char* functionName)                   \
    83                   : Exception(description, lineNumber, filename, functionName)      \
     101        ExceptionName##Exception(const std::string& description,                    \
     102                unsigned int lineNumber, const char* filename,                      \
     103                const char* functionName)                                           \
     104            : Exception(description, lineNumber, filename, functionName)            \
    84105        { }                                                                         \
    85106                                                                                    \
    86107        ExceptionName##Exception(const std::string& description)                    \
    87                   : Exception(description)                                          \
     108            : Exception(description)                                                \
    88109        { }                                                                         \
    89110                                                                                    \
     
    91112                                                                                    \
    92113        std::string getTypeName() const { return #ExceptionName; }                  \
    93     };
     114    }
    94115
    95116    // Creates all possible exception types.
    96117    // If you want to add a new type, simply copy and adjust a new line here.
     118#ifndef DOXYGEN_SHOULD_SKIP_THIS
    97119    CREATE_ORXONOX_EXCEPTION(General);
    98120    CREATE_ORXONOX_EXCEPTION(FileNotFound);
     
    106128    CREATE_ORXONOX_EXCEPTION(NoGraphics);
    107129    CREATE_ORXONOX_EXCEPTION(AbortLoading);
    108 }
     130#endif
    109131
    110132    /**
    111133    @brief
    112         Helper function that creates an exception, displays the message, but doesn't throw it.
     134        Helper function that forwards an exception and displays the message.
     135    @details
     136        This is necessary because only when using 'throw' the objects storage is managed.
    113137    */
    114138    template <class T>
    115     inline const T& InternalHandleException(const T& exception)
     139    inline const T& exceptionThrowerHelper(const T& exception)
    116140    {
    117141        // let the catcher decide whether to display the message below level 4
     
    120144    }
    121145
    122 #define ThrowException(type, description) \
    123     throw InternalHandleException(type##Exception(description, __LINE__, __FILE__, __FUNCTIONNAME__))
     146/**
     147@brief
     148    Throws an exception and logs a message beforehand.
     149@param type
     150    Type of the exception as literal (General, Initialisation, etc.)
     151@param description
     152    Exception description as string
     153*/
     154#define ThrowException(type, description, ...) \
     155    throw orxonox::exceptionThrowerHelper(type##Exception(static_cast<std::ostringstream&>(std::ostringstream().flush() << description).str(), __LINE__, __FILE__, __FUNCTIONNAME__))
    124156
    125     // define an assert macro that can display a message
    126 #ifndef NDEBUG
    127 #define OrxAssert(Assertion, ErrorMessage) \
    128     Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << ErrorMessage << std::endl); \
    129     assert(Assertion)
    130 #else
    131 #define OrxAssert(condition, errorMessage)  ((void)0)
    132 #endif
     157} /* namespace orxonox */
    133158
    134159#endif /* _Exception_H__ */
Note: See TracChangeset for help on using the changeset viewer.