Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 10, 2008, 12:05:03 AM (16 years ago)
Author:
landauf
Message:

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

Location:
code/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/util

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

    r2103 r2171  
    4343#include "Debug.h"
    4444
    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 
    5245namespace orxonox
    5346{
     
    5548    {
    5649    public:
    57         enum ExceptionType
    58         {
    59             General,
    60             FileNotFound,
    61             Argument,
    62             PluginsNotFound,
    63             InitialisationFailed,
    64             NotImplemented,
    65             GameState
    66         };
    6750
    6851        Exception(const std::string& description, int lineNumber,
     
    7457
    7558        virtual const std::string& getFullDescription() const;
    76         virtual ExceptionType      getType()            const = 0;
    7759        virtual std::string        getTypeName()        const = 0;
    7860        virtual const std::string& getDescription()     const { return this->description_; }
     
    9375
    9476
    95     template <Exception::ExceptionType Type>
    96     class SpecificException : public Exception
    97     {
    98     public:
    99         SpecificException(const std::string& description, int lineNumber,
    100                   const char* filename, const char* functionName)
    101                   : Exception(description, lineNumber, filename, functionName)
    102         {
    103             // let the catcher decide whether to display the message below level 4
    104             COUT(4) << this->getFullDescription() << std::endl;
    105         }
    106 
    107         SpecificException(const std::string& description)
    108             : Exception(description)
    109         {
    110             // let the catcher decide whether to display the message below level 4
    111             COUT(4) << this->getFullDescription() << std::endl;
    112         }
    113 
    114         ~SpecificException() throw() { }
    115 
    116         ExceptionType getType() const { return Type; }
    117         std::string getTypeName() const
    118         {
    119             // note: break is not necessary due to the return in the macros.
    120             switch (Type)
    121             {
    122             RETURN_EXCEPTION_CODE(General)
    123             RETURN_EXCEPTION_CODE(FileNotFound);
    124             RETURN_EXCEPTION_CODE(Argument);
    125             RETURN_EXCEPTION_CODE(PluginsNotFound);
    126             RETURN_EXCEPTION_CODE(InitialisationFailed);
    127             RETURN_EXCEPTION_CODE(NotImplemented);
    128             RETURN_EXCEPTION_CODE(GameState);
    129             default:
    130                 return "";
    131             }
    132         }
     77#define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \
     78    class ExceptionName##Exception : public Exception                               \
     79    {                                                                               \
     80    public:                                                                         \
     81        ExceptionName##Exception(const std::string& description, int lineNumber,    \
     82                  const char* filename, const char* functionName)                   \
     83                  : Exception(description, lineNumber, filename, functionName)      \
     84        {                                                                           \
     85            /* Let the catcher decide whether to display the message below level 4  \
     86               Note: Don't place this code in Exception c'tor because getTypeName() \
     87               is still pure virtual at that time. */                               \
     88            COUT(4) << this->getFullDescription() << std::endl;                     \
     89        }                                                                           \
     90                                                                                    \
     91        ExceptionName##Exception(const std::string& description)                    \
     92                  : Exception(description)                                          \
     93        { COUT(4) << this->getFullDescription() << std::endl; }                     \
     94                                                                                    \
     95        ~ExceptionName##Exception() throw() { }                                     \
     96                                                                                    \
     97        std::string getTypeName() const { return #ExceptionName; }                  \
    13398    };
    13499
    135     // define the template spcialisations
     100    // Creates all possible exception types.
     101    // If you want to add a new type, simply copy and adjust a new line here.
    136102    CREATE_ORXONOX_EXCEPTION(General);
    137103    CREATE_ORXONOX_EXCEPTION(FileNotFound);
     
    141107    CREATE_ORXONOX_EXCEPTION(NotImplemented);
    142108    CREATE_ORXONOX_EXCEPTION(GameState);
     109    CREATE_ORXONOX_EXCEPTION(NoGraphics);
     110    CREATE_ORXONOX_EXCEPTION(AbortLoading);
     111}
    143112
    144 #define ThrowException(type, description) \
    145     throw SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__)
     113#define ThrowException(Type, Description) \
     114    throw Type##Exception(Description, __LINE__, __FILE__, __FUNCTIONNAME__)
    146115
    147116    // define an assert macro that can display a message
    148117#ifndef NDEBUG
    149 #define OrxAssert(assertion, errorMessage) \
    150     assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \
    151     assert(assertion)
     118#define OrxAssert(Assertion, ErrorMessage) \
     119    Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << ErrorMessage << std::endl); \
     120    assert(Assertion)
    152121#else
    153122#define OrxAssert(condition, errorMessage)  ((void)0)
    154123#endif
    155124
    156 }
    157 
    158125#endif /* _Exception_H__ */
Note: See TracChangeset for help on using the changeset viewer.