Documentation and examples update.

[SVN r54831]
This commit is contained in:
Emil Dotchevski
2009-07-09 07:35:15 +00:00
parent 953399846a
commit 2b34528ef0
11 changed files with 6466 additions and 6410 deletions

View File

@ -63,13 +63,13 @@ else return &lt;unspecified-string-value&gt;;</pre>
</ul></div> </ul></div>
<div class="RenoIncludeDIV"><h4>Example:</h4> <div class="RenoIncludeDIV"><h4>Example:</h4>
<p>this is a possible output from the <span class="RenoLink">diagnostic_information</span> function, as used in <i>libs/exception/example/example_io.cpp:</i></p> <p>this is a possible output from the <span class="RenoLink">diagnostic_information</span> function, as used in <i>libs/exception/example/example_io.cpp:</i></p>
<pre>example_io.cpp(83): Throw in function class boost::shared_ptr&lt;struct _iobuf&gt; __cdecl my_fopen(const char *,const char *) <pre>example_io.cpp(70): Throw in function class boost::shared_ptr&lt;struct _iobuf&gt; __cdecl my_fopen(const char *,const char *)
Dynamic exception type: class boost::exception_detail::clone_impl&lt;class fopen_error&gt; Dynamic exception type: class boost::exception_detail::clone_impl&lt;class fopen_error&gt;
std::exception::what: example_io error std::exception::what: example_io error
[struct tag_errno *] = 2, OS says "No such file or directory" [struct boost::<span class="RenoLink"><a href="errinfo_api_function.html">errinfo_api_function</a></span>_ *] = fopen
[struct tag_file_name *] = tmp1.txt [struct boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>_ *] = 2, "No such file or directory"
[struct tag_function *] = fopen [struct boost::<span class="RenoLink"><a href="errinfo_file_name.html">errinfo_file_name</a></span>_ *] = tmp1.txt
[struct tag_open_mode *] = rb</pre> [struct boost::<span class="RenoLink"><a href="errinfo_file_open_mode.html">errinfo_file_open_mode</a></span>_ *] = rb</pre>
</div></div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div> </div></div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div>
<h3>See Also:</h3> <h3>See Also:</h3>
<div class="RenoPageList"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION<br/> <div class="RenoPageList"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION<br/>

File diff suppressed because it is too large Load Diff

View File

@ -47,13 +47,13 @@ g()
}</pre> }</pre>
<div class="RenoIncludeDIV"><h4>Example:</h4> <div class="RenoIncludeDIV"><h4>Example:</h4>
<p>this is a possible output from the <span class="RenoLink"><a href="diagnostic_information.html">diagnostic_information</a></span> function, as used in <i>libs/exception/example/example_io.cpp:</i></p> <p>this is a possible output from the <span class="RenoLink"><a href="diagnostic_information.html">diagnostic_information</a></span> function, as used in <i>libs/exception/example/example_io.cpp:</i></p>
<pre>example_io.cpp(83): Throw in function class boost::shared_ptr&lt;struct _iobuf&gt; __cdecl my_fopen(const char *,const char *) <pre>example_io.cpp(70): Throw in function class boost::shared_ptr&lt;struct _iobuf&gt; __cdecl my_fopen(const char *,const char *)
Dynamic exception type: class boost::exception_detail::clone_impl&lt;class fopen_error&gt; Dynamic exception type: class boost::exception_detail::clone_impl&lt;class fopen_error&gt;
std::exception::what: example_io error std::exception::what: example_io error
[struct tag_errno *] = 2, OS says "No such file or directory" [struct boost::<span class="RenoLink"><a href="errinfo_api_function.html">errinfo_api_function</a></span>_ *] = fopen
[struct tag_file_name *] = tmp1.txt [struct boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>_ *] = 2, "No such file or directory"
[struct tag_function *] = fopen [struct boost::<span class="RenoLink"><a href="errinfo_file_name.html">errinfo_file_name</a></span>_ *] = tmp1.txt
[struct tag_open_mode *] = rb</pre> [struct boost::<span class="RenoLink"><a href="errinfo_file_open_mode.html">errinfo_file_open_mode</a></span>_ *] = rb</pre>
</div></div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div> </div></div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div>
<h3>See Also:</h3> <h3>See Also:</h3>
<div class="RenoPageList"><a href="boost-exception.html">Boost Exception<br/> <div class="RenoPageList"><a href="boost-exception.html">Boost Exception<br/>

