fixed compile errors, removed tabs as required.

[SVN r44114]
This commit is contained in:
Emil Dotchevski
2008-04-08 21:29:37 +00:00
parent e0881a603d
commit 0fbc4dc974
33 changed files with 1402 additions and 1404 deletions

View File

@ -16,7 +16,7 @@ class file_read_error: public boost::exception { };
void void
file_read( FILE * f, void * buffer, size_t size ) file_read( FILE * f, void * buffer, size_t size )
{ {
if( size!=fread(buffer,1,size,f) ) if( size!=fread(buffer,1,size,f) )
throw boost::enable_exception_cloning(file_read_error()) << errno_info(errno); throw boost::enable_exception_cloning(file_read_error()) << errno_info(errno);
} }

View File

@ -13,27 +13,27 @@ void do_work(); //throws cloning-enabled boost::exceptions
void void
worker_thread( boost::exception_ptr & error ) worker_thread( boost::exception_ptr & error )
{ {
try try
{ {
do_work(); do_work();
error = boost::exception_ptr(); error = boost::exception_ptr();
} }
catch( catch(
boost::exception & e ) boost::exception & e )
{ {
error = boost::clone_exception(e); error = boost::clone_exception(e);
} }
} }
// //
void void
work() work()
{ {
boost::exception_ptr error; boost::exception_ptr error;
boost::thread t( boost::bind(worker_thread,boost::ref(error)) ); boost::thread t( boost::bind(worker_thread,boost::ref(error)) );
t.join(); t.join();
if( error ) if( error )
boost::rethrow_exception(error); boost::rethrow_exception(error);
} }

View File

@ -17,19 +17,19 @@ typedef boost::error_info<struct tag_std_range_index,size_t> std_range_index;
template <class T> template <class T>
class class
my_container my_container
{ {
public: public:
size_t size() const; size_t size() const;
T const & T const &
operator[]( size_t i ) const operator[]( size_t i ) const
{ {
if( i > size() ) if( i > size() )
throw boost::enable_error_info(std::range_error("Index out of range")) << throw boost::enable_error_info(std::range_error("Index out of range")) <<
std_range_min(0) << std_range_min(0) <<
std_range_max(size()) << std_range_max(size()) <<
std_range_index(i); std_range_index(i);
//.... //....
} }
}; };

View File

@ -16,21 +16,21 @@ class my_error: public boost::exception, public std::exception { }; //(2)
void void
f() f()
{ {
throw my_error() << errno_info(errno); //(3) throw my_error() << errno_info(errno); //(3)
} }
void void
g() g()
{ {
try try
{ {
f(); f();
} }
catch( catch(
my_error & x ) my_error & x )
{ {
if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) ) if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) )
std::cerr << "Error code: " << *err; std::cerr << "Error code: " << *err;
} }
} }

View File

@ -18,10 +18,10 @@ class file_read_error: public boost::exception { };
void void
file_read( FILE * f, void * buffer, size_t size ) file_read( FILE * f, void * buffer, size_t size )
{ {
if( size!=fread(buffer,1,size,f) ) if( size!=fread(buffer,1,size,f) )
throw file_read_error() << errno_info(errno); throw file_read_error() << errno_info(errno);
} }
// //
@ -32,18 +32,18 @@ void file_read( FILE * f, void * buffer, size_t size );
void void
parse_file( char const * file_name ) parse_file( char const * file_name )
{ {
boost::shared_ptr<FILE> f = file_open(file_name,"rb"); boost::shared_ptr<FILE> f = file_open(file_name,"rb");
assert(f); assert(f);
try try
{ {
char buf[1024]; char buf[1024];
file_read( f.get(), buf, sizeof(buf) ); file_read( f.get(), buf, sizeof(buf) );
} }
catch( catch(
boost::exception & e ) boost::exception & e )
{ {
e << file_name_info(file_name); e << file_name_info(file_name);
throw; throw;
} }
} }

View File

