Go to the documentation of this file.00001 #ifndef QPID_EXCEPTIONHOLDER_H
00002 #define QPID_EXCEPTIONHOLDER_H
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 #include <boost/shared_ptr.hpp>
00026 
00027 
00028 namespace qpid {
00029 namespace sys {
00030 
00031 struct Raisable {
00032     virtual ~Raisable() {};
00033     virtual void raise() const=0;
00034     virtual std::string what() const=0;
00035 };
00036 
00041 class ExceptionHolder : public Raisable {
00042   public:
00043     ExceptionHolder() {}
00044     
00045 
00047     template <class Ex> ExceptionHolder(Ex* ex) { wrap(ex); }
00048     template <class Ex> ExceptionHolder& operator=(Ex* ex) { wrap(ex); return *this; }
00049 
00050     void raise() const { if (wrapper.get()) wrapper->raise() ; }
00051     std::string what() const { return wrapper.get() ? wrapper->what() : std::string(); }
00052     bool empty() const { return !wrapper.get(); }
00053     operator bool() const { return !empty(); }
00054     void reset() { wrapper.reset(); }
00055 
00056   private:
00057     template <class Ex> struct Wrapper : public Raisable {
00058         Wrapper(Ex* ptr) : exception(ptr) {}
00059         void raise() const { throw *exception; }
00060         std::string what() const { return exception->what(); }
00061         boost::shared_ptr<Ex> exception;
00062     };
00063     template <class Ex> void wrap(Ex* ex) { wrapper.reset(new Wrapper<Ex>(ex)); }
00064     boost::shared_ptr<Raisable> wrapper;
00065 };
00066 
00067 
00068 }} 
00069 
00070 
00071 #endif