Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

rs_assert.h

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright (C) 2000
00004 // Ralf Westram
00005 // (Coded@ReallySoft.de)
00006 //
00007 // Permission to use, copy, modify, distribute and sell this software
00008 // and its documentation for any purpose is hereby granted without fee,
00009 // provided that the above copyright notice appear in all copies and
00010 // that both that copyright notice and this permission notice appear
00011 // in supporting documentation.  Ralf Westram makes no
00012 // representations about the suitability of this software for any
00013 // purpose.  It is provided "as is" without express or implied warranty.
00014 //
00015 // This code is part of my library.
00016 // You may find a more recent version at http://www.reallysoft.de/
00017 //
00019 
00020 #ifndef RS_ASSERT_H
00021 #define RS_ASSERT_H
00022 
00023 #ifndef __STDEXCEPT__
00024 #include <stdexcept>
00025 #endif
00026 #ifndef __CASSERT__
00027 #include <cassert>
00028 #endif
00029 
00030 #ifndef RSDEF_H
00031 #include "rsdef.h"
00032 #endif
00033 
00034 namespace rs {
00035 
00043     class Terminator {
00044     private:
00045 
00046     public:
00047         Terminator();
00048         virtual ~Terminator();
00049     };
00050 
00069     void handle_exception(std::exception& e);
00070 };
00071 
00072 // remove assert from <cassert>
00073 #undef assert
00074 
00075 #if defined(DEBUG)
00076 
00077 #define RAISE_EXCEPTION *(char*)0 = 0
00078 
00079 namespace rs {
00081     inline void guard() {
00082         std::cout << "guard\n";
00083     }
00084 };
00085 
00086 #define blub(cond)                                                                      \
00087 do {                                                                                    \
00088     if (!(cond)) { std::cerr << rs::assertionText(#cond, __FILE__, __LINE__) << "\n";   \
00089     rs::guard();                                                                        \
00090     RAISE_EXCEPTION;                                                                    \
00091     exit(1);                                                                            \
00092     }                                                                                   \
00093 } while (0)
00094 
00095 #define assert(bed) blub(bed)
00096 
00097 #define hassert(bed) assert(bed)
00098 
00099 #define GUARDED try {
00100 #define UNGUARDED }                                             \
00101 catch (exception& e) {                                          \
00102     try {                                                       \
00103         reThrow(e);                                             \
00104     }                                                           \
00105     catch (Error& err) {                                        \
00106         cerr << "Guard sees exception: " << err.get_message()   \
00107               << " " << __FILE__ << '#' << __LINE__ << endl;    \
00108         assert(0);                                              \
00109     }                                                           \
00110 }                                                               \
00111 catch (...) {                                                   \
00112     cerr << "Guard sees unknown exception"                      \
00113          << " " << __FILE__ << '#' << __LINE__ << endl;         \
00114     assert(0);                                                  \
00115 }
00116 
00117 #else // !DEBUG
00118 
00120 #define assert(bed)
00121 
00127 #define hassert(bed) do { if (!(bed)) throw std::runtime_error(rs::assertionText(#bed, __FILE__, __LINE__)); } while(0)
00128 
00129 #define GUARDED {
00130 #define UNGUARDED }
00131 
00132 #endif // DEBUG
00133 
00135 template <class T> const T& nonnull(const T& t) {
00136     if (t == 0) hassert(t);
00137     return t;
00138 }
00139 template <class T> T& nonnull(T& t) {
00140     if (t == 0) hassert(t);
00141     return t;
00142 }
00143 
00144 #else
00145 #error rs_assert.h included twice
00146 #endif // RS_ASSERT_H
00147 

Contact me in case of errors or questions.
This documentation is powered by Doxygen.
(C) 2000-2002 Doxygen