@ -8,7 +8,7 @@
// //
//The documentation for boost::exception can be found at: //The documentation for boost::exception can be found at:
// //
// http://www.revergestudios.com/boost-exception/boost-exception.htm. // http://www.revergestudios.com/boost-exception/boost-exception.htm.
// //
//The output from this program can vary depending on the compiler/OS combination. //The output from this program can vary depending on the compiler/OS combination.
@ -33,23 +33,23 @@ size_t const data_size = sizeof(data);
class class
error: //Base for all exception objects we throw. error: //Base for all exception objects we throw.
public std::exception, public std::exception,
public boost::exception public boost::exception
{ {
public: public:
char const * char const *
what() const throw() what() const throw()
{ {
return boost::exception::what(); return boost::exception::what();
} }
protected: protected:
~error() throw() ~error() throw()
{ {
} }
}; };
class open_error: public error { }; class open_error: public error { };
class read_error: public error { }; class read_error: public error { };
@ -62,154 +62,154 @@ class fwrite_error: public write_error { };
boost::shared_ptr<FILE> boost::shared_ptr<FILE>
my_fopen( char const * name, char const * mode ) my_fopen( char const * name, char const * mode )
{ {
if( FILE * f = ::fopen(name,mode) ) if( FILE * f = ::fopen(name,mode) )
return boost::shared_ptr<FILE>(f,fclose); return boost::shared_ptr<FILE>(f,fclose);
else else
throw fopen_error() << BOOST_ERROR_INFO << throw fopen_error() << BOOST_ERROR_INFO <<
errno_info(errno) << errno_info(errno) <<
file_name_info(name) << file_name_info(name) <<
open_mode_info(mode) << open_mode_info(mode) <<
function_info("fopen"); function_info("fopen");
} }
void void
my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream ) my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
{ {
assert(stream); assert(stream);
if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) ) if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) )
throw fread_error() << BOOST_ERROR_INFO << throw fread_error() << BOOST_ERROR_INFO <<
function_info("fread") << function_info("fread") <<
errno_info(errno) << errno_info(errno) <<
file_stream_info(boost::weak_ptr<FILE>(stream)); file_stream_info(boost::weak_ptr<FILE>(stream));
} }
void void
my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream ) my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
{ {
assert(stream); assert(stream);
if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) ) if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) )
throw fwrite_error() << BOOST_ERROR_INFO << throw fwrite_error() << BOOST_ERROR_INFO <<
function_info("fwrite") << function_info("fwrite") <<
errno_info(errno) << errno_info(errno) <<
file_stream_info(boost::weak_ptr<FILE>(stream)); file_stream_info(boost::weak_ptr<FILE>(stream));
} }
void void
reset_file( char const * file_name ) reset_file( char const * file_name )
{ {
(void) my_fopen(file_name,"wb"); (void) my_fopen(file_name,"wb");
} }
void void
create_data( char const * file_name ) create_data( char const * file_name )
{ {
boost::shared_ptr<FILE> f = my_fopen(file_name,"wb"); boost::shared_ptr<FILE> f = my_fopen(file_name,"wb");
my_fwrite( data, 1, data_size, f ); my_fwrite( data, 1, data_size, f );
} }
void void
copy_data( char const * src_file_name, char const * dst_file_name ) copy_data( char const * src_file_name, char const * dst_file_name )
{ {
boost::shared_ptr<FILE> src = my_fopen(src_file_name,"rb"); boost::shared_ptr<FILE> src = my_fopen(src_file_name,"rb");
boost::shared_ptr<FILE> dst = my_fopen(dst_file_name,"wb"); boost::shared_ptr<FILE> dst = my_fopen(dst_file_name,"wb");
try try
{ {
char buffer[data_size]; char buffer[data_size];
my_fread( buffer, 1, data_size, src ); my_fread( buffer, 1, data_size, src );
my_fwrite( buffer, 1, data_size, dst ); my_fwrite( buffer, 1, data_size, dst );
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
if( boost::shared_ptr<boost::weak_ptr<FILE> const> f=boost::get_error_info<file_stream_info>(x) ) if( boost::shared_ptr<boost::weak_ptr<FILE> const> f=boost::get_error_info<file_stream_info>(x) )
if( boost::shared_ptr<FILE> fs = f->lock() ) if( boost::shared_ptr<FILE> fs = f->lock() )
{ {
if( fs==src ) if( fs==src )
x << file_name_info(src_file_name); x << file_name_info(src_file_name);
else if( fs==dst ) else if( fs==dst )
x << file_name_info(dst_file_name); x << file_name_info(dst_file_name);
} }
x << x <<
file_name_src_info(src_file_name) << file_name_src_info(src_file_name) <<
file_name_dst_info(dst_file_name); file_name_dst_info(dst_file_name);
throw; throw;
} }
} }
void void
dump_copy_info( boost::exception const & x ) dump_copy_info( boost::exception const & x )
{ {
if( boost::shared_ptr<std::string const> src = boost::get_error_info<file_name_src_info>(x) ) if( boost::shared_ptr<std::string const> src = boost::get_error_info<file_name_src_info>(x) )
std::cout << "Source file name: " << *src << "\n"; std::cout << "Source file name: " << *src << "\n";
if( boost::shared_ptr<std::string const> dst = boost::get_error_info<file_name_dst_info>(x) ) if( boost::shared_ptr<std::string const> dst = boost::get_error_info<file_name_dst_info>(x) )
std::cout << "Destination file name: " << *dst << "\n"; std::cout << "Destination file name: " << *dst << "\n";
} }
void void
dump_file_info( boost::exception const & x ) dump_file_info( boost::exception const & x )
{ {
if( boost::shared_ptr<std::string const> fn = boost::get_error_info<file_name_info>(x) ) if( boost::shared_ptr<std::string const> fn = boost::get_error_info<file_name_info>(x) )
std::cout << "Source file name: " << *fn << "\n"; std::cout << "Source file name: " << *fn << "\n";
} }
void void
dump_clib_info( boost::exception const & x ) dump_clib_info( boost::exception const & x )
{ {
if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) ) if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) )
std::cout << "OS error: " << *err << "\n"; std::cout << "OS error: " << *err << "\n";
if( boost::shared_ptr<std::string const> fn=boost::get_error_info<function_info>(x) ) if( boost::shared_ptr<std::string const> fn=boost::get_error_info<function_info>(x) )
std::cout << "Failed function: " << *fn << "\n"; std::cout << "Failed function: " << *fn << "\n";
} }
void void
dump_all_info( boost::exception const & x ) dump_all_info( boost::exception const & x )
{ {
std::cout << "-------------------------------------------------\n"; std::cout << "-------------------------------------------------\n";
dump_copy_info(x); dump_copy_info(x);
dump_file_info(x); dump_file_info(x);
dump_clib_info(x); dump_clib_info(x);
std::cout << "\nOutput from what():\n"; std::cout << "\nOutput from what():\n";
std::cout << x.what(); std::cout << x.what();
} }
int int
main() main()
{ {
try try
{ {
create_data( "tmp1.txt" ); create_data( "tmp1.txt" );
copy_data( "tmp1.txt", "tmp2.txt" ); //This should succeed. copy_data( "tmp1.txt", "tmp2.txt" ); //This should succeed.
reset_file( "tmp1.txt" ); //Creates empty file. reset_file( "tmp1.txt" ); //Creates empty file.
try try
{ {
copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt is empty. copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt is empty.
} }
catch( catch(
read_error & x ) read_error & x )
{ {
std::cout << "\nCaught 'read_error' exception.\n"; std::cout << "\nCaught 'read_error' exception.\n";
dump_all_info(x); dump_all_info(x);
} }
remove( "tmp1.txt" ); remove( "tmp1.txt" );
remove( "tmp2.txt" ); remove( "tmp2.txt" );
try try
{ {
copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt does not exist. copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt does not exist.
} }
catch( catch(
open_error & x ) open_error & x )
{ {
std::cout << "\nCaught 'open_error' exception.\n"; std::cout << "\nCaught 'open_error' exception.\n";
dump_all_info(x); dump_all_info(x);
} }
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
std::cout << "\nCaught unexpected boost::exception!\n"; std::cout << "\nCaught unexpected boost::exception!\n";
dump_all_info(x); dump_all_info(x);
} }
} }

View File

@ -12,14 +12,14 @@ void f(); //throws unknown types that derive from boost::exception.
void void
g() g()
{ {
try try
{ {
f(); f();
} }
catch( catch(
boost::exception & e ) boost::exception & e )
{ {
std::cerr << e.what(); std::cerr << e.what();
} }
} }

View File

@ -13,47 +13,47 @@
namespace namespace
boost boost
{ {
class class
unknown_exception: unknown_exception:
public exception, public exception,
public std::exception public std::exception
{ {
public: public:
explicit explicit
unknown_exception() unknown_exception()
{ {
} }
explicit explicit
unknown_exception( boost::exception const & x ): unknown_exception( boost::exception const & x ):
boost::exception(x) boost::exception(x)
{ {
} }
}; };
typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr; typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr;
template <class T> template <class T>
exception_ptr exception_ptr
clone_exception( T const & e ) clone_exception( T const & e )
{ {
if( boost::exception_detail::cloning_base const * cb = dynamic_cast<boost::exception_detail::cloning_base 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() ) if( exception_detail::clone_base const * c = cb->clone() )
return exception_ptr(c); return exception_ptr(c);
if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) ) if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) )
return exception_ptr(exception_detail::make_clone(unknown_exception(*be))); return exception_ptr(exception_detail::make_clone(unknown_exception(*be)));
else else
return exception_ptr(exception_detail::make_clone(unknown_exception())); return exception_ptr(exception_detail::make_clone(unknown_exception()));
} }
inline inline
void void
rethrow_exception( exception_ptr const & p ) rethrow_exception( exception_ptr const & p )
{ {
p->rethrow(); p->rethrow();
} }
} }
#endif #endif

View File

@ -8,20 +8,20 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_detail exception_detail
{ {
class clone_base; class clone_base;
class class
cloning_base cloning_base
{ {
public: public:
virtual clone_base const * clone() const = 0; virtual clone_base const * clone() const = 0;
}; };
} }
} }
#endif #endif

View File

