Assertion.h

00001 /*
00002  * Assertion.h - design-by-contract, assertions.
00003  *
00004  * This file is part of the Rulbus Device Class Library (RDCL).
00005  *
00006  * Copyright (C) 2003-2004, Leiden University.
00007  *
00008  * This library is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * The library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with mngdriver; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * $Id: Assertion.h 2 2004-10-12 11:54:15Z moene $
00023  */
00024 
00025 #ifndef __ASSERTION_H
00026 #define __ASSERTION_H
00027 
00028 #ifndef __IN_COMMON_H
00029    #error assertion.h is included by common.h only.
00030 #endif
00031 
00032 #include <iosfwd>       // for std::ostream
00033 #include <stdexcept>    // for std::exception
00034 
00074 #define LOCATION __FILE__, __LINE__
00075 
00091 #if DEBUG & DFLAG_REQUIRE
00092    #define require(exp)           \
00093       if (!(exp))                 \
00094          throw Rulbus::RequireException( _T(#exp), LOCATION )
00095 
00096    #define require_e(exp, ecxpt)  \
00097       if (!(exp))                 \
00098          throw excpt
00099 #else
00100    #define require(exp)
00101    #define require_e(exp)
00102 #endif // DEBUG & DFLAG_REQUIRE
00103 
00114 #if DEBUG & DFLAG_ENSURE
00115    #define ensure(exp)          \
00116       if (!(exp))               \
00117          throw Rulbus::EnsureException( _T(#exp), LOCATION )
00118 
00119    #define ensure_e(exp, ecxpt) \
00120       if (!(exp))               \
00121          throw excpt
00122 #else
00123    #define ensure(exp)
00124    #define ensure_e(exp, ecxpt)
00125 #endif // DEBUG & DFLAG_ENSURE
00126 
00132 #if DEBUG & DFLAG_NEVER_GET_HERE
00133    #define NEVER_GET_HERE  throw Rulbus::NeverGetHereException( LOCATION )
00134 #else
00135    #define NEVER_GET_HERE
00136 #endif
00137 
00142 #define BASE_INVARIANT( base )  base::invariant()
00143 
00145 
00146 namespace Rulbus
00147 {
00148    DECLARE_CLASS( Assertion );                  
00149    DECLARE_CLASS( DbcException );               
00150    DECLARE_CLASS( RequireException );           
00151    DECLARE_CLASS( EnsureException );            
00152    DECLARE_CLASS( AssertException );            
00153    DECLARE_CLASS( NeverGetHereException );      
00154 
00165    class Assertion : public StdException
00166    {
00167    public:
00170 
00171       Assertion( CharCptr aReason, CharCptr aFile, int aLine );
00172 
00173       Assertion( AssertionCref rhs );
00174 
00175       virtual ~Assertion();
00176 
00180 
00181       AssertionRef operator=( AssertionCref rhs );
00182 
00183       bool operator==( AssertionCref rhs );
00184 
00188 
00189       virtual CharCptr what() const;
00190 
00191       String  reason() const;
00192       String  file() const;
00193       int     line() const;
00194 
00196 
00197    protected:
00198       String  theReason;    
00199       String  theFile;      
00200       int     theLine;      
00201    };
00202 
00207    inline Assertion::Assertion( CharCptr aReason, CharCptr aFile, int aLine ):
00208       theReason( aReason ), theFile( aFile ), theLine( aLine )
00209    {
00210       ; // do nothing
00211    }
00212 
00217    inline Assertion::Assertion( AssertionCref rhs ):
00218       theReason( rhs.theReason ), theFile( rhs.theFile ), theLine( rhs.theLine )
00219    {
00220       ; // do nothing
00221    }
00222 
00227    inline Assertion::~Assertion()
00228    {
00229       ; // do nothing
00230    }
00231 
00236    inline AssertionRef Assertion::operator=( AssertionCref rhs )
00237    {
00238       if( this != &rhs )
00239       {
00240          theReason = rhs.theReason;
00241          theFile = rhs.theFile;
00242          theLine = rhs.theLine;
00243       }
00244       else
00245       {
00246          ;  // do nothing
00247       }
00248 
00249       return *this;
00250    }
00251 
00256    inline bool Assertion::operator==( AssertionCref rhs )
00257    {
00258       return theReason == rhs.reason() &&
00259              theFile   == rhs.file()   &&
00260              theLine   == rhs.line();
00261    }
00262 
00267    inline String Assertion::reason() const
00268    {
00269       return theReason;
00270    }
00271 
00276    inline String Assertion::file() const
00277    {
00278       return theFile;
00279    }
00280 
00285    inline int Assertion::line() const
00286    {
00287       return theLine;
00288    }
00289 
00294    inline CharCptr Assertion::what() const
00295    {
00296       return reason().c_str();
00297    }
00298 
00309    class DbcException : public Assertion
00310    {
00311    public:
00316       enum Type
00317       {
00318          REQUIRE,
00319          ENSURE,
00320          ASSERT,
00321          NEVERGETHERE,
00322       };
00323 
00326 
00327       DbcException( Type aType, CharCptr aDescription, CharCptr aExpression, CharCptr aFile, int aLine );
00328 
00329       DbcException( DbcExceptionCref rhs );
00330 
00331       virtual ~DbcException();
00332 
00336 
00337       DbcExceptionRef operator=( DbcExceptionCref rhs );
00338 
00339       bool operator==( DbcExceptionCref rhs );
00340 
00344 
00345       virtual CharCptr what() const;
00346 
00347       Type    type() const;
00348       String  description() const;
00349 
00351 
00352    private:
00353       Type    theType;                  
00354       String  theDescription;           
00355       mutable String theMessage;        
00356    };
00357 
00362    inline DbcException::DbcException( DbcException::Type aType, CharCptr aDescription, CharCptr aExpression, CharCptr aFile, int aLine ):
00363       Assertion( aExpression, aFile, aLine ), theType( aType ), theDescription( aDescription )
00364    {
00365       ; // do nothing
00366    }
00367 
00372    inline DbcException::DbcException( DbcExceptionCref rhs ):
00373       Assertion( rhs ), theType( rhs.type() ), theDescription( rhs.description() )
00374    {
00375       ; // do nothing
00376    }
00377 
00382    inline DbcException::~DbcException()
00383    {
00384       ; // do nothing
00385    }
00386 
00391    inline DbcExceptionRef DbcException::operator=( DbcExceptionCref rhs )
00392    {
00393       if( this != &rhs )
00394       {
00395          (void) Assertion::operator=( rhs );
00396          theType = rhs.type();
00397          theDescription = rhs.description();
00398       }
00399       else
00400       {
00401          ;  // do nothing
00402       }
00403 
00404       return *this;
00405    }
00406 
00411    inline bool DbcException::operator==( DbcExceptionCref rhs )
00412    {
00413       return Assertion::operator==( rhs ) &&
00414                          type() == rhs.type() &&
00415                   description() == rhs.description();
00416    }
00417 
00422    inline DbcException::Type DbcException::type() const
00423    {
00424       return theType;
00425    }
00426 
00431    inline String DbcException::description() const
00432    {
00433       return theDescription;
00434    }
00435 
00444    class RequireException : public DbcException
00445    {
00446    public:
00448       RequireException( CharCptr aExpression, CharCptr aFile, int aLine ) :
00449          DbcException( REQUIRE, "require", aExpression, aFile, aLine ) { ; }
00450    };
00451 
00460    class EnsureException : public DbcException
00461    {
00462    public:
00464       EnsureException( CharCptr aExpression, CharCptr aFile, int aLine ) :
00465          DbcException( ENSURE, "promised", aExpression, aFile, aLine ) { ; }
00466    };
00467 
00476    class AssertException : public DbcException
00477    {
00478    public:
00480       AssertException( CharCptr aExpression, CharCptr aFile, int aLine ) :
00481          DbcException( ASSERT, "assert", aExpression, aFile, aLine ) { ; }
00482    };
00483 
00493    class NeverGetHereException : public DbcException
00494    {
00495    public:
00497       NeverGetHereException( CharCptr aFile, int aLine ) :
00498          DbcException( NEVERGETHERE, "shall never get here", "", aFile, aLine ) { ; }
00499    };
00500 
00501 } // namespace Rulbus
00502 
00508 std::ostream& operator<<( std::ostream& stream, Rulbus::StdExceptionCref rhs );
00509 
00510 #endif  // __ASSERTION_H
00511 
00512 /*
00513  * end of file
00514  */
00515 

Generated on Tue Oct 12 14:11:57 2004 for Rulbus Device Class Library for Microsoft Windows by doxygen 1.3.4