View File

@ -28,11 +28,10 @@
</div> </div>
<p>Here is how cloning can be enabled in a throw-expression (15.1):</p> <p>Here is how cloning can be enabled in a throw-expression (15.1):</p>
<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_info_hpp.html">boost/exception/info.hpp</a></span>&gt; <pre>#include &lt;<span class="RenoLink"><a href="boost_exception_info_hpp.html">boost/exception/info.hpp</a></span>&gt;
#include &lt;<span class="RenoLink"><a href="boost_exception_errinfo_errno_hpp.html">boost/exception/errinfo_errno.hpp</a></span>&gt;
#include &lt;stdio.h&gt; #include &lt;stdio.h&gt;
#include &lt;errno.h&gt; #include &lt;errno.h&gt;
typedef boost::error_info&lt;struct tag_errno,int&gt; errno_info;
class file_read_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { }; class file_read_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { };
void void
@ -40,7 +39,7 @@ 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::<span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span>(file_read_error()) &lt;&lt; throw boost::<span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span>(file_read_error()) &lt;&lt;
errno_info(errno); boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>(errno);
}</pre> }</pre>
<p>Of course, <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> may be used with any exception type; there is no requirement that it should derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.</p> <p>Of course, <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> may be used with any exception type; there is no requirement that it should derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.</p>
</div><div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Cloning and Re-Throwing an Exception</h3> </div><div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Cloning and Re-Throwing an Exception</h3>

View File

