Skip to content

Commit

Permalink
Interoperability with boost::system::result
Browse files Browse the repository at this point in the history
  • Loading branch information
zajo committed Sep 18, 2024
1 parent 10dfe3c commit a242ee1
Show file tree
Hide file tree
Showing 24 changed files with 478 additions and 251 deletions.
4 changes: 2 additions & 2 deletions doc/leaf.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ return leaf::try_catch(
[.text-right]
<<try_catch>> | <<result>>

NOTE: Follow this link to see a complete example program: https://github.com/boostorg/leaf/blob/master/example/try_capture_all_eh.cpp?ts=4[try_capture_all_eh.cpp].
NOTE: Follow this link to see a complete example program: https://github.com/boostorg/leaf/blob/master/example/try_capture_all_exceptions.cpp?ts=4[try_capture_all_exceptions.cpp].

'''

Expand Down Expand Up @@ -1776,7 +1776,7 @@ int main() noexcept

NOTE: Follow this link to see the complete program: https://github.com/boostorg/leaf/blob/master/example/lua_callback_result.cpp?ts=4[lua_callback_result.cpp].

TIP: When using Lua with {CPP}, we need to protect the Lua interpreter from exceptions that may be thrown from {CPP} functions installed as `lua_CFunction` callbacks. Here is the program from this section rewritten to use a {CPP} exception (instead of `leaf::result`) to safely communicate errors out of the `do_work` function: https://github.com/boostorg/leaf/blob/master/example/lua_callback_eh.cpp?ts=4[lua_callback_eh.cpp].
TIP: When using Lua with {CPP}, we need to protect the Lua interpreter from exceptions that may be thrown from {CPP} functions installed as `lua_CFunction` callbacks. Here is the program from this section rewritten to use a {CPP} exception (instead of `leaf::result`) to safely communicate errors out of the `do_work` function: https://github.com/boostorg/leaf/blob/master/example/lua_callback_exceptions.cpp?ts=4[lua_callback_exceptions.cpp].

''''

Expand Down
42 changes: 21 additions & 21 deletions example/error_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
// not captured, only printed.

#include <boost/leaf.hpp>

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif

////////////////////////////////////////

#include <iostream>
#include <cstdlib>

Expand Down Expand Up @@ -125,24 +146,3 @@ int main()
} );
return 0;
}

////////////////////////////////////////

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::cerr << "Terminating due to a C++ exception under BOOST_LEAF_NO_EXCEPTIONS: " << e.what();
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif
42 changes: 21 additions & 21 deletions example/error_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@
// recorded in a std::deque, rather than just printed in-place.

#include <boost/leaf.hpp>

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif

////////////////////////////////////////

#include <iostream>
#include <deque>
#include <cstdlib>
Expand Down Expand Up @@ -125,24 +146,3 @@ int main()
} );
return 0;
}

////////////////////////////////////////

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::cerr << "Terminating due to a C++ exception under BOOST_LEAF_NO_EXCEPTIONS: " << e.what();
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif
File renamed without changes.
42 changes: 22 additions & 20 deletions example/lua_callback_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@
// This is a simple program that shows how to report error objects out of a
// C-callback, converting them to leaf::result<T> as soon as controlreaches C++.

#include <boost/leaf.hpp>

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif

////////////////////////////////////////

extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
#include <boost/leaf.hpp>
#include <iostream>
#include <memory>
#include <stdlib.h>
Expand Down Expand Up @@ -153,22 +174,3 @@ int main()

return 0;
}

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::cerr << "Terminating due to a C++ exception under BOOST_LEAF_NO_EXCEPTIONS: " << e.what();
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// It reads a text file in a buffer and prints it to std::cout, using LEAF to
// handle errors. This version uses exception handling. The version that does
// not use exception handling is in print_file_result.cpp.
// not use exception handling is in print_file_leaf_result.cpp.

#include <boost/leaf.hpp>
#include <iostream>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,30 @@

// It reads a text file in a buffer and prints it to std::cout, using LEAF to
// handle errors. This version does not use exception handling. The version that
// does use exception handling is in print_file_eh.cpp.
// does use exception handling is in print_file_exceptions.cpp.

#include <boost/leaf.hpp>

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif

////////////////////////////////////////

#include <iostream>
#include <memory>
#include <stdio.h>
Expand Down Expand Up @@ -202,24 +223,3 @@ result<void> file_read( FILE & f, void * buf, std::size_t size )

return { };
}

////////////////////////////////////////

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::cerr << "Terminating due to a C++ exception under BOOST_LEAF_NO_EXCEPTIONS: " << e.what();
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif
44 changes: 22 additions & 22 deletions example/print_file/print_file_outcome_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
// It reads a text file in a buffer and prints it to std::cout, using LEAF to
// handle errors. This version does not use exception handling.

#include <boost/outcome/std_result.hpp>
#include <boost/leaf.hpp>

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif

////////////////////////////////////////

#include <boost/outcome/std_result.hpp>
#include <iostream>
#include <memory>
#include <stdio.h>
Expand Down Expand Up @@ -210,24 +231,3 @@ result<void> file_read( FILE & f, void * buf, std::size_t size )

return outcome::success();
}

////////////////////////////////////////

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace boost
{
[[noreturn]] void throw_exception( std::exception const & e )
{
std::cerr << "Terminating due to a C++ exception under BOOST_LEAF_NO_EXCEPTIONS: " << e.what();
std::terminate();
}

struct source_location;
[[noreturn]] void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}

#endif
Loading

0 comments on commit a242ee1

Please sign in to comment.