Skip to content

Commit ff4f5da

Browse files
committed
throw leaf::exception(....) becomes leaf::throw_exception(....)
1 parent 7ea5958 commit ff4f5da

29 files changed

+273
-281
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "(lldb) Launch",
99
"type": "cppdbg",
1010
"request": "launch",
11-
"program": "${workspaceFolder}/bld/debug/lua_callback_eh",
11+
"program": "${workspaceFolder}/bld/debug/capture_exception_async_test",
1212
"args": [ ],
1313
"cwd": "${workspaceFolder}",
1414
"stopAtEntry": false,

doc/LEAF-2.png

43.3 KB
Loading

doc/leaf.adoc

Lines changed: 52 additions & 67 deletions
Large diffs are not rendered by default.

example/asio_beast_leaf_rpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ leaf::result<std::string> execute_command(std::string_view line) {
376376
if (i2 == 0) {
377377
// In some cases this command execution function might throw, not
378378
// just return an error.
379-
throw leaf::exception(std::runtime_error{"division by zero"});
379+
leaf::throw_exception(std::runtime_error{"division by zero"});
380380
}
381381
response = std::to_string(i1 % i2);
382382
} else {

example/capture_in_exception.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ task_result task()
3333
if( succeed )
3434
return { };
3535
else
36-
throw leaf::exception(
36+
leaf::throw_exception(
3737
e_thread_id{std::this_thread::get_id()},
3838
e_failure_info1{"info"},
3939
e_failure_info2{42} );

example/lua_callback_eh.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int do_work( lua_State * L )
7575
}
7676
else
7777
{
78-
throw leaf::exception(ec1);
78+
leaf::throw_exception(ec1);
7979
}
8080
}
8181

@@ -127,7 +127,7 @@ int call_lua( lua_State * L )
127127
// cur_err.assigned_error_id() will return a new leaf::error_id,
128128
// otherwise we'll be working with the original error reported
129129
// by a C++ exception out of do_work.
130-
throw leaf::exception( cur_err.assigned_error_id().load( e_lua_pcall_error{err}, e_lua_error_message{std::move(msg)} ) );
130+
leaf::throw_exception( cur_err.assigned_error_id().load( e_lua_pcall_error{err}, e_lua_error_message{std::move(msg)} ) );
131131
}
132132
else
133133
{

example/print_file/print_file_eh.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main( int argc, char const * argv[] )
6767
std::cout << buffer;
6868
std::cout.flush();
6969
if( std::cout.fail() )
70-
throw leaf::exception(output_error, leaf::e_errno{errno});
70+
leaf::throw_exception(output_error, leaf::e_errno{errno});
7171

7272
return 0;
7373
},
@@ -152,7 +152,7 @@ char const * parse_command_line( int argc, char const * argv[] )
152152
if( argc==2 )
153153
return argv[1];
154154
else
155-
throw leaf::exception(bad_command_line);
155+
leaf::throw_exception(bad_command_line);
156156
}
157157

158158

@@ -162,7 +162,7 @@ std::shared_ptr<FILE> file_open( char const * file_name )
162162
if( FILE * f = fopen(file_name, "rb") )
163163
return std::shared_ptr<FILE>(f, &fclose);
164164
else
165-
throw leaf::exception(open_error, leaf::e_errno{errno});
165+
leaf::throw_exception(open_error, leaf::e_errno{errno});
166166
}
167167

168168

@@ -172,14 +172,14 @@ int file_size( FILE & f )
172172
auto load = leaf::on_error([] { return leaf::e_errno{errno}; });
173173

174174
if( fseek(&f, 0, SEEK_END) )
175-
throw leaf::exception(size_error);
175+
leaf::throw_exception(size_error);
176176

177177
int s = ftell(&f);
178178
if( s==-1L )
179-
throw leaf::exception(size_error);
179+
leaf::throw_exception(size_error);
180180

181181
if( fseek(&f,0,SEEK_SET) )
182-
throw leaf::exception(size_error);
182+
leaf::throw_exception(size_error);
183183

184184
return s;
185185
}
@@ -191,8 +191,8 @@ void file_read( FILE & f, void * buf, int size )
191191
int n = fread(buf, 1, size, &f);
192192

193193
if( ferror(&f) )
194-
throw leaf::exception(read_error, leaf::e_errno{errno});
194+
leaf::throw_exception(read_error, leaf::e_errno{errno});
195195

196196
if( n!=size )
197-
throw leaf::exception(eof_error);
197+
leaf::throw_exception(eof_error);
198198
}

include/boost/leaf/capture.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ namespace leaf_detail
132132
catch( exception_base const & e )
133133
{
134134
ctx->captured_id_ = e.get_error_id();
135-
throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
135+
leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
136136
}
137137
catch(...)
138138
{
139139
ctx->captured_id_ = cur_err.assigned_error_id();
140-
throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
140+
leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
141141
}
142142
}
143143

@@ -165,12 +165,12 @@ namespace leaf_detail
165165
catch( exception_base const & e )
166166
{
167167
ctx->captured_id_ = e.get_error_id();
168-
throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
168+
leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
169169
}
170170
catch(...)
171171
{
172172
ctx->captured_id_ = cur_err.assigned_error_id();
173-
throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
173+
leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
174174
}
175175
}
176176

include/boost/leaf/error.hpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -99,39 +99,6 @@ namespace leaf_detail
9999

100100
////////////////////////////////////////
101101

