Boost logo

Boost-Commit :

From: emil_at_[hidden]
Date: 2008-04-15 14:24:47


Author: emildotchevski
Date: 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
New Revision: 44438
URL: http://svn.boost.org/trac/boost/changeset/44438

Log:
N2179 compliance (pending documentation update)
Added:
   trunk/boost/exception/enable_current_exception.hpp
      - copied, changed from r44423, /trunk/boost/exception/enable_exception_cloning.hpp
   trunk/libs/exception/test/copy_exception_test.cpp (contents, props changed)
Removed:
   trunk/boost/exception/enable_exception_cloning.hpp
Text files modified:
   trunk/boost/exception/cloning.hpp | 160 ++++++++++++++++++++++++++++++++++++---
   trunk/boost/exception/enable_current_exception.hpp | 2
   trunk/libs/exception/example/cloning_1.cpp | 5
   trunk/libs/exception/example/cloning_2.cpp | 6
   trunk/libs/exception/test/Jamfile.v2 | 1
   trunk/libs/exception/test/cloning_test.cpp | 6
   trunk/libs/exception/test/throw_exception_test.cpp | 9 -
   trunk/libs/exception/test/unknown_exception_test.cpp | 35 +++++++-
   8 files changed, 191 insertions(+), 33 deletions(-)

Modified: trunk/boost/exception/cloning.hpp
==============================================================================
--- trunk/boost/exception/cloning.hpp (original)
+++ trunk/boost/exception/cloning.hpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -6,10 +6,10 @@
 #ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593
 #define UUID_FA5836A2CADA11DC8CD47C8555D89593
 
-#include <boost/exception/enable_exception_cloning.hpp>
+#include <boost/exception/enable_current_exception.hpp>
 #include <boost/exception/exception.hpp>
 #include <boost/exception/detail/cloning_base.hpp>
-#include <exception>
+#include <stdexcept>
 
 namespace
 boost
@@ -21,31 +21,165 @@
         {
         public:
 
- explicit
         unknown_exception()
             {
             }
 
         explicit
- unknown_exception( boost::exception const & x ):
- boost::exception(x)
+ unknown_exception( boost::exception const & e ):
+ boost::exception(e)
             {
             }
         };
 
     typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr;
 
+ namespace
+ exception_detail
+ {
+ template <class T>
+ class
+ current_exception_std_exception_wrapper:
+ public T,
+ public boost::exception
+ {
+ public:
+
+ explicit
+ current_exception_std_exception_wrapper( T const & e1 ):
+ T(e1)
+ {
+ }
+
+ current_exception_std_exception_wrapper( T const & e1, boost::exception const & e2 ):
+ T(e1),
+ boost::exception(e2)
+ {
+ }
+ };
+
+ template <class T>
+ exception_ptr
+ current_exception_std_exception( T const & e1 )
+ {
+ if( boost::exception const * e2 = dynamic_cast<boost::exception const *>(&e1) )
+ return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1,*e2)));
+ else
+ return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1)));
+ }
+
+ inline
+ exception_ptr
+ current_exception_unknown_exception()
+ {
+ return exception_ptr(exception_detail::make_clone(unknown_exception()));
+ }
+
+ inline
+ exception_ptr
+ current_exception_unknown_std_exception( std::exception const & e )
+ {
+ if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) )
+ return exception_ptr(exception_detail::make_clone(unknown_exception(*be)));
+ else
+ return current_exception_unknown_exception();
+ }
+
+ inline
+ exception_ptr
+ current_exception_unknown_boost_exception( boost::exception const & e )
+ {
+ return exception_ptr(exception_detail::make_clone(unknown_exception(e)));
+ }
+ }
+
+ inline
+ exception_ptr
+ current_exception()
+ {
+ try
+ {
+ throw;
+ }
+ catch(
+ exception_detail::cloning_base & e )
+ {
+ exception_detail::clone_base const * c = e.clone();
+ BOOST_ASSERT(c!=0);
+ return exception_ptr(c);
+ }
+ catch(
+ ... )
+ {
+ }
+ try
+ {
+ throw;
+ }
+ catch(
+ std::invalid_argument & 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::bad_alloc & e )
+ {
+ return exception_detail::current_exception_std_exception(e);
+ }
+ 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);
+ }
+ 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();
+ }
+ }
+
     template <class T>
     exception_ptr
