Skip to content

Commit

Permalink
Exception wrapper optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
zajo committed Sep 4, 2024
1 parent a4207bb commit c9d494a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
55 changes: 32 additions & 23 deletions include/boost/leaf/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ namespace leaf_detail

#else

#include <memory>

namespace boost { namespace leaf {

namespace leaf_detail
Expand All @@ -45,19 +43,6 @@ namespace leaf_detail
{
throw std::move(e);
}

class exception_id_bump
{
std::shared_ptr<void const> auto_id_bump_;
protected:

exception_id_bump():
auto_id_bump_(nullptr, [](void const *) { (void) new_id(); })
{
}

~exception_id_bump() noexcept { }
};
}

} }
Expand Down Expand Up @@ -98,12 +83,12 @@ namespace leaf_detail
public Ex,
public exception_base,
public error_id
#ifndef BOOST_LEAF_NO_EXCEPTIONS
,exception_id_bump
#endif
{
mutable bool clear_current_error_;

error_id get_error_id() const noexcept final override
{
clear_current_error_ = false;
return *this;
}

Expand All @@ -116,28 +101,52 @@ namespace leaf_detail

public:

exception( exception const & ) = default;
exception( exception && ) = default;
exception( exception const & other ):
Ex(other),
exception_base(other),
error_id(other),
clear_current_error_(other.clear_current_error_)
{
other.clear_current_error_ = false;
}

exception( exception && other ) noexcept:
Ex(std::move(other)),
exception_base(std::move(other)),
error_id(std::move(other)),
clear_current_error_(std::move(other.clear_current_error_))
{
other.clear_current_error_ = false;
}

exception( error_id id, Ex const & ex ) noexcept:
Ex(ex),
error_id(id)
error_id(id),
clear_current_error_(true)
{
enforce_std_exception(*this);
}

exception( error_id id, Ex && ex ) noexcept:
Ex(std::move(ex)),
error_id(id)
error_id(id),
clear_current_error_(true)
{
enforce_std_exception(*this);
}

explicit exception( error_id id ) noexcept:
error_id(id)
error_id(id),
clear_current_error_(true)
{
enforce_std_exception(*this);
}

~exception() noexcept
{
if( clear_current_error_ )
tls::write_uint<leaf_detail::tls_tag_id_factory_current_id>(0);
}
};

template <class... T>
Expand Down
2 changes: 2 additions & 0 deletions include/boost/leaf/handle_errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class BOOST_LEAF_SYMBOL_VISIBLE error_info
#ifndef BOOST_LEAF_NO_EXCEPTIONS
static error_id unpack_error_id( std::exception const * ex ) noexcept
{
if( leaf_detail::exception_base const * eb = dynamic_cast<leaf_detail::exception_base const *>(ex) )
return eb->get_error_id();
if( error_id const * err_id = dynamic_cast<error_id const *>(ex) )
return *err_id;
return current_error();
Expand Down
13 changes: 2 additions & 11 deletions include/boost/leaf/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,12 @@ namespace leaf_detail { class dynamic_allocator; }
////////////////////////////////////////

class bad_result:
public std::exception,
public error_id
public std::exception
{
char const * what() const noexcept final override
{
return "boost::leaf::bad_result";
}

public:

explicit bad_result( error_id id ) noexcept:
error_id(id)
{
BOOST_LEAF_ASSERT(value());
}
};

////////////////////////////////////////
Expand Down Expand Up @@ -326,7 +317,7 @@ class BOOST_LEAF_SYMBOL_VISIBLE BOOST_LEAF_ATTRIBUTE_NODISCARD result
#endif
case result_discriminant::err_id_zero:
case result_discriminant::err_id:
::boost::leaf::leaf_detail::throw_exception_impl(bad_result(get_error_id()));
throw_exception(get_error_id(), bad_result{});
case result_discriminant::val:
break;
}
Expand Down
16 changes: 8 additions & 8 deletions test/diagnostics_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ int main()
std::cout << s << std::endl;
if( BOOST_LEAF_CFG_DIAGNOSTICS )
BOOST_TEST(cmp(s,
"Error serial #7\n"
"Error serial #6\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
Expand All @@ -472,7 +472,7 @@ int main()
));
else
BOOST_TEST(cmp(s,
"Error serial #7\n"
"Error serial #6\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
Expand Down Expand Up @@ -517,7 +517,7 @@ int main()
if( BOOST_LEAF_CFG_DIAGNOSTICS )
if( BOOST_LEAF_CFG_CAPTURE )
BOOST_TEST(cmp(s,
"Error serial #9\n"
"Error serial #7\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
Expand All @@ -534,7 +534,7 @@ int main()
));
else
BOOST_TEST(cmp(s,
"Error serial #9\n"
"Error serial #7\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
Expand All @@ -549,7 +549,7 @@ int main()
));
else
BOOST_TEST(cmp(s,
"Error serial #9\n"
"Error serial #7\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
Expand Down Expand Up @@ -584,7 +584,7 @@ int main()
if( BOOST_LEAF_CFG_DIAGNOSTICS )
if( BOOST_LEAF_CFG_CAPTURE )
BOOST_TEST(cmp(s,
"Error serial #11\n"
"Error serial #8\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what\n"
Expand All @@ -601,15 +601,15 @@ int main()
));
else
BOOST_TEST(cmp(s,
"Error serial #11\n"
"Error serial #8\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
BOOST_LEAF_DIAGNOSTIC_DETAILS_NO_BOOST_LEAF_CFG_CAPTURE
));
else
BOOST_TEST(cmp(s,
"Error serial #11\n"
"Error serial #8\n"
"Caught C++ exception:"
"\n\tType: <removed variance>"
"\n\tstd::exception::what(): my_exception what"
Expand Down
6 changes: 3 additions & 3 deletions test/on_error_preload_exception_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int main()
},
[]( leaf::error_info const & err, info<2>, info<3> )
{
BOOST_TEST_EQ(err.error().value(), 13);
BOOST_TEST_EQ(err.error().value(), 9);
return 2;
},
[]( info<1>, info<2> )
Expand All @@ -114,7 +114,7 @@ int main()
},
[]( leaf::error_info const & err, info<1> )
{
BOOST_TEST_EQ(err.error().value(), 21);
BOOST_TEST_EQ(err.error().value(), 13);
return 1;
},
[]( info<2> )
Expand All @@ -139,7 +139,7 @@ int main()
},
[]( leaf::error_info const & err, info<2>, info<3> )
{
BOOST_TEST_EQ(err.error().value(), 25);
BOOST_TEST_EQ(err.error().value(), 17);
return 2;
},
[]( info<1>, info<2> )
Expand Down

0 comments on commit c9d494a

Please sign in to comment.