00001 /** 00002 * \file FailureHandler.h \brief Handle actions on check failures. 00003 * 00004 * Copyright 2007-2010 IMP Inventors. All rights reserved. 00005 * 00006 */ 00007 00008 #ifndef IMP_FAILURE_HANDLER_H 00009 #define IMP_FAILURE_HANDLER_H 00010 00011 #include "Object.h" 00012 #include "exception.h" 00013 00014 00015 IMP_BEGIN_NAMESPACE 00016 00017 00018 /** A class to handle actions to take on check failures. 00019 The function is called when an assertion or check fails. 00020 \ingroup assert 00021 Implementors should see IMP_FAILURE_HANDLER(). 00022 */ 00023 class IMPEXPORT FailureHandler: public Object { 00024 public: 00025 FailureHandler(); 00026 virtual void handle_failure()=0; 00027 IMP_REF_COUNTED_DESTRUCTOR(FailureHandler); 00028 }; 00029 00030 00031 //! A base class for setting up failure handlers 00032 /** The only function you have to implement is handle_failure. 00033 \untested{FailureHandlerBase} 00034 \unstable{FailureHandlerBase} 00035 */ 00036 class FailureHandlerBase: public FailureHandler { 00037 public: 00038 void show(std::ostream &out=std::cout) const { 00039 out << "Temporary FailureHandler" << std::endl; 00040 } 00041 VersionInfo get_version_info() const { 00042 return VersionInfo(); 00043 } 00044 IMP_REF_COUNTED_DESTRUCTOR(FailureHandlerBase); 00045 }; 00046 00047 00048 //! Add a custom function to be called on an error 00049 /** \relatesalso FailureHandler 00050 \ingroup assert 00051 */ 00052 IMPEXPORT void add_failure_handler(FailureHandler *f); 00053 00054 //! Remove a failure handler from the list 00055 /** \relatesalso FailureHandler 00056 \ingroup assert 00057 */ 00058 IMPEXPORT void remove_failure_handler(FailureHandler *f); 00059 00060 00061 //! Control a scope-dependent failure handler 00062 /** The failure handler is added on construction and removed 00063 on destruction. 00064 */ 00065 class ScopedFailureHandler: public RAII { 00066 FailureHandler* fh_; 00067 public: 00068 IMP_RAII(ScopedFailureHandler, (FailureHandler *fh), 00069 {fh_=NULL;}, 00070 { 00071 fh_=fh; 00072 if (fh_) add_failure_handler(fh_); 00073 }, 00074 { 00075 if (fh_) remove_failure_handler(fh_); 00076 fh_=NULL; 00077 } 00078 ) 00079 }; 00080 00081 IMP_OBJECTS(FailureHandler); 00082 00083 IMP_END_NAMESPACE 00084 00085 #endif /* IMP_FAILURE_HANDLER_H */