summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/exception/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/exception/example')
-rw-r--r--src/boost/libs/exception/example/Jamfile16
-rw-r--r--src/boost/libs/exception/example/cloning_1.cpp21
-rw-r--r--src/boost/libs/exception/example/cloning_2.cpp39
-rw-r--r--src/boost/libs/exception/example/enable_error_info.cpp35
-rw-r--r--src/boost/libs/exception/example/errinfos.cpp53
-rw-r--r--src/boost/libs/exception/example/error_info_1.cpp35
-rw-r--r--src/boost/libs/exception/example/error_info_2.cpp45
-rw-r--r--src/boost/libs/exception/example/example_io.cpp209
-rw-r--r--src/boost/libs/exception/example/info_tuple.cpp31
-rw-r--r--src/boost/libs/exception/example/logging.cpp25
10 files changed, 509 insertions, 0 deletions
diff --git a/src/boost/libs/exception/example/Jamfile b/src/boost/libs/exception/example/Jamfile
new file mode 100644
index 000000000..44e18bfd9
--- /dev/null
+++ b/src/boost/libs/exception/example/Jamfile
@@ -0,0 +1,16 @@
+# Boost Exception Library example Jamfile
+#
+# 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)
+
+exe example_io : example_io.cpp ;
+obj error_info_1 : error_info_1.cpp ;
+obj error_info_2 : error_info_2.cpp ;
+obj cloning_1 : cloning_1.cpp ;
+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 ;
diff --git a/src/boost/libs/exception/example/cloning_1.cpp b/src/boost/libs/exception/example/cloning_1.cpp
new file mode 100644
index 000000000..16103a9d9
--- /dev/null
+++ b/src/boost/libs/exception/example/cloning_1.cpp
@@ -0,0 +1,21 @@
+//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 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>
+
+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()) <<
+ boost::errinfo_errno(errno);
+ }
diff --git a/src/boost/libs/exception/example/cloning_2.cpp b/src/boost/libs/exception/example/cloning_2.cpp
new file mode 100644
index 000000000..79b56cbbe
--- /dev/null
+++ b/src/boost/libs/exception/example/cloning_2.cpp
@@ -0,0 +1,39 @@
+//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 shows how to transport cloning-enabled boost::exceptions between threads.
+
+#include <boost/exception_ptr.hpp>
+#include <boost/thread.hpp>
+#include <boost/bind.hpp>
+
+void do_work(); //throws cloning-enabled boost::exceptions
+
+void
+worker_thread( boost::exception_ptr & error )
+ {
+ try
+ {
+ do_work();
+ error = boost::exception_ptr();
+ }
+ catch(
+ ... )
+ {
+ error = boost::current_exception();
+ }
+ }
+
+// ...continued
+
+void
+work()
+ {
+ boost::exception_ptr error;
+ boost::thread t( boost::bind(worker_thread,boost::ref(error)) );
+ t.join();
+ if( error )
+ boost::rethrow_exception(error);
+ }
diff --git a/src/boost/libs/exception/example/enable_error_info.cpp b/src/boost/libs/exception/example/enable_error_info.cpp
new file mode 100644
index 000000000..5f9a69147
--- /dev/null
+++ b/src/boost/libs/exception/example/enable_error_info.cpp
@@ -0,0 +1,35 @@
+//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 shows how to throw exception objects that support
+//transporting of arbitrary data to the catch site, even for types
+//that do not derive from boost::exception.
+
+#include <boost/exception/all.hpp>
+#include <stdexcept>
+
+typedef boost::error_info<struct tag_std_range_min,size_t> std_range_min;
+typedef boost::error_info<struct tag_std_range_max,size_t> std_range_max;
+typedef boost::error_info<struct tag_std_range_index,size_t> std_range_index;
+
+template <class T>
+class
+my_container
+ {
+ public:
+
+ size_t size() const;
+
+ T const &
+ operator[]( size_t i ) const
+ {
+ if( i > size() )
+ throw boost::enable_error_info(std::range_error("Index out of range")) <<
+ std_range_min(0) <<
+ std_range_max(size()) <<
+ std_range_index(i);
+ //....
+ }
+ };
diff --git a/src/boost/libs/exception/example/errinfos.cpp b/src/boost/libs/exception/example/errinfos.cpp
new file mode 100644
index 000000000..2b3cff1bc
--- /dev/null
+++ b/src/boost/libs/exception/example/errinfos.cpp
@@ -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;
+ }
diff --git a/src/boost/libs/exception/example/error_info_1.cpp b/src/boost/libs/exception/example/error_info_1.cpp
new file mode 100644
index 000000000..05339e918
--- /dev/null
+++ b/src/boost/libs/exception/example/error_info_1.cpp
@@ -0,0 +1,35 @@
+//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 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/all.hpp>
+#include <iostream>
+
+typedef boost::error_info<struct tag_my_info,int> my_info; //(1)
+
+struct my_error: virtual boost::exception, virtual std::exception { }; //(2)
+
+void
+f()
+ {
+ throw my_error() << my_info(42); //(3)
+ }
+
+void
+g()
+ {
+ try
+ {
+ f();
+ }
+ catch(
+ my_error & x )
+ {
+ if( int const * mi=boost::get_error_info<my_info>(x) )
+ std::cerr << "My info: " << *mi;
+ }
+ }
diff --git a/src/boost/libs/exception/example/error_info_2.cpp b/src/boost/libs/exception/example/error_info_2.cpp
new file mode 100644
index 000000000..dea512a9c
--- /dev/null
+++ b/src/boost/libs/exception/example/error_info_2.cpp
@@ -0,0 +1,45 @@
+//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 shows how to add arbitrary data to active exception objects.
+
+#include <boost/exception/all.hpp>
+#include <boost/shared_ptr.hpp>
+#include <stdio.h>
+#include <errno.h>
+
+//
+
+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() << boost::errinfo_errno(errno);
+ }
+
+//
+
+boost::shared_ptr<FILE> file_open( char const * file_name, char const * mode );
+void file_read( FILE * f, void * buffer, size_t size );
+
+void
+parse_file( char const * file_name )
+ {
+ boost::shared_ptr<FILE> f = file_open(file_name,"rb");
+ assert(f);
+ try
+ {
+ char buf[1024];
+ file_read( f.get(), buf, sizeof(buf) );
+ }
+ catch(
+ boost::exception & e )
+ {
+ e << boost::errinfo_file_name(file_name);
+ throw;
+ }
+ }
diff --git a/src/boost/libs/exception/example/example_io.cpp b/src/boost/libs/exception/example/example_io.cpp
new file mode 100644
index 000000000..f48933282
--- /dev/null
+++ b/src/boost/libs/exception/example/example_io.cpp
@@ -0,0 +1,209 @@
+//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 program simulates errors on copying simple data files. It demonstrates
+//typical Boost Exception usage.
+
+//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 <iostream>
+
+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 virtual std::exception,
+ public virtual boost::exception
+ {
+ public:
+
+ char const *
+ what() const BOOST_NOEXCEPT_OR_NOTHROW
+ {
+ return "example_io error";
+ }
+
+ protected:
+
+ ~error() BOOST_NOEXCEPT_OR_NOTHROW
+ {
+ }
+ };
+
+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 )
+ {
+ if( FILE * f = ::fopen(name,mode) )
+ return boost::shared_ptr<FILE>(f,fclose);
+ else
+ BOOST_THROW_EXCEPTION(fopen_error() <<
+ boost::errinfo_errno (errno) <<
+ boost::errinfo_file_name(name) <<
+ boost::errinfo_file_open_mode(mode) <<
+ boost::errinfo_api_function("fopen"));
+ }
+
+void
+my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
+ {
+ assert(stream);
+ if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) )
+ BOOST_THROW_EXCEPTION(fread_error() <<
+ boost::errinfo_api_function("fread") <<
+ boost::errinfo_errno(errno) <<
+ boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
+ }
+
+void
+my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
+ {
+ assert(stream);
+ if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) )
+ BOOST_THROW_EXCEPTION(fwrite_error() <<
+ boost::errinfo_api_function("fwrite") <<
+ boost::errinfo_errno(errno) <<
+ boost::errinfo_file_handle(boost::weak_ptr<FILE>(stream)));
+ }
+
+void
+reset_file( char const * file_name )
+ {
+ (void) my_fopen(file_name,"wb");
+ }
+
+void
+create_data( char const * file_name )
+ {
+ boost::shared_ptr<FILE> f = my_fopen(file_name,"wb");
+ my_fwrite( data, 1, data_size, f );
+ }
+
+void
+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> dst = my_fopen(dst_file_name,"wb");
+ try
+ {
+ char buffer[data_size];
+ my_fread( buffer, 1, data_size, src );
+ my_fwrite( buffer, 1, data_size, dst );
+ }
+ catch(
+ boost::exception & 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 << boost::errinfo_file_name(src_file_name);
+ else if( fs==dst )
+ x << boost::errinfo_file_name(dst_file_name);
+ }
+ x <<
+ errinfo_src_file_name(src_file_name) <<
+ errinfo_dst_file_name(dst_file_name);
+ throw;
+ }
+ }
+
+void
+dump_copy_info( boost::exception const & 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<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<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<boost::errinfo_errno>(x) )
+ std::cerr << "OS error: " << *err << "\n";
+ if( char const * const * fn=boost::get_error_info<boost::errinfo_api_function>(x) )
+ std::cerr << "Failed function: " << *fn << "\n";
+ }
+
+void
+dump_all_info( boost::exception const & x )
+ {
+ std::cerr << "-------------------------------------------------\n";
+ dump_copy_info(x);
+ dump_file_info(x);
+ dump_clib_info(x);
+ std::cerr << "\nOutput from diagnostic_information():\n";
+ std::cerr << diagnostic_information(x);
+ }
+
+int
+main()
+ {
+ try
+ {
+ create_data( "tmp1.txt" );
+ copy_data( "tmp1.txt", "tmp2.txt" ); //This should succeed.
+
+ reset_file( "tmp1.txt" ); //Creates empty file.
+ try
+ {
+ copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt is empty.
+ }
+ catch(
+ read_error & x )
+ {
+ std::cerr << "\nCaught 'read_error' exception.\n";
+ dump_all_info(x);
+ }
+
+ remove( "tmp1.txt" );
+ remove( "tmp2.txt" );
+ try
+ {
+ copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt does not exist.
+ }
+ catch(
+ open_error & x )
+ {
+ std::cerr << "\nCaught 'open_error' exception.\n";
+ dump_all_info(x);
+ }
+ }
+ catch(
+ ... )
+ {
+ std::cerr << "\nCaught unexpected exception!\n";
+ std::cerr << boost::current_exception_diagnostic_information();
+ }
+ }
diff --git a/src/boost/libs/exception/example/info_tuple.cpp b/src/boost/libs/exception/example/info_tuple.cpp
new file mode 100644
index 000000000..f2e961f6b
--- /dev/null
+++ b/src/boost/libs/exception/example/info_tuple.cpp
@@ -0,0 +1,31 @@
+//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 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::tuple<boost::errinfo_api_function,boost::errinfo_errno> clib_failure;
+
+struct file_open_error: virtual boost::exception { };
+
+boost::shared_ptr<FILE>
+file_open( char const * name, char const * mode )
+ {
+ if( FILE * f=fopen(name,mode) )
+ return boost::shared_ptr<FILE>(f,fclose);
+ else
+ throw file_open_error() <<
+ boost::errinfo_file_name(name) <<
+ clib_failure("fopen",errno);
+ }
diff --git a/src/boost/libs/exception/example/logging.cpp b/src/boost/libs/exception/example/logging.cpp
new file mode 100644
index 000000000..c1b51e7de
--- /dev/null
+++ b/src/boost/libs/exception/example/logging.cpp
@@ -0,0 +1,25 @@
+//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 shows how to print all data contained in a boost::exception.
+
+#include <boost/exception/all.hpp>
+#include <iostream>
+
+void f(); //throws unknown types that derive from boost::exception.
+
+void
+g()
+ {
+ try
+ {
+ f();
+ }
+ catch(
+ boost::exception & e )
+ {
+ std::cerr << diagnostic_information(e);
+ }
+ }