Merging in changes trunk updates: adding standard error_info typedefs, updating the documentation.

[SVN r55094]
This commit is contained in:
Emil Dotchevski
2009-07-22 20:55:50 +00:00
parent 6a8b96d9c3
commit 0f5a51238b
64 changed files with 13772 additions and 8727 deletions

View File

@ -13,3 +13,4 @@ obj cloning_2 : cloning_2.cpp : <threading>multi ;
obj info_tuple : info_tuple.cpp ;
obj enable_error_info : enable_error_info.cpp ;
obj logging : logging.cpp ;
obj errinfos : errinfos.cpp ;

View File

@ -6,17 +6,16 @@
//This example shows how to enable cloning when throwing a boost::exception.
#include <boost/exception/info.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <stdio.h>
#include <errno.h>
typedef boost::error_info<struct tag_errno,int> errno_info;
class file_read_error: public boost::exception { };
struct file_read_error: virtual boost::exception { };
void
file_read( FILE * f, void * buffer, size_t size )
{
if( size!=fread(buffer,1,size,f) )
throw boost::enable_current_exception(file_read_error()) <<
errno_info(errno);
boost::errinfo_errno(errno);
}

View File

@ -7,7 +7,7 @@
//transporting of arbitrary data to the catch site, even for types
//that do not derive from boost::exception.
#include <boost/exception.hpp>
#include <boost/exception/all.hpp>
#include <stdexcept>
typedef boost::error_info<struct tag_std_range_min,size_t> std_range_min;

53
example/errinfos.cpp Normal file
View File

@ -0,0 +1,53 @@
//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)
//This example demonstrates the intended use of various commonly used
//error_info typedefs provided by Boost Exception.
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_at_line.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_handle.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/info.hpp>
#include <boost/throw_exception.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <stdio.h>
#include <errno.h>
#include <exception>
struct error : virtual std::exception, virtual boost::exception { };
struct file_error : virtual error { };
struct file_open_error: virtual file_error { };
struct file_read_error: virtual file_error { };
boost::shared_ptr<FILE>
open_file( char const * file, char const * mode )
{
if( FILE * f=fopen(file,mode) )
return boost::shared_ptr<FILE>(f,fclose);
else
BOOST_THROW_EXCEPTION(
file_open_error() <<
boost::errinfo_api_function("fopen") <<
boost::errinfo_errno(errno) <<
boost::errinfo_file_name(file) <<
boost::errinfo_file_open_mode(mode) );
}
size_t
read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size )
{
size_t nr=fread(buf,1,size,f.get());
if( ferror(f.get()) )
BOOST_THROW_EXCEPTION(
file_read_error() <<
boost::errinfo_api_function("fread") <<
boost::errinfo_errno(errno) <<
boost::errinfo_file_handle(f) );
return nr;
}

View File

@ -6,18 +6,17 @@
//This example shows how to add data to boost::exception objects at the
//point of the throw, and how to retrieve that data at the point of the catch.
#include <boost/exception.hpp>
#include <errno.h>
#include <boost/exception/all.hpp>
#include <iostream>
typedef boost::error_info<struct tag_errno,int> errno_info; //(1)
typedef boost::error_info<struct tag_my_info,int> my_info; //(1)
class my_error: public boost::exception, public std::exception { }; //(2)
struct my_error: virtual boost::exception, virtual std::exception { }; //(2)
void
f()
{
throw my_error() << errno_info(errno); //(3)
throw my_error() << my_info(42); //(3)
}
void
@ -30,7 +29,7 @@ g()
catch(
my_error & x )
{
if( int const * err=boost::get_error_info<errno_info>(x) )
std::cerr << "Error code: " << *err;
if( int const * mi=boost::get_error_info<my_info>(x) )
std::cerr << "My info: " << *mi;
}
}

View File

