123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
- //Distributed under the Boost Software License, Version 1.0. (See accompanying
- //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- #ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593
- #define UUID_FA5836A2CADA11DC8CD47C8555D89593
- #include <boost/config.hpp>
- #ifdef BOOST_NO_EXCEPTIONS
- #error This header requires exception handling to be enabled.
- #endif
- #include <boost/exception/exception.hpp>
- #include <boost/exception/info.hpp>
- #include <boost/exception/diagnostic_information.hpp>
- #include <boost/exception/detail/type_info.hpp>
- #include <boost/shared_ptr.hpp>
- #include <stdexcept>
- #include <new>
- #include <ios>
- namespace
- boost
- {
- #ifndef BOOST_NO_RTTI
- typedef error_info<struct tag_original_exception_type,std::type_info const *> original_exception_type;
- inline
- std::string
- to_string( original_exception_type const & x )
- {
- return x.value()->name();
- }
- #endif
- class exception_ptr;
- exception_ptr current_exception();
- void rethrow_exception( exception_ptr const & );
- class
- exception_ptr:
- public exception_detail::exception_ptr_base
- {
- typedef bool exception_ptr::*unspecified_bool_type;
- friend exception_ptr current_exception();
- friend void rethrow_exception( exception_ptr const & );
- shared_ptr<exception_detail::clone_base const> c_;
- bool bad_alloc_;
- struct
- bad_alloc_tag
- {
- };
- explicit
- exception_ptr( bad_alloc_tag ):
- bad_alloc_(true)
- {
- }
- explicit
- exception_ptr( shared_ptr<exception_detail::clone_base const> const & c ):
- c_(c),
- bad_alloc_(false)
- {
- BOOST_ASSERT(c);
- }
- void
- _rethrow() const
- {
- BOOST_ASSERT(*this);
- if( bad_alloc_ )
- throw enable_current_exception(std::bad_alloc());
- else
- c_->rethrow();
- }
- bool
- _empty() const
- {
- return !bad_alloc_ && !c_;
- }
- public:
- exception_ptr():
- bad_alloc_(false)
- {
- }
- operator unspecified_bool_type() const
- {
- return _empty() ? 0 : &exception_ptr::bad_alloc_;
- }
- friend
- bool
- operator==( exception_ptr const & a, exception_ptr const & b )
- {
- return a.c_==b.c_ && a.bad_alloc_==b.bad_alloc_;
- }
- friend
- bool
- operator!=( exception_ptr const & a, exception_ptr const & b )
- {
- return !(a==b);
- }
- };
- class
- unknown_exception:
- public exception,
- public std::exception,
- public exception_detail::clone_base
- {
- public:
- unknown_exception()
- {
- }
- explicit
- unknown_exception( std::exception const & e )
- {
- add_original_type(e);
- }
- explicit
- unknown_exception( boost::exception const & e ):
- boost::exception(e)
- {
- add_original_type(e);
- }
- ~unknown_exception() throw()
- {
- }
- private:
- exception_detail::clone_base const *
- clone() const
- {
- return new unknown_exception(*this);
- }
- void
- rethrow() const
- {
- throw*this;
- }
- template <class E>
- void
- add_original_type( E const & e )
- {
- #ifndef BOOST_NO_RTTI
- (*this) << original_exception_type(&typeid(e));
- #endif
- }
- };
- namespace
- exception_detail
- {
- template <class T>
- class
- current_exception_std_exception_wrapper:
- public T,
- public boost::exception,
- public clone_base
- {
- public:
- explicit
- current_exception_std_exception_wrapper( T const & e1 ):
- T(e1)
- {
- add_original_type(e1);
- }
- current_exception_std_exception_wrapper( T const & e1, boost::exception const & e2 ):
- T(e1),
- boost::exception(e2)
- {
- add_original_type(e1);
- }
- ~current_exception_std_exception_wrapper() throw()
- {
- }
- private:
- clone_base const *
- clone() const
- {
- return new current_exception_std_exception_wrapper(*this);
- }
- void
- rethrow() const
- {
- throw *this;
- }
- template <class E>
- void
- add_original_type( E const & e )
- {
- #ifndef BOOST_NO_RTTI
- (*this) << original_exception_type(&typeid(e));
- #endif
- }
- };
- #ifdef BOOST_NO_RTTI
- template <class T>
- exception const *
- get_boost_exception( T const * )
- {
- try
- {
- throw;
- }
- catch(
- exception & x )
- {
- return &x;
- }
- catch(...)
- {
- return 0;
- }
- }
- #else
- template <class T>
- exception const *
- get_boost_exception( T const * x )
- {
- return dynamic_cast<exception const *>(x);
- }
- #endif
- template <class T>
- inline
- shared_ptr<clone_base const>
- current_exception_std_exception( T const & e1 )
- {
- if( boost::exception const * e2 = get_boost_exception(&e1) )
- return shared_ptr<current_exception_std_exception_wrapper<T> const>(new current_exception_std_exception_wrapper<T>(e1,*e2));
- else
- return shared_ptr<current_exception_std_exception_wrapper<T> const>(new current_exception_std_exception_wrapper<T>(e1));
- }
- inline
- shared_ptr<clone_base const>
- current_exception_unknown_exception()
- {
- return shared_ptr<unknown_exception const>(new unknown_exception());
- }
- inline
- shared_ptr<clone_base const>
- current_exception_unknown_boost_exception( boost::exception const & e )
- {
- return shared_ptr<unknown_exception const>(new unknown_exception(e));
- }
- inline
- shared_ptr<clone_base const>
- current_exception_unknown_std_exception( std::exception const & e )
- {
- if( boost::exception const * be = get_boost_exception(&e) )
- return current_exception_unknown_boost_exception(*be);
- else
- return shared_ptr<unknown_exception const>(new unknown_exception(e));
- }
- inline
- shared_ptr<clone_base const>
- current_exception_impl()
- {
- try
- {
- throw;
- }
- catch(
- exception_detail::clone_base & e )
- {
- return shared_ptr<exception_detail::clone_base const>(e.clone());
- }
- catch(
- std::domain_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::invalid_argument & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::length_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::out_of_range & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::logic_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::range_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::overflow_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::underflow_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::ios_base::failure & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::runtime_error & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::bad_alloc & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- #ifndef BOOST_NO_TYPEID
- catch(
- std::bad_cast & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::bad_typeid & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- #endif
- catch(
- std::bad_exception & e )
- {
- return exception_detail::current_exception_std_exception(e);
- }
- catch(
- std::exception & e )
- {
- return exception_detail::current_exception_unknown_std_exception(e);
- }
- catch(
- boost::exception & e )
- {
- return exception_detail::current_exception_unknown_boost_exception(e);
- }
- catch(
- ... )
- {
- return exception_detail::current_exception_unknown_exception();
- }
- }
- }
- inline
- exception_ptr
- current_exception()
- {
- try
- {
- return exception_ptr(exception_detail::current_exception_impl());
- }
- catch(
- std::bad_alloc & )
- {
- }
- catch(
- ... )
- {
- try
- {
- return exception_ptr(exception_detail::current_exception_std_exception(std::bad_exception()));
- }
- catch(
- std::bad_alloc & )
- {
- }
- catch(
- ... )
- {
- BOOST_ASSERT(0);
- }
- }
- return exception_ptr(exception_ptr::bad_alloc_tag());
- }
- template <class T>
- inline
- exception_ptr
- copy_exception( T const & e )
- {
- try
- {
- throw enable_current_exception(e);
- }
- catch(
- ... )
- {
- return current_exception();
- }
- }
- inline
- void
- rethrow_exception( exception_ptr const & p )
- {
- p._rethrow();
- }
- inline
- std::string
- to_string( exception_ptr const & p )
- {
- std::string s='\n'+diagnostic_information(p);
- std::string padding(" ");
- std::string r;
- bool f=false;
- for( std::string::const_iterator i=s.begin(),e=s.end(); i!=e; ++i )
- {
- if( f )
- r+=padding;
- char c=*i;
- r+=c;
- f=(c=='\n');
- }
- return r;
- }
- }
- #endif
|