- clone_exception( T const & e )
+ copy_exception( T const & e )
         {
- if( boost::exception_detail::cloning_base const * cb = dynamic_cast<boost::exception_detail::cloning_base const *>(&e) )
- if( exception_detail::clone_base const * c = cb->clone() )
- return exception_ptr(c);
- if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) )
- return exception_ptr(exception_detail::make_clone(unknown_exception(*be)));
- else
- return exception_ptr(exception_detail::make_clone(unknown_exception()));
+ try
+ {
+ throw enable_current_exception(e);
+ }
+ catch( ... )
+ {
+ return current_exception();
+ }
         }
 
     inline

Copied: trunk/boost/exception/enable_current_exception.hpp (from r44423, /trunk/boost/exception/enable_exception_cloning.hpp)
==============================================================================
--- /trunk/boost/exception/enable_exception_cloning.hpp (original)
+++ trunk/boost/exception/enable_current_exception.hpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -139,7 +139,7 @@
 
     template <class T>
     exception_detail::clone_impl<T>
- enable_exception_cloning( T const & x )
+ enable_current_exception( T const & x )
         {
         return exception_detail::clone_impl<T>(x);
         }

Deleted: trunk/boost/exception/enable_exception_cloning.hpp
==============================================================================
--- trunk/boost/exception/enable_exception_cloning.hpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
+++ (empty file)
@@ -1,148 +0,0 @@
-//Copyright (c) 2006-2008 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_78CC85B2914F11DC8F47B48E55D89593
-#define UUID_78CC85B2914F11DC8F47B48E55D89593
-
-#include <boost/exception/detail/counted_base.hpp>
-#include <boost/exception/detail/cloning_base.hpp>
-#include <boost/detail/atomic_count.hpp>
-#include <boost/assert.hpp>
-#include <new>
-
-namespace
-boost
- {
- namespace
- exception_detail
- {
- class
- clone_base:
- public counted_base
- {
- public:
-
- virtual void rethrow() const=0;
- };
-
- struct
- bad_alloc_impl:
- public clone_base,
- public std::bad_alloc
- {
- void
- add_ref() const
- {
- }
-
- void
- release() const
- {
- }
-
- void
- rethrow() const
- {
- throw *this;
- }
- };
-
- template <class T>
- clone_base * make_clone( T const & );
-
- template <class T>
- class
- clone_impl:
- public T,
- public cloning_base
- {
- public:
-
- explicit
- clone_impl( T const & x ):
- T(x)
- {
- }
-
- private:
-
- clone_base const *
- clone() const
- {
- return make_clone<T>(*this);
- }
- };
-
- template <class T>
- class
- exception_clone:
- public T,
- public clone_base
- {
- public:
-
- explicit
- exception_clone( T const & x ):
- T(x),
- count_(0)
- {
- }
-
- private:
-
- detail::atomic_count mutable count_;
-
- void
- add_ref() const
- {
- ++count_;
- }
-
- void
- release() const
- {
- if( !--count_ )
- delete this;
- }
-
- void
- rethrow() const
- {
- throw clone_impl<T>(*this);
- }
- };
-
- template <class T>
- clone_base *
- make_clone( T const & x )
- {
- try
- {
- return new exception_clone<T>(x);
- }
- catch(
- std::bad_alloc & )
- {
- static bad_alloc_impl bad_alloc;
- return &bad_alloc;
- }
- catch(
- ... )
- {
- BOOST_ASSERT(0);
- return 0;
- }
- }
- }
-
- template <class T>
- exception_detail::clone_impl<T>
- enable_exception_cloning( T const & x )
- {
- return exception_detail::clone_impl<T>(x);
- }
- }
-
-#endif

Modified: trunk/libs/exception/example/cloning_1.cpp
==============================================================================
--- trunk/libs/exception/example/cloning_1.cpp (original)
+++ trunk/libs/exception/example/cloning_1.cpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -5,7 +5,7 @@
 
 //This example shows how to enable cloning when throwing a boost::exception.
 
-#include <boost/exception/enable_exception_cloning.hpp>
+#include <boost/exception/enable_current_exception.hpp>
 #include <boost/exception/info.hpp>
 #include <stdio.h>
 #include <errno.h>
@@ -18,5 +18,6 @@
 file_read( FILE * f, void * buffer, size_t size )
     {
     if( size!=fread(buffer,1,size,f) )
- throw boost::enable_exception_cloning(file_read_error()) << errno_info(errno);
+ throw boost::enable_current_exception(file_read_error()) <<
+ errno_info(errno);
     }