@ -5,28 +5,24 @@
//This example shows how to add arbitrary data to active exception objects.
#include <boost/exception.hpp>
#include <boost/exception/all.hpp>
#include <boost/shared_ptr.hpp>
#include <stdio.h>
#include <errno.h>
//
typedef boost::error_info<struct tag_errno,int> errno_info;
class file_read_error: public boost::exception { };
struct file_read_error: virtual boost::exception { };
void
file_read( FILE * f, void * buffer, size_t size )
{
if( size!=fread(buffer,1,size,f) )
throw file_read_error() << errno_info(errno);
throw file_read_error() << boost::errinfo_errno(errno);
}
//
typedef boost::error_info<struct tag_file_name,std::string> file_name_info;
boost::shared_ptr<FILE> file_open( char const * file_name, char const * mode );
void file_read( FILE * f, void * buffer, size_t size );
@ -43,7 +39,7 @@ parse_file( char const * file_name )
catch(
boost::exception & e )
{
e << file_name_info(file_name);
e << boost::errinfo_file_name(file_name);
throw;
}
}

View File

@ -3,48 +3,34 @@
//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)
//This program simulates errors on copying simple data files.
//It demonstrates how the proposed Boost exception library can be used.
//
//The documentation for boost::exception can be found at:
//
// http://www.revergestudios.com/boost-exception/boost-exception.htm.
//
//The output from this program can vary depending on the compiler/OS combination.
//This program simulates errors on copying simple data files. It demonstrates
//typical Boost Exception usage.
#include <boost/exception.hpp>
//The output from this program can vary depending on the platform.
#include <boost/throw_exception.hpp>
#include <boost/exception/info.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/errinfo_file_handle.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <stdio.h>
#include <errno.h>
#include <string>
#include <iostream>
#include <sstream>
typedef boost::error_info<struct tag_errno,int> errno_info;
typedef boost::error_info<struct tag_file_stream,boost::weak_ptr<FILE> > file_stream_info;
typedef boost::error_info<struct tag_open_mode,std::string> open_mode_info; //The open mode of a failed fopen request.
typedef boost::error_info<struct tag_file_name,std::string> file_name_info; //The file name of a failed file operation.
typedef boost::error_info<struct tag_file_name_src,std::string> file_name_src_info; //The source file name of a failed copy operation.
typedef boost::error_info<struct tag_file_name_dst,std::string> file_name_dst_info; //The destination file name of a failed copy operation.
typedef boost::error_info<struct tag_function,std::string> function_info; //The name of the C function which reported the failure.
std::string
to_string( errno_info const & e )
{
int en=e.value();
std::ostringstream s;
s << en << ", OS says \"" << strerror(en) << "\"";
return s.str();
}
typedef boost::error_info<struct tag_file_name_src,std::string> errinfo_src_file_name;
typedef boost::error_info<struct tag_file_name_dst,std::string> errinfo_dst_file_name;
char const data[] = "example";
size_t const data_size = sizeof(data);
class
error: //Base for all exception objects we throw.
public std::exception,
public boost::exception
public virtual std::exception,
public virtual boost::exception
{
public:
@ -61,14 +47,12 @@ error: //Base for all exception objects we throw.
}
};
class open_error: public error { };
class read_error: public error { };
class write_error: public error { };
class fopen_error: public open_error { };
class fread_error: public read_error { };
class fwrite_error: public write_error { };
struct open_error: virtual error { };
struct read_error: virtual error { };
struct write_error: virtual error { };
struct fopen_error: virtual open_error { };
struct fread_error: virtual read_error { };
struct fwrite_error: virtual write_error { };
boost::shared_ptr<FILE>
my_fopen( char const * name, char const * mode )
@ -77,10 +61,10 @@ my_fopen( char const * name, char const * mode )
return boost::shared_ptr<FILE>(f,fclose);
else
BOOST_THROW_EXCEPTION(fopen_error() <<
errno_info(errno) <<
file_name_info(name) <<
open_mode_info(mode) <<
function_info("fopen"));
boost::errinfo_errno (errno) <<
boost::errinfo_file_name(name) <<
boost::errinfo_file_open_mode(mode) <<
boost::errinfo_api_function("fopen"));
}
void
@ -89,9 +73,9 @@ my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> cons
assert(stream);
if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) )
BOOST_THROW_EXCEPTION(fread_error() <<
function_info("fread") <<
errno_info(errno) <<
file_stream_info(boost::weak_ptr<FILE>(stream)));
boost::errinfo_api_function("fread") <<
boost::errinfo_errno(errno) <<
boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
}
void
@ -100,9 +84,9 @@ my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FIL
assert(stream);
if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) )
BOOST_THROW_EXCEPTION(fwrite_error() <<
function_info("fwrite") <<
errno_info(errno) <<
file_stream_info(boost::weak_ptr<FILE>(stream)));
boost::errinfo_api_function("fwrite") <<
boost::errinfo_errno(errno) <<
boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
}
void
@ -132,17 +116,17 @@ copy_data( char const * src_file_name, char const * dst_file_name )
catch(
boost::exception & x )
{
if( boost::weak_ptr<FILE> const * f=boost::get_error_info<file_stream_info>(x) )
if( boost::weak_ptr<FILE> const * f=boost::get_error_info<boost::errinfo_file_handle>(x) )
if( boost::shared_ptr<FILE> fs = f->lock() )
{
if( fs==src )
x << file_name_info(src_file_name);
x << boost::errinfo_file_name(src_file_name);
else if( fs==dst )
x << file_name_info(dst_file_name);
x << boost::errinfo_file_name(dst_file_name);
}
x <<
file_name_src_info(src_file_name) <<
file_name_dst_info(dst_file_name);
errinfo_src_file_name(src_file_name) <<
errinfo_dst_file_name(dst_file_name);
throw;
}
}
@ -150,25 +134,25 @@ copy_data( char const * src_file_name, char const * dst_file_name )
void
dump_copy_info( boost::exception const & x )
{
if( std::string const * src = boost::get_error_info<file_name_src_info>(x) )
if( std::string const * src = boost::get_error_info<errinfo_src_file_name>(x) )
std::cerr << "Source file name: " << *src << "\n";
if( std::string const * dst = boost::get_error_info<file_name_dst_info>(x) )
if( std::string const * dst = boost::get_error_info<errinfo_dst_file_name>(x) )
std::cerr << "Destination file name: " << *dst << "\n";
}
void
dump_file_info( boost::exception const & x )
{
if( std::string const * fn = boost::get_error_info<file_name_info>(x) )
if( std::string const * fn = boost::get_error_info<boost::errinfo_file_name>(x) )
std::cerr << "File name: " << *fn << "\n";
}
void
dump_clib_info( boost::exception const & x )
{
if( int const * err=boost::get_error_info<errno_info>(x) )
if( int const * err=boost::get_error_info<boost::errinfo_errno>(x) )
std::cerr << "OS error: " << *err << "\n";
if( std::string const * fn=boost::get_error_info<function_info>(x) )
if( char const * const * fn=boost::get_error_info<boost::errinfo_api_function>(x) )
std::cerr << "Failed function: " << *fn << "\n";
}