@ -8,31 +8,31 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_detail exception_detail
{ {
class class
counted_base counted_base
{ {
friend friend
void void
intrusive_ptr_add_ref( counted_base const * c ) intrusive_ptr_add_ref( counted_base const * c )
{ {
c->add_ref(); c->add_ref();
} }
friend friend
void void
intrusive_ptr_release( counted_base const * c ) intrusive_ptr_release( counted_base const * c )
{ {
c->release(); c->release();
} }
virtual void add_ref() const=0; virtual void add_ref() const=0;
virtual void release() const=0; virtual void release() const=0;
}; };
} }
} }
#endif #endif

View File

@ -10,60 +10,60 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_detail exception_detail
{ {
template <class T> template <class T>
struct struct
error_info_injector: error_info_injector:
public T, public T,
public exception public exception
{ {
explicit explicit
error_info_injector( T const & x ): error_info_injector( T const & x ):
T(x) T(x)
{ {
} }
}; };
struct large_size { char c[256]; }; struct large_size { char c[256]; };
large_size dispatch( exception * ); large_size dispatch( exception * );
struct small_size { }; struct small_size { };
small_size dispatch( void * ); small_size dispatch( void * );
template <class,size_t> template <class,size_t>
struct enable_error_info_helper; struct enable_error_info_helper;
template <class T> template <class T>
struct struct
enable_error_info_helper<T,sizeof(large_size)> enable_error_info_helper<T,sizeof(large_size)>
{ {
typedef T type; typedef T type;
}; };
template <class T> template <class T>
struct struct
enable_error_info_helper<T,sizeof(small_size)> enable_error_info_helper<T,sizeof(small_size)>
{ {
typedef error_info_injector<T> type; typedef error_info_injector<T> type;
}; };
template <class T> template <class T>
struct struct
enable_error_info_return_type enable_error_info_return_type
{ {
typedef typename enable_error_info_helper<T,sizeof(dispatch((T*)0))>::type type; typedef typename enable_error_info_helper<T,sizeof(dispatch((T*)0))>::type type;
}; };
} }
template <class T> template <class T>
typename exception_detail::enable_error_info_return_type<T>::type typename exception_detail::enable_error_info_return_type<T>::type
enable_error_info( T const & x ) enable_error_info( T const & x )
{ {
return typename exception_detail::enable_error_info_return_type<T>::type(x); return typename exception_detail::enable_error_info_return_type<T>::type(x);
} }
} }
#endif #endif

View File

@ -14,135 +14,135 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_detail exception_detail
{ {
class class
clone_base: clone_base:
public counted_base public counted_base
{ {
public: public:
virtual void rethrow() const=0; virtual void rethrow() const=0;
}; };
struct struct
bad_alloc_impl: bad_alloc_impl:
public clone_base, public clone_base,
public std::bad_alloc public std::bad_alloc
{ {
void void
add_ref() const add_ref() const
{ {
} }
void void
release() const release() const
{ {
} }
void void
rethrow() const rethrow() const
{ {
throw *this; throw *this;
} }
}; };
template <class T> template <class T>
clone_base * make_clone( T const & ); clone_base * make_clone( T const & );
template <class T> template <class T>
class class
clone_impl: clone_impl:
public T, public T,
public cloning_base public cloning_base
{ {
public: public:
explicit explicit
clone_impl( T const & x ): clone_impl( T const & x ):
T(x) T(x)
{ {
} }
private: private:
clone_base const * clone_base const *
clone() const clone() const
{ {
return make_clone<T>(*this); return make_clone<T>(*this);
} }
}; };
template <class T> template <class T>
class class
exception_clone: exception_clone:
public T, public T,
public clone_base public clone_base
{ {
public: public:
explicit explicit
exception_clone( T const & x ): exception_clone( T const & x ):
T(x), T(x),
count_(0) count_(0)
{ {
} }
private: private:
detail::atomic_count mutable count_; detail::atomic_count mutable count_;
void void
add_ref() const add_ref() const
{ {
++count_; ++count_;
} }
void void
release() const release() const
{ {
if( !--count_ ) if( !--count_ )
delete this; delete this;
} }
void void
rethrow() const rethrow() const
{ {
throw clone_impl<T>(*this); throw clone_impl<T>(*this);
} }
}; };
template <class T> template <class T>
clone_base * clone_base *
make_clone( T const & x ) make_clone( T const & x )
{ {
try try
{ {
return new exception_clone<T>(x); return new exception_clone<T>(x);
} }
catch( catch(
std::bad_alloc & ) std::bad_alloc & )
{ {
static bad_alloc_impl bad_alloc; static bad_alloc_impl bad_alloc;
return &bad_alloc; return &bad_alloc;
} }
catch( catch(
... ) ... )
{ {
BOOST_ASSERT(0); BOOST_ASSERT(0);
return 0; return 0;
} }
} }
} }
template <class T> template <class T>
exception_detail::clone_impl<T> exception_detail::clone_impl<T>
enable_exception_cloning( T const & x ) enable_exception_cloning( T const & x )
{ {
return exception_detail::clone_impl<T>(x); return exception_detail::clone_impl<T>(x);
} }
} }
#endif #endif

View File

@ -8,9 +8,9 @@
namespace namespace
boost boost
{ {
template <class Tag,class T> template <class Tag,class T>
class error_info; class error_info;
} }
#endif #endif

View File

@ -12,79 +12,79 @@
namespace namespace
boost boost
{ {
template <class T> template <class T>
class shared_ptr; class shared_ptr;
namespace namespace
exception_detail exception_detail
{ {
class error_info_base; class error_info_base;
struct struct
error_info_container: error_info_container:
public exception_detail::counted_base public exception_detail::counted_base
{ {
virtual char const * what( std::type_info const & ) const = 0; virtual char const * what( std::type_info const & ) const = 0;
virtual shared_ptr<error_info_base const> get( std::type_info const & ) const = 0; virtual shared_ptr<error_info_base const> get( std::type_info const & ) const = 0;
virtual void set( shared_ptr<error_info_base const> const & ) = 0; virtual void set( shared_ptr<error_info_base const> const & ) = 0;
}; };
} }
template <class Tag,class T> template <class Tag,class T>
class error_info; class error_info;
template <class E,class Tag,class T> template <class E,class Tag,class T>
E const & operator<<( E const &, error_info<Tag,T> const & ); E const & operator<<( E const &, error_info<Tag,T> const & );
template <class ErrorInfo,class E> template <class ErrorInfo,class E>
shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & ); shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & );
class class
exception exception
{ {
public: public:
virtual ~exception() throw()=0; virtual ~exception() throw()=0;
virtual char const * what() const throw(); virtual char const * what() const throw();
private: private:
shared_ptr<exception_detail::error_info_base const> get( std::type_info const & ) const; shared_ptr<exception_detail::error_info_base const> get( std::type_info const & ) const;
void set( shared_ptr<exception_detail::error_info_base const> const & ) const; void set( shared_ptr<exception_detail::error_info_base const> const & ) const;
template <class E,class Tag,class T> template <class E,class Tag,class T>
friend E const & operator<<( E const &, error_info<Tag,T> const & ); friend E const & operator<<( E const &, error_info<Tag,T> const & );
template <class ErrorInfo,class E> template <class ErrorInfo,class E>
friend shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & ); friend shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & );
intrusive_ptr<exception_detail::error_info_container> mutable data_; intrusive_ptr<exception_detail::error_info_container> mutable data_;
}; };
inline inline
exception:: exception::
~exception() throw() ~exception() throw()
{ {
} }
inline inline
char const * char const *
exception:: exception::
what() const throw() what() const throw()
{ {
if( data_ ) if( data_ )
try try
{ {
char const * w = data_->what(typeid(*this)); char const * w = data_->what(typeid(*this));
BOOST_ASSERT(0!=w); BOOST_ASSERT(0!=w);
return w; return w;
} }
catch(...) catch(...)
{ {
} }
return typeid(*this).name(); return typeid(*this).name();
} }
} }
#endif #endif

