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

rs_assert.cpp

Go to the documentation of this file.
00001 //  ======================================================================== //
00002 //                                                                           //
00003 //    File      : rs_assert.cpp                                              //
00004 //    Purpose   :                                                            //
00005 //    Time-stamp: <Wed May/08/2002 20:31 MET Coder@ReallySoft.de>            //
00006 //                                                                           //
00007 //    (C) May 2002 by Ralf Westram                                           //
00008 //                                                                           //
00009 //    Permission to use, copy, modify, distribute and sell this software     //
00010 //    and its documentation for any purpose is hereby granted without fee,   //
00011 //    provided that the above copyright notice appear in all copies and      //
00012 //    that both that copyright notice and this permission notice appear      //
00013 //    in supporting documentation.                                           //
00014 //                                                                           //
00015 //    Ralf Westram makes no representations about the suitability of this    //
00016 //    software for any purpose.  It is provided "as is" without express or   //
00017 //    implied warranty.                                                      //
00018 //                                                                           //
00019 //  ======================================================================== //
00020 
00021 #include "rs_assert.h"
00022 
00023 #include <Err.h>
00024 #include <Tools.h>
00025 #include <stdexcept>
00026 
00027 
00028 using namespace std;
00029 using namespace rs;
00030 
00031 static bool              initialized   = false;
00032 static terminate_handler old_terminate = 0;
00033 
00034 using namespace rs;
00035 using namespace rs::err;
00036 using namespace rs::str;
00037 
00038 namespace rs {
00039     // ---------------------------------------------
00040     //      void handle_exception(exception& e)
00041     // ---------------------------------------------
00042     void handle_exception(exception& e) {
00043         Error *error = dynamic_cast<Error*>(&e);
00044 
00045         if (error) {
00046             error->print(cerr);
00047         }
00048         else {
00049             try {
00050                 reThrow(e);
00051             }
00052             catch (Error& E) { E.print(cerr); }
00053             catch (...) {
00054                 InternalError E(strf("unknown exception: %s", e.what()));
00055                 E.print(cerr);
00056             }
00057         }
00058     }
00059 };
00060 
00061 // --------------------------------------
00062 //      static void real_terminate()
00063 // --------------------------------------
00064 static void real_terminate() {
00065 #if defined(DEBUG)
00066     cerr << "Program terminating..\n";
00067 #endif // DEBUG
00068     if (uncaught_exception()) {
00069         cerr << "An uncaught exception occurred.\n\n"
00070              << "Maybe you forgot to put the following code into your main():\n\n"
00071              << "try {\n"
00072              << " // your stuff\n"
00073              << " return EXIT_SUCCESS;\n"
00074              << "}\n"
00075              << "catch (exception& e) { handle_exception(e); }\n"
00076              << "catch (...) { Error e(\"Unknown exception\"); e.print(cerr); }\n"
00077              << "return EXIT_FAILURE;\n"
00078             ;
00079     }
00080 
00081     old_terminate();
00082 }
00083 
00084 // start of implementation of class Terminator:
00085 
00086 // ---------------------------------
00087 //      Terminator::Terminator()
00088 // ---------------------------------
00089 Terminator::Terminator()
00090 {
00091     hassert(!initialized);
00092     initialized   = true;
00093     old_terminate = set_terminate(real_terminate);
00094 }
00095 
00096 // ----------------------------------
00097 //      Terminator::~Terminator()
00098 // ----------------------------------
00099 Terminator::~Terminator()
00100 {
00101     set_terminate(old_terminate);
00102 }
00103 
00104 // -end- of implementation of class Terminator.
00105 

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