View File

@ -3,21 +3,21 @@
//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)
//This example shows how boost::error_info_group can be used to bundle
//the name of the function that fails together with the reported errno.
//This example shows how boost::tuple can be used to bundle the
//name of the function that fails together with the reported errno.
#include <boost/exception/info_tuple.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/shared_ptr.hpp>
#include <stdio.h>
#include <string>
#include <errno.h>
typedef boost::error_info<struct tag_file_name,std::string> file_name_info;
typedef boost::error_info<struct tag_function,char const *> function_info;
typedef boost::error_info<struct tag_errno,int> errno_info;
typedef boost::tuple<function_info,errno_info> clib_failure;
typedef boost::tuple<boost::errinfo_api_function,boost::errinfo_errno> clib_failure;
class file_open_error: public boost::exception { };
struct file_open_error: virtual boost::exception { };
boost::shared_ptr<FILE>
file_open( char const * name, char const * mode )
@ -26,6 +26,6 @@ file_open( char const * name, char const * mode )
return boost::shared_ptr<FILE>(f,fclose);
else
throw file_open_error() <<
file_name_info(name) <<
boost::errinfo_file_name(name) <<
clib_failure("fopen",errno);
}

View File

@ -3,9 +3,9 @@
//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)
//This example shows to print all data contained in a boost::exception.
//This example shows how to print all data contained in a boost::exception.
#include <boost/exception.hpp>
#include <boost/exception/all.hpp>
#include <iostream>
void f(); //throws unknown types that derive from boost::exception.