View File

@ -15,219 +15,219 @@
#include <map> #include <map>
#define BOOST_ERROR_INFO\ #define BOOST_ERROR_INFO\
::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\ ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
::boost::throw_file(__FILE__) <<\ ::boost::throw_file(__FILE__) <<\
::boost::throw_line((int)__LINE__) ::boost::throw_line((int)__LINE__)
namespace namespace
boost boost
{ {
typedef error_info<struct tag_throw_function,char const *> throw_function; typedef error_info<struct tag_throw_function,char const *> throw_function;
typedef error_info<struct tag_throw_file,char const *> throw_file; typedef error_info<struct tag_throw_file,char const *> throw_file;
typedef error_info<struct tag_throw_line,int> throw_line; typedef error_info<struct tag_throw_line,int> throw_line;
namespace namespace
exception_detail exception_detail
{ {
class class
error_info_base error_info_base
{ {
public: public:
virtual std::type_info const & tag_typeid() const = 0; virtual std::type_info const & tag_typeid() const = 0;
virtual std::string value_as_string() const = 0; virtual std::string value_as_string() const = 0;
}; };
} }
template <class Tag,class T> template <class Tag,class T>
class class
error_info: error_info:
public exception_detail::error_info_base public exception_detail::error_info_base
{ {
public: public:
typedef T value_type; typedef T value_type;
error_info( value_type const & value ): error_info( value_type const & value ):
value_(value) value_(value)
{ {
} }
value_type const & value_type const &
value() const value() const
{ {
return value_; return value_;
} }
private: private:
std::type_info const & std::type_info const &
tag_typeid() const tag_typeid() const
{ {
return typeid(type<Tag>); return typeid(type<Tag>);
} }
std::string std::string
value_as_string() const value_as_string() const
{ {
return to_string_stub(value_); return to_string_stub(value_);
} }
value_type const value_; value_type const value_;
}; };
template <class ErrorInfo> template <class ErrorInfo>
struct struct
error_info_type error_info_type
{ {
typedef typename ErrorInfo::value_type value_type; typedef typename ErrorInfo::value_type value_type;
}; };
template <class E,class Tag,class T> template <class E,class Tag,class T>
E const & E const &
operator<<( E const & x, error_info<Tag,T> const & v ) operator<<( E const & x, error_info<Tag,T> const & v )
{ {
shared_ptr< error_info<Tag,T> > p( new error_info<Tag,T>(v) ); shared_ptr< error_info<Tag,T> > p( new error_info<Tag,T>(v) );
x.set(p); x.set(p);
return x; return x;
} }
template <class ErrorInfo,class E> template <class ErrorInfo,class E>
shared_ptr<typename ErrorInfo::value_type const> shared_ptr<typename ErrorInfo::value_type const>
get_error_info( E const & some_exception ) get_error_info( E const & some_exception )
{ {
if( exception const * x = dynamic_cast<exception const *>(&some_exception) ) if( exception const * x = dynamic_cast<exception const *>(&some_exception) )
if( shared_ptr<exception_detail::error_info_base const> eib = x->get(typeid(ErrorInfo)) ) if( shared_ptr<exception_detail::error_info_base const> eib = x->get(typeid(ErrorInfo)) )
{ {
BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo const *>(eib.get()) ); BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo const *>(eib.get()) );
ErrorInfo const * w = static_cast<ErrorInfo const *>(eib.get()); ErrorInfo const * w = static_cast<ErrorInfo const *>(eib.get());
return shared_ptr<typename ErrorInfo::value_type const>(eib,&w->value()); return shared_ptr<typename ErrorInfo::value_type const>(eib,&w->value());
} }
return shared_ptr<typename ErrorInfo::value_type const>(); return shared_ptr<typename ErrorInfo::value_type const>();
} }
namespace namespace
exception_detail exception_detail
{ {
class class
error_info_container_impl: error_info_container_impl:
public error_info_container public error_info_container
{ {
public: public:
error_info_container_impl(): error_info_container_impl():
count_(0) count_(0)
{ {
} }
~error_info_container_impl() throw() ~error_info_container_impl() throw()
{ {
} }
shared_ptr<error_info_base const> shared_ptr<error_info_base const>
get( std::type_info const & ti ) const get( std::type_info const & ti ) const
{ {
error_info_map::const_iterator i=info_.find(typeinfo(ti)); error_info_map::const_iterator i=info_.find(typeinfo(ti));
if( info_.end()!=i ) if( info_.end()!=i )
{ {
shared_ptr<error_info_base const> const & p = i->second; shared_ptr<error_info_base const> const & p = i->second;
BOOST_ASSERT( typeid(*p)==ti ); BOOST_ASSERT( typeid(*p)==ti );
return p; return p;
} }
return shared_ptr<error_info_base const>(); return shared_ptr<error_info_base const>();
} }
void void
set( shared_ptr<error_info_base const> const & x ) set( shared_ptr<error_info_base const> const & x )
{ {
BOOST_ASSERT(x); BOOST_ASSERT(x);
info_[typeinfo(typeid(*x))] = x; info_[typeinfo(typeid(*x))] = x;
what_.clear(); what_.clear();
} }
char const * char const *
what( std::type_info const & exception_type ) const what( std::type_info const & exception_type ) const
{ {
if( what_.empty() ) if( what_.empty() )
{ {
std::string tmp(exception_type.name()); std::string tmp(exception_type.name());
tmp += '\n'; tmp += '\n';
for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i ) for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
{ {
shared_ptr<error_info_base const> const & x = i->second; shared_ptr<error_info_base const> const & x = i->second;
tmp += '['; tmp += '[';
tmp += x->tag_typeid().name(); tmp += x->tag_typeid().name();
tmp += "] = "; tmp += "] = ";
tmp += x->value_as_string(); tmp += x->value_as_string();
tmp += '\n'; tmp += '\n';
} }
what_.swap(tmp); what_.swap(tmp);
} }
return what_.c_str(); return what_.c_str();
} }
private: private:
friend class exception; friend class exception;
struct struct
typeinfo typeinfo
{ {
std::type_info const * type; std::type_info const * type;
explicit explicit
typeinfo( std::type_info const & t ): typeinfo( std::type_info const & t ):
type(&t) type(&t)
{ {
} }
bool bool
operator<( typeinfo const & b ) const operator<( typeinfo const & b ) const
{ {
return 0!=(type->before(*b.type)); return 0!=(type->before(*b.type));
} }
}; };
typedef std::map< typeinfo, shared_ptr<error_info_base const> > error_info_map; typedef std::map< typeinfo, shared_ptr<error_info_base const> > error_info_map;
error_info_map info_; error_info_map info_;
std::string mutable what_; std::string mutable what_;
int mutable count_; int mutable count_;
void void
add_ref() const add_ref() const
{ {
++count_; ++count_;
} }
void void
release() const release() const
{ {
if( !--count_ ) if( !--count_ )
delete this; delete this;
} }
}; };
} }
inline inline
void void
exception:: exception::
set( shared_ptr<exception_detail::error_info_base const> const & x ) const set( shared_ptr<exception_detail::error_info_base const> const & x ) const
{ {
if( !data_ ) if( !data_ )
data_ = intrusive_ptr<exception_detail::error_info_container>(new exception_detail::error_info_container_impl); data_ = intrusive_ptr<exception_detail::error_info_container>(new exception_detail::error_info_container_impl);
data_->set(x); data_->set(x);
} }
inline inline
shared_ptr<exception_detail::error_info_base const> shared_ptr<exception_detail::error_info_base const>
exception:: exception::
get( std::type_info const & ti ) const get( std::type_info const & ti ) const
{ {
if( data_ ) if( data_ )
return data_->get(ti); return data_->get(ti);
else else
return shared_ptr<exception_detail::error_info_base const>(); return shared_ptr<exception_detail::error_info_base const>();
} }
} }
#endif #endif

View File

@ -11,54 +11,54 @@
namespace namespace
boost boost
{ {
template < template <
class E, class E,
class Tag1,class T1, class Tag1,class T1,
class Tag2,class T2 > class Tag2,class T2 >
E const & E const &
operator<<( operator<<(
E const & x, E const & x,
tuple< tuple<
error_info<Tag1,T1>, error_info<Tag1,T1>,
error_info<Tag2,T2> > const & v ) error_info<Tag2,T2> > const & v )
{ {
return x << v.template get<0>() << v.template get<1>(); return x << v.template get<0>() << v.template get<1>();
} }
template < template <
class E, class E,
class Tag1,class T1, class Tag1,class T1,
class Tag2,class T2, class Tag2,class T2,
class Tag3,class T3 > class Tag3,class T3 >
E const & E const &
operator<<( operator<<(
E const & x, E const & x,
tuple< tuple<
error_info<Tag1,T1>, error_info<Tag1,T1>,
error_info<Tag2,T2>, error_info<Tag2,T2>,
error_info<Tag3,T3> > const & v ) error_info<Tag3,T3> > const & v )
{ {
return x << v.template get<0>() << v.template get<1>() << v.template get<2>(); return x << v.template get<0>() << v.template get<1>() << v.template get<2>();
} }
template < template <
class E, class E,
class Tag1,class T1, class Tag1,class T1,
class Tag2,class T2, class Tag2,class T2,
class Tag3,class T3, class Tag3,class T3,
class Tag4,class T4 > class Tag4,class T4 >
E const & E const &
operator<<( operator<<(
E const & x, E const & x,
tuple< tuple<
error_info<Tag1,T1>, error_info<Tag1,T1>,
error_info<Tag2,T2>, error_info<Tag2,T2>,
error_info<Tag3,T3>, error_info<Tag3,T3>,
error_info<Tag4,T4> > const & v ) error_info<Tag4,T4> > const & v )
{ {
return x << v.template get<0>() << v.template get<1>() << v.template get<2>() << v.template get<3>(); return x << v.template get<0>() << v.template get<1>() << v.template get<2>() << v.template get<3>();
} }
} }
#endif #endif

View File

@ -10,18 +10,18 @@
namespace namespace
boost boost
{ {
namespace namespace
{ {
template <class T> template <class T>
std::string std::string
to_string( T const & x ) to_string( T const & x )
{ {
std::ostringstream out; std::ostringstream out;
out << x; out << x;
return out.str(); return out.str();
} }
} }
} }
#endif #endif

View File

@ -12,138 +12,135 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_detail exception_detail
{ {
template <bool ShiftLeftAvailable> template <bool ShiftLeftAvailable>
struct shift_left_dispatcher; struct shift_left_dispatcher;
template <> template <>
struct struct
shift_left_dispatcher<true> shift_left_dispatcher<true>
{ {
template <class T,class CharT,class Traits,class Stub> template <class T,class CharT,class Traits,class Stub>
static static
void void
convert( std::basic_ostream<CharT,Traits> & out, T const & x, Stub ) convert( std::basic_ostream<CharT,Traits> & out, T const & x, Stub )
{ {
out << x; out << x;
} }
}; };
template <> template <>
struct struct
shift_left_dispatcher<false> shift_left_dispatcher<false>
{ {
template <class T,class CharT,class Traits,class Stub> template <class T,class CharT,class Traits,class Stub>
static static
void void
convert( std::basic_ostream<CharT,Traits> & out, T const & x, Stub s ) convert( std::basic_ostream<CharT,Traits> & out, T const & x, Stub s )
{ {
out << s(x); out << s(x);
} }
}; };
namespace namespace
shift_left_dispatch shift_left_dispatch
{ {
template <class T,class CharT,class Traits> template <class T,class CharT,class Traits>
char operator<<( std::basic_ostream<CharT,Traits> &, T ); char operator<<( std::basic_ostream<CharT,Traits> &, T );
template <class T,class CharT,class Traits,class Stub> template <class T,class CharT,class Traits,class Stub>
void void
dispatch( std::basic_ostream<CharT,Traits> & out, T const & x, Stub s ) dispatch( std::basic_ostream<CharT,Traits> & out, T const & x, Stub s )
{ {
shift_left_dispatcher<1!=sizeof(out<<x)>::convert(out,x,s); shift_left_dispatcher<1!=sizeof(out<<x)>::convert(out,x,s);
} }
} }
} }
namespace namespace
exception_detail exception_detail
{ {
template <bool ToStringAvailable> template <bool ToStringAvailable>
struct to_string_dispatcher; struct
to_string_dispatcher
{
template <class T,class Stub>
static
std::string
convert( T const & x, Stub )
{
return to_string(x);
}
};
template <> template <>
struct struct
to_string_dispatcher<true> to_string_dispatcher<false>
{ {
template <class T,class Stub> template <class T,class Stub>
static static
std::string std::string
convert( T const & x, Stub ) convert( T const & x, Stub s )
{ {
return to_string(x); std::ostringstream out;
} shift_left_dispatch::dispatch(out,x,s);
}; return out.str();
}
};
template <> namespace
struct to_string_dispatch
to_string_dispatcher<false> {
{ template <class T>
template <class T,class Stub> char to_string( T );
static
std::string
convert( T const & x, Stub s )
{
std::ostringstream out;
shift_left_dispatch::dispatch(out,x,s);
return out.str();
}
};
namespace template <class T,class Stub>
to_string_dispatch std::string
{ dispatch( T const & x, Stub s )
template <class T> {
char to_string( T ); return to_string_dispatcher<1!=sizeof(to_string(x))>::convert(x,s);
}
}
template <class T,class Stub> template <class T>
std::string std::string
dispatch( T const & x, Stub s ) string_stub_dump( T const & x )
{ {
return to_string_dispatcher<1!=sizeof(to_string(x))>::convert(x,s); std::ostringstream s;
} s << "[ type: " << typeid(x).name() << ", size: " << sizeof(T) << ", dump: ";
} size_t n=sizeof(T)>16?16:sizeof(T);
s.fill('0');
s.width(2);
for( unsigned char const * b=reinterpret_cast<unsigned char const *>(&x),* e=b+n; b!=e; ++b )
s << std::setw(2) << std::hex << (unsigned int)*b << " ";
s << "]";
return s.str();
}
}
template <class T> template <class T>
std::string std::string
string_stub_dump( T const & x ) to_string_stub( T const & x )
{ {
std::ostringstream s; return exception_detail::to_string_dispatch::dispatch(x,&exception_detail::string_stub_dump<T>);
s << "[ type: " << typeid(x).name() << ", size: " << sizeof(T) << ", dump: "; }
size_t n=sizeof(T)>16?16:sizeof(T);
s.fill('0');
s.width(2);
for( unsigned char const * b=reinterpret_cast<unsigned char const *>(&x),* e=b+n; b!=e; ++b )
s << std::setw(2) << std::hex << (unsigned int)*b << " ";
s << "]";
return s.str();
}
}
template <class T> template <class T,class Stub>
std::string std::string
to_string_stub( T const & x ) to_string_stub( T const & x, Stub s )
{ {
return exception_detail::to_string_dispatch::dispatch(x,&exception_detail::string_stub_dump<T>); return exception_detail::to_string_dispatch::dispatch(x,s);
} }
template <class T,class Stub> template <class T,class U>
std::string std::string
to_string_stub( T const & x, Stub s ) to_string( std::pair<T,U> const & x )
{ {
return exception_detail::to_string_dispatch::dispatch(x,s); return std::string("(") + to_string(x.first) + ',' + to_string(x.second) + ')';
} }
}
template <class T,class U>
std::string
to_string( std::pair<T,U> const & x )
{
return std::string("(") + to_string(x.first) + ',' + to_string(x.second) + ')';
}
}
#endif #endif