102-
#ifdef BOOST_LEAF_NO_EXCEPTIONS
103-
104-
namespace boost
105-
{
106-
[[noreturn]] void throw_exception( std::exception const & ); // user defined
107-
}
108-
109-
namespace boost { namespace leaf {
110-
111-
template <class T>
112-
[[noreturn]] void throw_exception( T const & e )
113-
{
114-
::boost::throw_exception(e);
115-
}
116-
117-
} }
118-
119-
#else
120-
121-
namespace boost { namespace leaf {
122-
123-
template <class T>
124-
[[noreturn]] void throw_exception( T const & e )
125-
{
126-
throw e;
127-
}
128-
129-
} }
130-
131-
#endif
132-
133-
////////////////////////////////////////
134-
135102
namespace boost { namespace leaf {
136103

137104
#if BOOST_LEAF_CFG_DIAGNOSTICS

include/boost/leaf/exception.hpp

Lines changed: 114 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,58 @@
88

99
#include <boost/leaf/config.hpp>
1010
#include <boost/leaf/error.hpp>
11-
12-
#ifndef BOOST_LEAF_NO_EXCEPTIONS
13-
1411
#include <exception>
1512

16-
#define BOOST_LEAF_EXCEPTION ::boost::leaf::leaf_detail::inject_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::exception
17-
#define BOOST_LEAF_THROW_EXCEPTION ::boost::leaf::leaf_detail::throw_with_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::exception
13+
#ifdef BOOST_LEAF_NO_EXCEPTIONS
1814

19-
////////////////////////////////////////
15+
namespace boost
16+
{
17+
[[noreturn]] void throw_exception( std::exception const & ); // user defined
18+
}
2019

2120
namespace boost { namespace leaf {
2221

2322
namespace leaf_detail
2423
{
25-
struct throw_with_loc
24+
template <class T>
25+
[[noreturn]] void throw_exception_impl( T && e )
2626
{
27-
char const * const file;
28-
int const line;
29-
char const * const fn;
27+
::boost::throw_exception(std::move(e));
28+
}
3029

31-
template <class Ex>
32-
[[noreturn]] friend void operator+( throw_with_loc loc, Ex const & ex )
33-
{
34-
ex.load_source_location_(loc.file, loc.line, loc.fn);
35-
::boost::leaf::throw_exception(ex);
36-
}
30+
class BOOST_LEAF_SYMBOL_VISIBLE exception_base
31+
{
32+
public:
33+
34+
virtual error_id get_error_id() const noexcept = 0;
35+
36+
protected:
37+
38+
exception_base() noexcept { }
39+
~exception_base() noexcept { }
3740
};
3841
}
3942

4043
} }
4144

42-
////////////////////////////////////////
45+
#else
46+
47+
#include <memory>
4348

4449
namespace boost { namespace leaf {
4550

4651
namespace leaf_detail
4752
{
48-
inline void enforce_std_exception( std::exception const & ) noexcept { }
53+
template <class T>
54+
[[noreturn]] void throw_exception_impl( T && e )
55+
{
56+
throw std::move(e);
57+
}
4958

5059
class BOOST_LEAF_SYMBOL_VISIBLE exception_base
5160
{
5261
std::shared_ptr<void const> auto_id_bump_;
62+
5363
public:
5464

5565
virtual error_id get_error_id() const noexcept = 0;
@@ -63,6 +73,44 @@ namespace leaf_detail
6373

6474
~exception_base() noexcept { }
6575
};
76+
}
77+
78+
} }
79+
80+
#endif
81+
82+
////////////////////////////////////////
83+
84+
#define BOOST_LEAF_THROW_EXCEPTION ::boost::leaf::leaf_detail::throw_with_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::leaf_detail::make_exception
85+
86+
namespace boost { namespace leaf {
87+
88+
namespace leaf_detail
89+
{
90+
struct throw_with_loc
91+
{
92+
char const * const file;
93+
int const line;
94+
char const * const fn;
95+
96+
template <class Ex>
97+
[[noreturn]] friend void operator+( throw_with_loc loc, Ex && ex )
98+
{
99+
ex.load_source_location_(loc.file, loc.line, loc.fn);
100+
::boost::leaf::leaf_detail::throw_exception_impl(std::move(ex));
101+
}
102+
};
103+
}
104+
105+
} }
106+
107+
////////////////////////////////////////
108+
109+
namespace boost { namespace leaf {
110+
111+
namespace leaf_detail
112+
{
113+
inline void enforce_std_exception( std::exception const & ) noexcept { }
66114

67115
template <class Ex>
68116
class BOOST_LEAF_SYMBOL_VISIBLE exception:
@@ -112,52 +160,62 @@ namespace leaf_detail
112160
{
113161
constexpr static const bool value = std::is_base_of<std::exception,typename std::remove_reference<T>::type>::value || at_least_one_derives_from_std_exception<Rest...>::value;
114162
};
115-
}
116163

117-
template <class Ex, class... E>
118-
inline
119-
typename std::enable_if<std::is_base_of<std::exception,typename std::remove_reference<Ex>::type>::value, leaf_detail::exception<typename std::remove_reference<Ex>::type>>::type
120-
exception( error_id err, Ex && ex, E && ... e ) noexcept
121-
{
122-
static_assert(!leaf_detail::at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
123-
return leaf_detail::exception<typename std::remove_reference<Ex>::type>( err.load(std::forward<E>(e)...), std::forward<Ex>(ex) );
124-
}
164+
template <class Ex, class... E>
165+
inline
166+
typename std::enable_if<std::is_base_of<std::exception,typename std::remove_reference<Ex>::type>::value, exception<typename std::remove_reference<Ex>::type>>::type
167+
make_exception( error_id err, Ex && ex, E && ... e ) noexcept
168+
{
169+
static_assert(!at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
170+
return exception<typename std::remove_reference<Ex>::type>( err.load(std::forward<E>(e)...), std::forward<Ex>(ex) );
171+
}
125172

126-
template <class E1, class... E>
127-
inline
128-
typename std::enable_if<!std::is_base_of<std::exception,typename std::remove_reference<E1>::type>::value, leaf_detail::exception<std::exception>>::type
129-
exception( error_id err, E1 && car, E && ... cdr ) noexcept
130-
{
131-
static_assert(!leaf_detail::at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
132-
return leaf_detail::exception<std::exception>( err.load(std::forward<E1>(car), std::forward<E>(cdr)...) );
133-
}
173+
template <class E1, class... E>
174+
inline
175+
typename std::enable_if<!std::is_base_of<std::exception,typename std::remove_reference<E1>::type>::value, exception<std::exception>>::type
176+
make_exception( error_id err, E1 && car, E && ... cdr ) noexcept
177+
{
178+
static_assert(!at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
179+
return exception<std::exception>( err.load(std::forward<E1>(car), std::forward<E>(cdr)...) );
180+
}
134181

135-
inline leaf_detail::exception<std::exception> exception( error_id err ) noexcept
136-
{
137-
return leaf_detail::exception<std::exception>(err);
138-
}
182+
inline exception<std::exception> make_exception( error_id err ) noexcept
183+
{
184+
return exception<std::exception>(err);
185+
}
139186

140-
template <class Ex, class... E>
141-
inline
142-
typename std::enable_if<std::is_base_of<std::exception,typename std::remove_reference<Ex>::type>::value, leaf_detail::exception<typename std::remove_reference<Ex>::type>>::type
143-
exception( Ex && ex, E && ... e ) noexcept
144-
{
145-
static_assert(!leaf_detail::at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
146-
return leaf_detail::exception<typename std::remove_reference<Ex>::type>( new_error().load(std::forward<E>(e)...), std::forward<Ex>(ex) );
147-
}
187+
template <class Ex, class... E>
188+
inline
189+
typename std::enable_if<std::is_base_of<std::exception,typename std::remove_reference<Ex>::type>::value, exception<typename std::remove_reference<Ex>::type>>::type
190+
make_exception( Ex && ex, E && ... e ) noexcept
191+
{
192+
static_assert(!at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
193+
return exception<typename std::remove_reference<Ex>::type>( new_error().load(std::forward<E>(e)...), std::forward<Ex>(ex) );
194+
}
148195

149-
template <class E1, class... E>
150-
inline
151-
typename std::enable_if<!std::is_base_of<std::exception,typename std::remove_reference<E1>::type>::value, leaf_detail::exception<std::exception>>::type
152-
exception( E1 && car, E && ... cdr ) noexcept
153-
{
154-
static_assert(!leaf_detail::at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
155-
return leaf_detail::exception<std::exception>( new_error().load(std::forward<E1>(car), std::forward<E>(cdr)...) );
196+
template <class E1, class... E>
197+
inline
198+
typename std::enable_if<!std::is_base_of<std::exception,typename std::remove_reference<E1>::type>::value, exception<std::exception>>::type
199+
make_exception( E1 && car, E && ... cdr ) noexcept
200+
{
201+
static_assert(!at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
202+
return exception<std::exception>( new_error().load(std::forward<E1>(car), std::forward<E>(cdr)...) );
203+
}
204+
205+
inline exception<std::exception> make_exception() noexcept
206+
{
207+
return exception<std::exception>(leaf::new_error());
208+
}
156209
}
157210

158-
inline leaf_detail::exception<std::exception> exception() noexcept
211+
template <class... E>
212+
[[noreturn]] void throw_exception( E && ... e )
159213
{
160-
return leaf_detail::exception<std::exception>(leaf::new_error());
214+
// Warning: setting a breakpoint here will not intercept exceptions thrown
215+
// via BOOST_LEAF_THROW_EXCEPTION or originating in the few other throw
216+
// points elsewhere in LEAF. To intercept all of those exceptions as well,
217+
// set a breakpoint inside boost::leaf::leaf_detail::throw_exception_impl.
218+
leaf_detail::throw_exception_impl(leaf_detail::make_exception(std::forward<E>(e)...));
161219
}
162220

163221
////////////////////////////////////////
@@ -223,5 +281,3 @@ exception_to_result( F && f ) noexcept
223281
} }
224282

225283
#endif
226-
227-
#endif

0 commit comments

Comments
 (0)