Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2162


Ignore:
Timestamp:
Nov 9, 2008, 11:08:24 AM (15 years ago)
Author:
rgrieder
Message:
  • Simplified creation of a new Exception type (only one line to add now)
  • Replaced newly created "trow TypeException("description")" with "ThrowException(Type, "description")". That adds line number, file name and function name to the full message.
Location:
code/branches/objecthierarchy/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/Camera.cc

    r2161 r2162  
    5353
    5454        if (!this->getScene() || !this->getScene()->getSceneManager())
    55             throw AbortLoadingException("Can't create Camera, no scene or no scene manager given.");
     55            ThrowException(AbortLoading, "Can't create Camera, no scene or no scene manager given.");
    5656
    5757        this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString());
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/ParticleEmitter.cc

    r2161 r2162  
    5151
    5252        if (!this->getScene() || !this->getScene()->getSceneManager())
    53             throw AbortLoadingException("Can't create Camera, no scene or no scene manager given.");
     53            ThrowException(AbortLoading, "Can't create Camera, no scene or no scene manager given.");
    5454
    5555        this->particles_ = 0;
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/WorldEntity.cc

    r2161 r2162  
    5454
    5555        if (!this->getScene() || !this->getScene()->getRootSceneNode())
    56             throw AbortLoadingException("Can't create WorldEntity, no scene or no root-scenenode given.");
     56            ThrowException(AbortLoading, "Can't create WorldEntity, no scene or no root-scenenode given.");
    5757
    5858        this->node_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
  • code/branches/objecthierarchy/src/orxonox/overlays/OrxonoxOverlay.cc

    r2161 r2162  
    6262
    6363        if (!Core::showsGraphics())
    64             throw NoGraphicsException("Can't create OrxonoxOverlay, graphics engine not initialized");
     64            ThrowException(NoGraphics, "Can't create OrxonoxOverlay, graphics engine not initialized");
    6565
    6666        // add this overlay to the static map of OrxonoxOverlays
  • code/branches/objecthierarchy/src/util/Exception.h

    r2161 r2162  
    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             NoGraphics,
    67             AbortLoading
    68         };
    6950
    7051        Exception(const std::string& description, int lineNumber,
     
    7657
    7758        virtual const std::string& getFullDescription() const;
    78         virtual ExceptionType      getType()            const = 0;
    7959        virtual std::string        getTypeName()        const = 0;
    8060        virtual const std::string& getDescription()     const { return this->description_; }
     
    9575
    9676
    97     template <Exception::ExceptionType Type>
    98     class SpecificException : public Exception
    99     {
    100     public:
    101         SpecificException(const std::string& description, int lineNumber,
    102                   const char* filename, const char* functionName)
    103                   : Exception(description, lineNumber, filename, functionName)
    104         {
    105             // let the catcher decide whether to display the message below level 4
    106             COUT(4) << this->getFullDescription() << std::endl;
    107         }
    108 
    109         SpecificException(const std::string& description)
    110             : Exception(description)
    111         {
    112             // let the catcher decide whether to display the message below level 4
    113             COUT(4) << this->getFullDescription() << std::endl;
    114         }
    115 
    116         ~SpecificException() throw() { }
    117 
    118         ExceptionType getType() const { return Type; }
    119         std::string getTypeName() const
    120         {
    121             // note: break is not necessary due to the return in the macros.
    122             switch (Type)
    123             {
    124             RETURN_EXCEPTION_CODE(General)
    125             RETURN_EXCEPTION_CODE(FileNotFound);
    126             RETURN_EXCEPTION_CODE(Argument);
    127             RETURN_EXCEPTION_CODE(PluginsNotFound);
    128             RETURN_EXCEPTION_CODE(InitialisationFailed);
    129             RETURN_EXCEPTION_CODE(NotImplemented);
    130             RETURN_EXCEPTION_CODE(GameState);
    131             RETURN_EXCEPTION_CODE(NoGraphics);
    132             RETURN_EXCEPTION_CODE(AbortLoading);
    133             default:
    134                 return "";
    135             }
    136         }
     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; }                  \
    13798    };
    13899
    139     // 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.
    140102    CREATE_ORXONOX_EXCEPTION(General);
    141103    CREATE_ORXONOX_EXCEPTION(FileNotFound);
     
    149111}
    150112
    151 #define ThrowException(type, description) \
    152     throw SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__)
     113#define ThrowException(Type, Description) \
     114    throw Type##Exception(Description, __LINE__, __FILE__, __FUNCTIONNAME__)
    153115
    154116    // define an assert macro that can display a message
    155117#ifndef NDEBUG
    156 #define OrxAssert(assertion, errorMessage) \
    157     assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \
    158     assert(assertion)
     118#define OrxAssert(Assertion, ErrorMessage) \
     119    Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << ErrorMessage << std::endl); \
     120    assert(Assertion)
    159121#else
    160122#define OrxAssert(condition, errorMessage)  ((void)0)
Note: See TracChangeset for help on using the changeset viewer.