info_tuple.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. //This example shows how boost::tuple can be used to bundle the
  5. //name of the function that fails together with the reported errno.
  6. #include <boost/exception/info_tuple.hpp>
  7. #include <boost/exception/errinfo_file_name.hpp>
  8. #include <boost/exception/errinfo_api_function.hpp>
  9. #include <boost/exception/errinfo_errno.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include <stdio.h>
  12. #include <string>
  13. #include <errno.h>
  14. typedef boost::tuple<boost::errinfo_api_function,boost::errinfo_errno> clib_failure;
  15. struct file_open_error: virtual boost::exception { };
  16. boost::shared_ptr<FILE>
  17. file_open( char const * name, char const * mode )
  18. {
  19. if( FILE * f=fopen(name,mode) )
  20. return boost::shared_ptr<FILE>(f,fclose);
  21. else
  22. throw file_open_error() <<
  23. boost::errinfo_file_name(name) <<
  24. clib_failure("fopen",errno);
  25. }