Modified: trunk/libs/exception/example/cloning_2.cpp
==============================================================================
--- trunk/libs/exception/example/cloning_2.cpp (original)
+++ trunk/libs/exception/example/cloning_2.cpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -20,13 +20,13 @@
         error = boost::exception_ptr();
         }
     catch(
- boost::exception & e )
+ ... )
         {
- error = boost::clone_exception(e);
+ error = boost::current_exception();
         }
     }
 
-//
+// ...continued
 
 void
 work()

Modified: trunk/libs/exception/test/Jamfile.v2
==============================================================================
--- trunk/libs/exception/test/Jamfile.v2 (original)
+++ trunk/libs/exception/test/Jamfile.v2 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -16,6 +16,7 @@
 
 #exception
 run cloning_test.cpp ;
+run copy_exception_test.cpp ;
 run unknown_exception_test.cpp ;
 run exception_test.cpp ;
 run boost_error_info_test.cpp ;

Modified: trunk/libs/exception/test/cloning_test.cpp
==============================================================================
--- trunk/libs/exception/test/cloning_test.cpp (original)
+++ trunk/libs/exception/test/cloning_test.cpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -17,12 +17,12 @@
     {
     try
         {
- throw boost::enable_exception_cloning(test_exception());
+ throw boost::enable_current_exception(test_exception());
         }
     catch(
- std::exception & x )
+ ... )
         {
- boost::exception_ptr p = boost::clone_exception(x);
+ boost::exception_ptr p = boost::current_exception();
         try
             {
             rethrow_exception(p);

Added: trunk/libs/exception/test/copy_exception_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/exception/test/copy_exception_test.cpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -0,0 +1,34 @@
+//Copyright (c) 2006-2008 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)
+
+#include <boost/exception/cloning.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+struct
+test_exception:
+ std::exception
+ {
+ };
+
+int
+main()
+ {
+ boost::exception_ptr p = boost::copy_exception(test_exception());
+ try
+ {
+ rethrow_exception(p);
+ BOOST_TEST(false);
+ }
+ catch(
+ test_exception & )
+ {
+ }
+ catch(
+ ... )
+ {
+ BOOST_TEST(false);
+ }
+ return boost::report_errors();
+ }

Modified: trunk/libs/exception/test/throw_exception_test.cpp
==============================================================================
--- trunk/libs/exception/test/throw_exception_test.cpp (original)
+++ trunk/libs/exception/test/throw_exception_test.cpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -35,9 +35,9 @@
         BOOST_ASSERT(false);
         }
     catch(
- std::exception & x )
+ ... )
         {
- boost::exception_ptr p = boost::clone_exception(x);
+ boost::exception_ptr p = boost::current_exception();
         try
             {
             rethrow_exception(p);
@@ -55,11 +55,6 @@
             BOOST_TEST(false);
             }
         }
- catch(
- ... )
- {
- BOOST_TEST(false);
- }
     }
 
 int

Modified: trunk/libs/exception/test/unknown_exception_test.cpp
==============================================================================
--- trunk/libs/exception/test/unknown_exception_test.cpp (original)
+++ trunk/libs/exception/test/unknown_exception_test.cpp 2008-04-15 14:24:46 EDT (Tue, 15 Apr 2008)
@@ -40,9 +40,9 @@
         throw_boost_exception();
         }
     catch(
- boost::exception & x )
+ ... )
         {
- boost::exception_ptr ep=boost::clone_exception(x);
+ boost::exception_ptr ep=boost::current_exception();
         try
             {
             rethrow_exception(ep);
@@ -57,15 +57,29 @@
             {
             BOOST_TEST(false);
             }
+ try
+ {
+ rethrow_exception(ep);
+ }
+ catch(
+ boost::exception & x )
+ {
+ BOOST_TEST( 42==*boost::get_error_info<test>(x) );
+ }
+ catch(
+ ... )
+ {
+ BOOST_TEST(false);
+ }
         }
     try
         {
         throw_unknown_exception();
         }
     catch(
- std::exception & x )
+ ... )
         {
- boost::exception_ptr ep=boost::clone_exception(x);
+ boost::exception_ptr ep=boost::current_exception();
         try
             {
             rethrow_exception(ep);
@@ -79,6 +93,19 @@
             {
             BOOST_TEST(false);
             }
+ try
+ {
+ rethrow_exception(ep);
+ }
+ catch(
+ boost::exception & )
+ {
+ }
+ catch(
+ ... )
+ {
+ BOOST_TEST(false);
+ }
         }
     return boost::report_errors();
     }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk