From 019fbaa77e35e1aa020f2e25a3dd4a71e8767beb Mon Sep 17 00:00:00 2001 From: Emil Dotchevski Date: Mon, 1 Sep 2008 21:06:09 +0000 Subject: [PATCH] Optimization for error_info, error_info, error_info. Refactored exception_ptr to use shared_ptr. [SVN r48521] --- include/boost/exception/exception.hpp | 193 +++++++++++++++----------- include/boost/throw_exception.hpp | 7 + 2 files changed, 116 insertions(+), 84 deletions(-) diff --git a/include/boost/exception/exception.hpp b/include/boost/exception/exception.hpp index dcc6d43..5cba804 100644 --- a/include/boost/exception/exception.hpp +++ b/include/boost/exception/exception.hpp @@ -9,9 +9,6 @@ namespace boost { - - //////////////////////////////////////////////////////////////////////// - namespace exception_detail { @@ -66,65 +63,77 @@ boost add_ref() { if( px_ ) - intrusive_ptr_add_ref(px_); + px_->add_ref(); } void release() { if( px_ ) - intrusive_ptr_release(px_); + px_->release(); } }; } //////////////////////////////////////////////////////////////////////// - namespace - exception_detail + template + class error_info; + + typedef error_info throw_function; + typedef error_info throw_file; + typedef error_info throw_line; + + template <> + class + error_info { - class - counted_base + public: + typedef char const * value_type; + value_type v_; + explicit + error_info( value_type v ): + v_(v) { - friend - void - intrusive_ptr_add_ref( counted_base const * c ) - { - c->add_ref(); - } + } + }; - friend - void - intrusive_ptr_release( counted_base const * c ) - { - c->release(); - } + template <> + class + error_info + { + public: + typedef char const * value_type; + value_type v_; + explicit + error_info( value_type v ): + v_(v) + { + } + }; - virtual void add_ref() const=0; - virtual void release() const=0; + template <> + class + error_info + { + public: + typedef int value_type; + value_type v_; + explicit + error_info( value_type v ): + v_(v) + { + } + }; - protected: - - virtual - ~counted_base() throw() - { - } - }; - } - - //////////////////////////////////////////////////////////////////////// + template + E const & operator<<( E const &, error_info const & ); class exception; template class shared_ptr; - template - class error_info; - - template - E const & operator<<( E const &, error_info const & ); - namespace exception_detail { @@ -132,62 +141,83 @@ boost struct type_info_; struct - error_info_container: - public exception_detail::counted_base + error_info_container { virtual char const * diagnostic_information() const = 0; virtual shared_ptr get( type_info_ const & ) const = 0; virtual void set( shared_ptr const &, type_info_ const & ) = 0; + virtual void add_ref() const = 0; + virtual void release() const = 0; + + protected: + + virtual + ~error_info_container() throw() + { + } }; - template - shared_ptr get_info( exception const & ); + template + struct get_info; + + char const * get_diagnostic_information( exception const & ); } class exception { - public: - - virtual - char const * - diagnostic_information() const throw() - { - return _diagnostic_information(); - } - protected: - exception() + exception(): + throw_function_(0), + throw_file_(0), + throw_line_(-1) { } - virtual ~exception() throw()=0; - - char const * - _diagnostic_information() const throw() - { - if( exception_detail::error_info_container * c=data_.get() ) - try - { - if( char const * w = c->diagnostic_information() ) - return w; - } - catch(...) - { - } - return ""; - } + virtual ~exception() throw() = 0; private: - template - friend E const & operator<<( E const &, error_info const & ); + template + friend + E const & + operator<<( E const & x, throw_function y ) + { + x.throw_function_=y.v_; + return x; + } - template - friend shared_ptr exception_detail::get_info( exception const & ); + template + friend + E const & + operator<<( E const & x, throw_file y ) + { + x.throw_file_=y.v_; + return x; + } + + template + friend + E const & + operator<<( E const & x, throw_line y ) + { + x.throw_line_=y.v_; + return x; + } + + friend char const * exception_detail::get_diagnostic_information( exception const & ); + + template + friend E const & operator<<( E const &, error_info const & ); + + template + friend struct exception_detail::get_info; mutable exception_detail::refcount_ptr data_; + mutable char const * throw_function_; + mutable char const * throw_file_; + mutable int throw_line_; }; inline @@ -270,14 +300,12 @@ boost virtual clone_base const * clone() const = 0; virtual void rethrow() const = 0; - virtual ~clone_base() throw() = 0; - }; - inline - clone_base:: - ~clone_base() throw() - { - } + virtual + ~clone_base() throw() + { + } + }; inline void @@ -322,7 +350,7 @@ boost void rethrow() const { - throw *this; + throw*this; } }; } @@ -334,9 +362,6 @@ boost { return exception_detail::clone_impl(x); } - - //////////////////////////////////////////////////////////////////////// - } #endif diff --git a/include/boost/throw_exception.hpp b/include/boost/throw_exception.hpp index 0834e9d..94536d3 100644 --- a/include/boost/throw_exception.hpp +++ b/include/boost/throw_exception.hpp @@ -38,6 +38,13 @@ #if !defined( BOOST_NO_EXCEPTIONS ) && !defined( BOOST_EXCEPTION_DISABLE ) # include +# include +# define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(::boost::enable_error_info(x) <<\ + ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\ + ::boost::throw_file(__FILE__) <<\ + ::boost::throw_line((int)__LINE__)) +#else +# define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x) #endif namespace boost