View File

@ -9,32 +9,32 @@
namespace namespace
test test
{ {
class my_exception: public boost::exception { }; class my_exception: public boost::exception { };
typedef boost::error_info<struct tag_my_info,int> my_info; typedef boost::error_info<struct tag_my_info,int> my_info;
void void
test_boost_error_info() test_boost_error_info()
{ {
try try
{ {
throw my_exception() << BOOST_ERROR_INFO << my_info(1); throw my_exception() << BOOST_ERROR_INFO << my_info(1);
} }
catch( catch(
my_exception & x ) my_exception & x )
{ {
BOOST_TEST(1==*boost::get_error_info<my_info>(x)); BOOST_TEST(1==*boost::get_error_info<my_info>(x));
BOOST_TEST(boost::get_error_info<boost::throw_function>(x)); BOOST_TEST(boost::get_error_info<boost::throw_function>(x));
BOOST_TEST(boost::get_error_info<boost::throw_file>(x)); BOOST_TEST(boost::get_error_info<boost::throw_file>(x));
BOOST_TEST(boost::get_error_info<boost::throw_line>(x)); BOOST_TEST(boost::get_error_info<boost::throw_line>(x));
} }
} }
} }
int int
main() main()
{ {
test::test_boost_error_info(); test::test_boost_error_info();
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -8,35 +8,35 @@
struct struct
test_exception: test_exception:
std::exception std::exception
{ {
}; };
int int
main() main()
{ {
try try
{ {
throw boost::enable_exception_cloning(test_exception()); throw boost::enable_exception_cloning(test_exception());
} }
catch( catch(
std::exception & x ) std::exception & x )
{ {
boost::exception_ptr p = boost::clone_exception(x); boost::exception_ptr p = boost::clone_exception(x);
try try
{ {
rethrow_exception(p); rethrow_exception(p);
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
test_exception & ) test_exception & )
{ {
} }
catch( catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
} }
} }
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -8,42 +8,42 @@
#include <boost/detail/lightweight_test.hpp> #include <boost/detail/lightweight_test.hpp>
namespace namespace
{ {
typedef boost::error_info<struct tag_test_int,int> test_int; typedef boost::error_info<struct tag_test_int,int> test_int;
void void
throw_wrapper() throw_wrapper()
{ {
try try
{ {
boost::exception_test::throw_length_error(); boost::exception_test::throw_length_error();
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
x << test_int(42); x << test_int(42);
throw; throw;
} }
} }
} }
int int
main() main()
{ {
try try
{ {
throw_wrapper(); throw_wrapper();
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
std::exception & x ) std::exception & x )
{ {
BOOST_TEST( 42==*boost::get_error_info<test_int>(x) ); BOOST_TEST( 42==*boost::get_error_info<test_int>(x) );
} }
catch( catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
} }
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -11,23 +11,23 @@ typedef boost::error_info<struct tag_errno,int> info_errno;
class class
my_exception: my_exception:
public boost::exception public boost::exception
{ {
}; };
int int
main() main()
{ {
try try
{ {
errno=1; errno=1;
throw my_exception() << info_errno(errno); throw my_exception() << info_errno(errno);
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
my_exception & x ) my_exception & x )
{ {
BOOST_TEST(1==*boost::get_error_info<info_errno>(x)); BOOST_TEST(1==*boost::get_error_info<info_errno>(x));
} }
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -18,200 +18,200 @@ typedef boost::error_info<struct tag_test_6,non_printable> test_6;
struct struct
test_exception: test_exception:
public boost::exception public boost::exception
{ {
}; };
struct struct
throws_on_copy throws_on_copy
{ {
throws_on_copy() throws_on_copy()
{ {
} }
throws_on_copy( throws_on_copy const & ) throws_on_copy( throws_on_copy const & )
{ {
throw test_exception(); throw test_exception();
} }
}; };
void void
basic_test() basic_test()
{ {
test_exception x; test_exception x;
x << test_1(1) << test_2(2u) << test_3(3.14159f); x << test_1(1) << test_2(2u) << test_3(3.14159f);
BOOST_TEST(*boost::get_error_info<test_1>(x)==1); BOOST_TEST(*boost::get_error_info<test_1>(x)==1);
BOOST_TEST(*boost::get_error_info<test_2>(x)==2u); BOOST_TEST(*boost::get_error_info<test_2>(x)==2u);
BOOST_TEST(*boost::get_error_info<test_3>(x)==3.14159f); BOOST_TEST(*boost::get_error_info<test_3>(x)==3.14159f);
BOOST_TEST(!boost::get_error_info<test_4>(x)); BOOST_TEST(!boost::get_error_info<test_4>(x));
} }
void void
exception_safety_test() exception_safety_test()
{ {
test_exception x; test_exception x;
try try
{ {
x << test_4(throws_on_copy()); x << test_4(throws_on_copy());
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
test_exception & ) test_exception & )
{ {
} }
BOOST_TEST(!boost::get_error_info<test_4>(x)); BOOST_TEST(!boost::get_error_info<test_4>(x));
} }
void void
throw_empty() throw_empty()
{ {
throw test_exception(); throw test_exception();
} }
void void
throw_test_1( char const * value ) throw_test_1( char const * value )
{ {
throw test_exception() << test_5(std::string(value)); throw test_exception() << test_5(std::string(value));
} }
void void
throw_test_2() throw_test_2()
{ {
throw test_exception() << test_6(non_printable()); throw test_exception() << test_6(non_printable());
} }
void void
throw_catch_add_file_name( char const * name ) throw_catch_add_file_name( char const * name )
{ {
try try
{ {
throw_empty(); throw_empty();
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
x << test_5(std::string(name)); x << test_5(std::string(name));
throw; throw;
} }
} }
void void
test_empty() test_empty()
{ {
try try
{ {
throw_empty(); throw_empty();
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
BOOST_TEST( dynamic_cast<test_exception *>(&x) ); BOOST_TEST( dynamic_cast<test_exception *>(&x) );
BOOST_TEST( !boost::get_error_info<test_1>(x) ); BOOST_TEST( !boost::get_error_info<test_1>(x) );
} }
try try
{ {
throw_empty(); throw_empty();
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
test_exception & x ) test_exception & x )
{ {
BOOST_TEST( dynamic_cast<boost::exception *>(&x) ); BOOST_TEST( dynamic_cast<boost::exception *>(&x) );
} }
} }
void void
test_basic_throw_catch() test_basic_throw_catch()
{ {
try try
{ {
throw_test_1("test"); throw_test_1("test");
BOOST_ASSERT(false); BOOST_ASSERT(false);
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test")); BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
} }
try try
{ {
throw_test_2(); throw_test_2();
BOOST_ASSERT(false); BOOST_ASSERT(false);
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
BOOST_TEST(boost::get_error_info<test_6>(x)); BOOST_TEST(boost::get_error_info<test_6>(x));
} }
} }
void void
test_catch_add_info() test_catch_add_info()
{ {
try try
{ {
throw_catch_add_file_name("test"); throw_catch_add_file_name("test");
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test")); BOOST_TEST(*boost::get_error_info<test_5>(x)==std::string("test"));
} }
} }
void void
test_add_tuple() test_add_tuple()
{ {
typedef boost::tuple<test_1,test_2> test_12; typedef boost::tuple<test_1,test_2> test_12;
typedef boost::tuple<test_1,test_2,test_3> test_123; typedef boost::tuple<test_1,test_2,test_3> test_123;
typedef boost::tuple<test_1,test_2,test_3,test_5> test_1235; typedef boost::tuple<test_1,test_2,test_3,test_5> test_1235;
try try
{ {
throw test_exception() << test_12(42,42u); throw test_exception() << test_12(42,42u);
} }
catch( catch(
test_exception & x ) test_exception & x )
{ {
BOOST_TEST( *boost::get_error_info<test_1>(x)==42 ); BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
BOOST_TEST( *boost::get_error_info<test_2>(x)==42u ); BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
} }
try try
{ {
throw test_exception() << test_123(42,42u,42.0f); throw test_exception() << test_123(42,42u,42.0f);
} }
catch( catch(
test_exception & x ) test_exception & x )
{ {
BOOST_TEST( *boost::get_error_info<test_1>(x)==42 ); BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
BOOST_TEST( *boost::get_error_info<test_2>(x)==42u ); BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f ); BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
} }
try try
{ {
throw test_exception() << test_1235(42,42u,42.0f,std::string("42")); throw test_exception() << test_1235(42,42u,42.0f,std::string("42"));
} }
catch( catch(
test_exception & x ) test_exception & x )
{ {
BOOST_TEST( *boost::get_error_info<test_1>(x)==42 ); BOOST_TEST( *boost::get_error_info<test_1>(x)==42 );
BOOST_TEST( *boost::get_error_info<test_2>(x)==42u ); BOOST_TEST( *boost::get_error_info<test_2>(x)==42u );
BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f ); BOOST_TEST( *boost::get_error_info<test_3>(x)==42.0f );
BOOST_TEST( *boost::get_error_info<test_5>(x)=="42" ); BOOST_TEST( *boost::get_error_info<test_5>(x)=="42" );
} }
} }
int int
main() main()
{ {
basic_test(); basic_test();
exception_safety_test(); exception_safety_test();
test_empty(); test_empty();
test_basic_throw_catch(); test_basic_throw_catch();
test_catch_add_info(); test_catch_add_info();
test_add_tuple(); test_add_tuple();
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -7,6 +7,6 @@
void void
tester( boost::exception & x ) tester( boost::exception & x )
{ {
throw x; //must not compile. throw x; //must not compile.
} }

View File

@ -8,28 +8,28 @@
class class
test_exception: test_exception:
public boost::exception public boost::exception
{ {
}; };
void void
test_throw() test_throw()
{ {
throw test_exception(); throw test_exception();
} }
int int
main() main()
{ {
try try
{ {
test_throw(); test_throw();
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
test_exception & ) test_exception & )
{ {
BOOST_TEST(true); BOOST_TEST(true);
} }
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -5,17 +5,18 @@
#include <boost/exception/enable_error_info.hpp> #include <boost/exception/enable_error_info.hpp>
#include <stdexcept> #include <stdexcept>
#include <string>
namespace namespace
boost boost
{ {
namespace namespace
exception_test exception_test
{ {
void void
throw_length_error() throw_length_error()
{ {
throw enable_error_info( std::length_error("my length error") ); throw enable_error_info( std::length_error("my length error") );
} }
} }
} }

View File

@ -8,12 +8,12 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_test exception_test
{ {
void throw_length_error(); void throw_length_error();
} }
} }
#endif #endif

View File

@ -8,46 +8,46 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_test exception_test
{ {
inline inline
some_boost_exception:: some_boost_exception::
some_boost_exception( int x ): some_boost_exception( int x ):
x_(x) x_(x)
{ {
} }
some_boost_exception:: some_boost_exception::
~some_boost_exception() throw() ~some_boost_exception() throw()
{ {
} }
inline inline
some_std_exception:: some_std_exception::
some_std_exception( int x ): some_std_exception( int x ):
x_(x) x_(x)
{ {
} }
some_std_exception:: some_std_exception::
~some_std_exception() throw() ~some_std_exception() throw()
{ {
} }
template <> template <>
void void
throw_test_exception<some_boost_exception>( int x ) throw_test_exception<some_boost_exception>( int x )
{ {
boost::throw_exception( some_boost_exception(x) ); boost::throw_exception( some_boost_exception(x) );
} }
template <> template <>
void void
throw_test_exception<some_std_exception>( int x ) throw_test_exception<some_std_exception>( int x )
{ {
boost::throw_exception( some_std_exception(x) ); boost::throw_exception( some_std_exception(x) );
} }
} }
} }

View File

@ -10,38 +10,38 @@
namespace namespace
boost boost
{ {
namespace namespace
exception_test exception_test
{ {
struct struct
some_boost_exception: some_boost_exception:
public boost::exception, public boost::exception,
public std::exception public std::exception
{ {
explicit some_boost_exception( int x ); explicit some_boost_exception( int x );
virtual ~some_boost_exception() throw(); virtual ~some_boost_exception() throw();
int x_; int x_;
}; };
struct struct
some_std_exception: some_std_exception:
public std::exception public std::exception
{ {
explicit some_std_exception( int x ); explicit some_std_exception( int x );
virtual ~some_std_exception() throw(); virtual ~some_std_exception() throw();
int x_; int x_;
}; };
template <class> template <class>
void throw_test_exception( int ); void throw_test_exception( int );
template <> template <>
void throw_test_exception<some_boost_exception>( int ); void throw_test_exception<some_boost_exception>( int );
template <> template <>
void throw_test_exception<some_std_exception>( int ); void throw_test_exception<some_std_exception>( int );
} }
} }
#endif #endif

View File

@ -12,60 +12,60 @@ typedef boost::error_info<struct tag_test_int,int> test_int;
void void
throw_fwd( void (*thrower)(int) ) throw_fwd( void (*thrower)(int) )
{ {
try try
{ {
thrower(42); thrower(42);
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
x << test_int(42); x << test_int(42);
throw; throw;
} }
} }
template <class T> template <class T>
void void
tester() tester()
{ {
try try
{ {
throw_fwd( &boost::exception_test::throw_test_exception<T> ); throw_fwd( &boost::exception_test::throw_test_exception<T> );
BOOST_ASSERT(false); BOOST_ASSERT(false);
} }
catch( catch(
std::exception & x ) std::exception & x )
{ {
boost::exception_ptr p = boost::clone_exception(x); boost::exception_ptr p = boost::clone_exception(x);
try try
{ {
rethrow_exception(p); rethrow_exception(p);
BOOST_TEST(false); BOOST_TEST(false);
} }
catch( catch(
T & y ) T & y )
{ {
BOOST_TEST(*boost::get_error_info<test_int>(y)==42); BOOST_TEST(*boost::get_error_info<test_int>(y)==42);
BOOST_TEST(y.x_==42); BOOST_TEST(y.x_==42);
} }
catch( catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
} }
} }
catch( catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
} }
} }
int int
main() main()
{ {
tester<boost::exception_test::some_boost_exception>(); tester<boost::exception_test::some_boost_exception>();
tester<boost::exception_test::some_std_exception>(); tester<boost::exception_test::some_std_exception>();
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -9,89 +9,89 @@
namespace namespace
n1 n1
{ {
class class
c1 c1
{ {
}; };
std::string std::string
to_string( c1 const & ) to_string( c1 const & )
{ {
return "c1"; return "c1";
} }
} }
namespace namespace
n2 n2
{ {
class class
c2 c2
{ {
}; };
std::ostream & std::ostream &
operator<<( std::ostream & s, c2 const & ) operator<<( std::ostream & s, c2 const & )
{ {
s << "c2"; s << "c2";
return s; return s;
} }
} }
namespace namespace
n3 n3
{ {
class class
c3 c3
{ {
}; };
std::ostream & std::ostream &
operator<<( std::ostream & s, c3 const & ) operator<<( std::ostream & s, c3 const & )
{ {
s << "bad"; s << "bad";
return s; return s;
} }
std::string std::string
to_string( c3 const & ) to_string( c3 const & )
{ {
return "c3"; return "c3";
} }
} }
namespace namespace
boost boost
{ {
class class
to_string_tester to_string_tester
{ {
}; };
} }
template <class T> template <class T>
struct struct
my_stub my_stub
{ {
std::string std::string
operator()( T const & ) operator()( T const & )
{ {
return "stub"; return "stub";
} }
}; };
int int
main() main()
{ {
using namespace boost; using namespace boost;
BOOST_TEST( to_string(5)=="5" ); BOOST_TEST( to_string(5)=="5" );
BOOST_TEST( to_string(n1::c1())=="c1" ); BOOST_TEST( to_string(n1::c1())=="c1" );
BOOST_TEST( to_string(n2::c2())=="c2" ); BOOST_TEST( to_string(n2::c2())=="c2" );
BOOST_TEST( to_string(n3::c3())=="c3" ); BOOST_TEST( to_string(n3::c3())=="c3" );
BOOST_TEST( to_string_stub(5)=="5" ); BOOST_TEST( to_string_stub(5)=="5" );
BOOST_TEST( to_string_stub(n1::c1())=="c1" ); BOOST_TEST( to_string_stub(n1::c1())=="c1" );
BOOST_TEST( to_string_stub(n2::c2())=="c2" ); BOOST_TEST( to_string_stub(n2::c2())=="c2" );
BOOST_TEST( to_string_stub(n3::c3())=="c3" ); BOOST_TEST( to_string_stub(n3::c3())=="c3" );
BOOST_TEST( to_string_stub(to_string_tester(),my_stub<to_string_tester>())=="stub" ); BOOST_TEST( to_string_stub(to_string_tester(),my_stub<to_string_tester>())=="stub" );
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -11,74 +11,74 @@ typedef boost::error_info<struct tag_test,int> test;
struct struct
test_boost_exception: test_boost_exception:
boost::exception boost::exception
{ {
}; };
void void
throw_boost_exception() throw_boost_exception()
{ {
throw test_boost_exception() << test(42); throw test_boost_exception() << test(42);
} }
void void
throw_unknown_exception() throw_unknown_exception()
{ {
struct struct
test_exception: test_exception:
std::exception std::exception
{ {
}; };
throw test_exception(); throw test_exception();
} }
int int
main() main()
{ {
try try
{ {
throw_boost_exception(); throw_boost_exception();
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
boost::exception_ptr ep=boost::clone_exception(x); boost::exception_ptr ep=boost::clone_exception(x);
try try
{ {
rethrow_exception(ep); rethrow_exception(ep);
} }
catch( catch(
boost::unknown_exception & x ) boost::unknown_exception & x )
{ {
BOOST_TEST( 42==*boost::get_error_info<test>(x) ); BOOST_TEST( 42==*boost::get_error_info<test>(x) );
} }
catch( catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
} }
} }
try try
{ {
throw_unknown_exception(); throw_unknown_exception();
} }
catch( catch(
std::exception & x ) std::exception & x )
{ {
boost::exception_ptr ep=boost::clone_exception(x); boost::exception_ptr ep=boost::clone_exception(x);
try try
{ {
rethrow_exception(ep); rethrow_exception(ep);
} }
catch( catch(
boost::unknown_exception & ) boost::unknown_exception & )
{ {
} }
catch( catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
} }
} }
return boost::report_errors(); return boost::report_errors();
} }

View File

@ -10,18 +10,18 @@ typedef boost::error_info<struct tag_test,int> test;
class class
my_exception: my_exception:
public boost::exception public boost::exception
{ {
}; };
int int
main() main()
{ {
my_exception x; my_exception x;
x << test(1); x << test(1);
std::string w1 = x.what(); std::string w1 = x.what();
x << test(2); x << test(2);
std::string w2 = x.what(); std::string w2 = x.what();
BOOST_TEST( w1!=w2 ); BOOST_TEST( w1!=w2 );
return boost::report_errors(); return boost::report_errors();
} }