From c95f803a5bb2d8447c8021b3157bc2706bc3ecdf Mon Sep 17 00:00:00 2001 From: Emil Dotchevski Date: Tue, 12 Dec 2023 10:33:37 -0800 Subject: [PATCH] Minor tweaks to leaf_detail::capture_list --- include/boost/leaf/detail/capture_list.hpp | 28 +++++++++------------- include/boost/leaf/error.hpp | 11 +++++---- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/include/boost/leaf/detail/capture_list.hpp b/include/boost/leaf/detail/capture_list.hpp index 4d0e2992..491415a8 100644 --- a/include/boost/leaf/detail/capture_list.hpp +++ b/include/boost/leaf/detail/capture_list.hpp @@ -49,8 +49,7 @@ namespace leaf_detail *last = this; last = &next_; } - }; - node * first_; + } * first_; template BOOST_LEAF_CONSTEXPR void for_each( F f ) const @@ -59,17 +58,6 @@ namespace leaf_detail f(*p); } - void clear() noexcept - { - for( node const * p = first_; p; ) - { - node const * n = p -> next_; - delete p; - p = n; - } - first_ = nullptr; - } - public: BOOST_LEAF_CONSTEXPR explicit capture_list( node * first ) noexcept: @@ -85,18 +73,24 @@ namespace leaf_detail ~capture_list() noexcept { - clear(); + for( node const * p = first_; p; ) + { + node const * n = p -> next_; + delete p; + p = n; + } } void unload( int const err_id ) { + capture_list moved(first_); + first_ = nullptr; tls::write_uint(unsigned(err_id)); - for_each( + moved.for_each( [err_id]( node & n ) { - n.unload(err_id); + n.unload(err_id); // last node may throw } ); - clear(); } template diff --git a/include/boost/leaf/error.hpp b/include/boost/leaf/error.hpp index dd890e82..b77b68c1 100644 --- a/include/boost/leaf/error.hpp +++ b/include/boost/leaf/error.hpp @@ -337,7 +337,7 @@ namespace leaf_detail BOOST_LEAF_ASSERT(last_ != nullptr); BOOST_LEAF_ASSERT(*last_ == nullptr); BOOST_LEAF_ASSERT(other.first_ == nullptr); - other.last_ = nullptr; + other.last_ = &other.first_; } template @@ -364,18 +364,19 @@ namespace leaf_detail bool empty() const noexcept { - return last_ == &first_; + BOOST_LEAF_ASSERT(first_ != nullptr || last_ == &first_); + return first_ != nullptr; } int size() const noexcept { - int n = 0; + int c = 0; for_each( [&]( capture_list::node const & ) { - ++n; + ++c; } ); - return n; + return c; } template