@ -29,24 +29,22 @@
<div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Adding of Arbitrary Data at the Point of the Throw</h3> <div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Adding of Arbitrary Data at the Point of the Throw</h3>
</div> </div>
<p>The following example demonstrates how errno can be stored in exception objects using Boost Exception:</p> <p>The following example demonstrates how errno can be stored in exception objects using Boost Exception:</p>
<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_hpp.html">boost/exception.hpp</a></span>&gt; <pre>#include &lt;<span class="RenoLink"><a href="boost_exception_all_hpp.html">boost/exception/all.hpp</a></span>&gt;
#include &lt;errno.h&gt;
#include &lt;iostream&gt; #include &lt;iostream&gt;
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_errno,int&gt; errno_info; //(1) typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_my_info,int&gt; my_info; //(1)
class my_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, public std::exception { }; //(2) class my_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, public std::exception { }; //(2)
void void
f() f()
{ {
throw my_error() &lt;&lt; errno_info(errno); //(3) throw my_error() &lt;&lt; my_info(42); //(3)
} }</pre>
</pre> <p>First, we instantiate the <span class="RenoLink"><a href="error_info.html">error_info</a></span> template using a unique identifier -- tag_my_info, and the type of the info it identifies -- int. This provides compile-time type safety for the various values stored in exception objects.</p>
<p>First, we instantiate the <span class="RenoLink"><a href="error_info.html">error_info</a></span> template using a unique identifier -- tag_errno, and the type of the info it identifies -- int. This provides compile-time type safety for the various values stored in exception objects.</p>
<p>Second, we define class my_error, which derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.</p> <p>Second, we define class my_error, which derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.</p>
<p>Finally, (3) illustrates how the typedef from (1) can be used with <span class="RenoLink"><a href="exception_operator_shl.html">operator&lt;&lt;</a></span> to store values in exception objects at the point of the throw.</p> <p>Finally, (3) illustrates how the typedef from (1) can be used with <span class="RenoLink"><a href="exception_operator_shl.html">operator&lt;&lt;</a></span> to store values in exception objects at the point of the throw.</p>
<p>The stored errno value can be recovered at a later time like this:</p> <p>The stored my_info value can be recovered at a later time like this:</p>
<pre>// ...continued <pre>// ...continued
void void
@ -59,8 +57,8 @@ g()
catch( catch(
my_error &amp; x ) my_error &amp; x )
{ {
if( int const * err=boost::<span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span>&lt;errno_info&gt;(x) ) if( int const * mi=boost::<span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span>&lt;my_info&gt;(x) )
std::cerr &lt;&lt; "Error code: " &lt;&lt; *err; std::cerr &lt;&lt; "My info: " &lt;&lt; *mi;
} }
}</pre> }</pre>
<p>The <span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span> function template is instantiated with the typedef from (1), and is passed an exception object of a polymorphic type. If the exception object contains the requested value, err will point to it; otherwise a null pointer is returned.</p> <p>The <span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span> function template is instantiated with the typedef from (1), and is passed an exception object of a polymorphic type. If the exception object contains the requested value, err will point to it; otherwise a null pointer is returned.</p>
@ -75,28 +73,25 @@ file_read( FILE * f, void * buffer, size_t size )
}</pre> }</pre>
<p>How can the file_read function pass a file name to the exception type constructor? All it has is a FILE handle.</p> <p>How can the file_read function pass a file name to the exception type constructor? All it has is a FILE handle.</p>
<p>Using boost::<span class="RenoLink"><a href="exception.html">exception</a></span> allows us to free the file_read function from the burden of storing the file name in exceptions it throws:</p> <p>Using boost::<span class="RenoLink"><a href="exception.html">exception</a></span> allows us to free the file_read function from the burden of storing the file name in exceptions it throws:</p>
<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_hpp.html">boost/exception.hpp</a></span>&gt; <pre><span class="RenoLink"><a href="boost_exception_all_hpp.html">boost/exception/all.hpp</a></span>&gt;
#include &lt;boost/shared_ptr.hpp&gt;
#include &lt;stdio.h&gt; #include &lt;stdio.h&gt;
#include &lt;errno.h&gt; #include &lt;errno.h&gt;
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_errno,int&gt; errno_info;
class file_read_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { }; class file_read_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { };
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() &lt;&lt; errno_info(errno); throw file_read_error() &lt;&lt; boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>(errno);
}</pre> }</pre>
<p>If file_read detects a failure, it throws an exception which contains the information that is available at the time, namely the errno. Other relevant information, such as the file name, can be added in a context higher up the call stack, where it is known naturally:</p> <p>If file_read detects a failure, it throws an exception which contains the information that is available at the time, namely the errno. Other relevant information, such as the file name, can be added in a context higher up the call stack, where it is known naturally:</p>
<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_hpp.html">boost/exception.hpp</a></span>&gt; <pre>#include &lt;<span class="RenoLink"><a href="boost_exception_all_hpp.html">boost/exception/all.hpp</a></span>&gt;
#include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt;
#include &lt;stdio.h&gt; #include &lt;stdio.h&gt;
#include &lt;string&gt; #include &lt;string&gt;
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_file_name,std::string&gt; file_name_info;
boost::shared_ptr&lt;FILE&gt; file_open( char const * file_name, char const * mode ); boost::shared_ptr&lt;FILE&gt; file_open( char const * file_name, char const * mode );
void file_read( FILE * f, void * buffer, size_t size ); void file_read( FILE * f, void * buffer, size_t size );
@ -113,7 +108,7 @@ parse_file( char const * file_name )
catch( catch(
boost::<span class="RenoLink"><a href="exception.html">exception</a></span> &amp; e ) boost::<span class="RenoLink"><a href="exception.html">exception</a></span> &amp; e )
{ {
e &lt;&lt; file_name_info(file_name); e &lt;&lt; boost::<span class="RenoLink"><a href="errinfo_file_name.html">errinfo_file_name</a></span>(file_name);
throw; throw;
} }
}</pre> }</pre>
@ -122,15 +117,15 @@ parse_file( char const * file_name )
</div> </div>
<p>The code snippet below demonstrates how boost::<span class="RenoLink"><a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html">tuple</a></span> can be used to bundle the name of the function that failed, together with the reported errno so that they can be added to exception objects more conveniently together:</p> <p>The code snippet below demonstrates how boost::<span class="RenoLink"><a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html">tuple</a></span> can be used to bundle the name of the function that failed, together with the reported errno so that they can be added to exception objects more conveniently together:</p>
<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_info_tuple_hpp.html">boost/exception/info_tuple.hpp</a></span>&gt; <pre>#include &lt;<span class="RenoLink"><a href="boost_exception_info_tuple_hpp.html">boost/exception/info_tuple.hpp</a></span>&gt;
#include &lt;<span class="RenoLink"><a href="boost_exception_errinfo_file_name_hpp.html">boost/exception/errinfo_file_name.hpp</a></span>&gt;
#include &lt;<span class="RenoLink"><a href="boost_exception_errinfo_api_function_hpp.html">boost/exception/errinfo_api_function.hpp</a></span>&gt;
#include &lt;<span class="RenoLink"><a href="boost_exception_errinfo_errno_hpp.html">boost/exception/errinfo_errno.hpp</a></span>&gt;
#include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt;
#include &lt;stdio.h&gt; #include &lt;stdio.h&gt;
#include &lt;string&gt; #include &lt;string&gt;
#include &lt;errno.h&gt; #include &lt;errno.h&gt;
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_file_name,std::string&gt; file_name_info; typedef boost::<span class="RenoLink"><a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html">tuple</a></span>&lt;boost::<span class="RenoLink"><a href="errinfo_api_function.html">errinfo_api_function</a></span>,boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>&gt; clib_failure;
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_function,char const *&gt; function_info;
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_errno,int&gt; errno_info;
typedef boost::tuple&lt;function_info,errno_info&gt; clib_failure;
class file_open_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { }; class file_open_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { };
@ -141,7 +136,7 @@ file_open( char const * name, char const * mode )
return boost::shared_ptr&lt;FILE&gt;(f,fclose); return boost::shared_ptr&lt;FILE&gt;(f,fclose);
else else
throw file_open_error() &lt;&lt; throw file_open_error() &lt;&lt;
file_name_info(name) &lt;&lt; boost::<span class="RenoLink"><a href="errinfo_file_name.html">errinfo_file_name</a></span>(name) &lt;&lt;
clib_failure("fopen",errno); clib_failure("fopen",errno);
}</pre> }</pre>
<p>Note that the members of a boost::<span class="RenoLink"><a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html">tuple</a></span> are stored separately in exception objects; they can only be retrieved individually, using <span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span>.</p> <p>Note that the members of a boost::<span class="RenoLink"><a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html">tuple</a></span> are stored separately in exception objects; they can only be retrieved individually, using <span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span>.</p>

