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 3, 2024
1 parent a4207bb commit 1be31fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
33 changes: 14 additions & 19 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,9 +83,6 @@ namespace leaf_detail
public Ex,
public exception_base,
public error_id
#ifndef BOOST_LEAF_NO_EXCEPTIONS
,exception_id_bump
#endif
{
error_id get_error_id() const noexcept final override
{
Expand All @@ -117,7 +99,14 @@ namespace leaf_detail
public:

exception( exception const & ) = default;
exception( exception && ) = default;

exception( exception && other ) noexcept:
Ex(std::move(other)),
exception_base(std::move(other)),
error_id(std::move(other))
{
static_cast<error_id &>(other) = error_id();
}

exception( error_id id, Ex const & ex ) noexcept:
Ex(ex),
Expand All @@ -138,6 +127,12 @@ namespace leaf_detail
{
enforce_std_exception(*this);
}

~exception() noexcept
{
if( current_error() == *this )
tls::write_uint<leaf_detail::tls_tag_id_factory_current_id>(0);
}
};

template <class... T>
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 1be31fd

Please sign in to comment.