View File

@ -6,11 +6,10 @@
//This example shows how to enable cloning when throwing a boost::exception. //This example shows how to enable cloning when throwing a boost::exception.
#include <boost/exception/info.hpp> #include <boost/exception/info.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
typedef boost::error_info<struct tag_errno,int> errno_info;
class file_read_error: public boost::exception { }; class file_read_error: public boost::exception { };
void void
@ -18,5 +17,5 @@ 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_current_exception(file_read_error()) << throw boost::enable_current_exception(file_read_error()) <<
errno_info(errno); boost::errinfo_errno(errno);
} }

View File

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

View File

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

View File

@ -3,16 +3,17 @@
//Distributed under the Boost Software License, Version 1.0. (See accompanying //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) //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. //This program simulates errors on copying simple data files. It demonstrates
//It demonstrates how the proposed Boost exception library can be used. //typical Boost Exception usage.
//
//The documentation for boost::exception can be found at: //The output from this program can vary depending on the platform.
//
// http://www.revergestudios.com/boost-exception/boost-exception.htm.
//
//The output from this program can vary depending on the compiler/OS combination.
#include <boost/exception.hpp> #include <boost/exception.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/shared_ptr.hpp>
#include <boost/weak_ptr.hpp> #include <boost/weak_ptr.hpp>
#include <stdio.h> #include <stdio.h>
@ -21,22 +22,8 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
typedef boost::error_info<struct tag_errno,int> errno_info; typedef boost::error_info<struct tag_file_name_src,std::string> errinfo_src_file_name;
typedef boost::error_info<struct tag_file_stream,boost::weak_ptr<FILE> > file_stream_info; typedef boost::error_info<struct tag_file_name_dst,std::string> errinfo_dst_file_name;
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();
}
char const data[] = "example"; char const data[] = "example";
size_t const data_size = sizeof(data); size_t const data_size = sizeof(data);
@ -77,10 +64,10 @@ my_fopen( char const * name, char const * mode )
return boost::shared_ptr<FILE>(f,fclose); return boost::shared_ptr<FILE>(f,fclose);
else else
BOOST_THROW_EXCEPTION(fopen_error() << BOOST_THROW_EXCEPTION(fopen_error() <<
errno_info(errno) << boost::errinfo_errno (errno) <<
file_name_info(name) << boost::errinfo_file_name(name) <<
open_mode_info(mode) << boost::errinfo_file_open_mode(mode) <<
function_info("fopen")); boost::errinfo_api_function("fopen"));
} }
void void
@ -89,9 +76,9 @@ my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> cons
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()) )
BOOST_THROW_EXCEPTION(fread_error() << BOOST_THROW_EXCEPTION(fread_error() <<
function_info("fread") << boost::errinfo_api_function("fread") <<
errno_info(errno) << boost::errinfo_errno(errno) <<
file_stream_info(boost::weak_ptr<FILE>(stream))); boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
} }
void void
@ -100,9 +87,9 @@ my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FIL
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()) )
BOOST_THROW_EXCEPTION(fwrite_error() << BOOST_THROW_EXCEPTION(fwrite_error() <<
function_info("fwrite") << boost::errinfo_api_function("fwrite") <<
errno_info(errno) << boost::errinfo_errno(errno) <<
file_stream_info(boost::weak_ptr<FILE>(stream))); boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
} }
void void
@ -132,17 +119,17 @@ copy_data( char const * src_file_name, char const * dst_file_name )
catch( catch(
boost::exception & x ) 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( boost::shared_ptr<FILE> fs = f->lock() )
{ {
if( fs==src ) if( fs==src )
x << file_name_info(src_file_name); x << boost::errinfo_file_name(src_file_name);
else if( fs==dst ) else if( fs==dst )
x << file_name_info(dst_file_name); x << boost::errinfo_file_name(dst_file_name);
} }
x << x <<
file_name_src_info(src_file_name) << errinfo_src_file_name(src_file_name) <<
file_name_dst_info(dst_file_name); errinfo_dst_file_name(dst_file_name);
throw; throw;
} }
} }
@ -150,25 +137,25 @@ copy_data( char const * src_file_name, char const * dst_file_name )
void void
dump_copy_info( boost::exception const & x ) 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"; 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"; std::cerr << "Destination file name: " << *dst << "\n";
} }
void void
dump_file_info( boost::exception const & x ) 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"; std::cerr << "File name: " << *fn << "\n";
} }
void void
dump_clib_info( boost::exception const & x ) 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"; 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"; std::cerr << "Failed function: " << *fn << "\n";
} }

View File

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

View File

@ -3,7 +3,7 @@
//Distributed under the Boost Software License, Version 1.0. (See accompanying //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) //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/all.hpp> #include <boost/exception/all.hpp>
#include <iostream> #include <iostream>