From 1b74d04b343265357bbbfbdc2582b42b6208d98f Mon Sep 17 00:00:00 2001 From: Emil Dotchevski Date: Thu, 24 Jan 2019 00:13:20 -0800 Subject: [PATCH] optimizations --- example/capture_eh.cpp | 2 +- example/exception_to_result.cpp | 5 ++- example/lua_callback_eh.cpp | 4 +- example/lua_callback_result.cpp | 4 +- example/print_file_eh.cpp | 2 +- example/print_file_result.cpp | 2 +- include/boost/leaf/all.hpp | 2 +- include/boost/leaf/detail/optional.hpp | 4 +- include/boost/leaf/detail/static_store.hpp | 37 ++++++++----------- include/boost/leaf/error.hpp | 22 +++++------ .../boost/leaf/{throw.hpp => exception.hpp} | 0 include/boost/leaf/exception_to_result.hpp | 9 +++-- include/boost/leaf/preload.hpp | 12 ++++-- include/boost/leaf/result.hpp | 2 +- test/_hpp_throw_test.cpp | 4 +- test/capture_exception_async_test.cpp | 2 +- test/capture_exception_state_test.cpp | 2 +- test/capture_exception_unload_test.cpp | 2 +- test/defer_nested_error_exception_test.cpp | 2 +- .../defer_nested_new_error_exception_test.cpp | 2 +- test/diagnostic_info_test.cpp | 2 +- test/exception_to_result_test.cpp | 1 + test/preload_nested_error_exception_test.cpp | 2 +- ...reload_nested_new_error_exception_test.cpp | 2 +- test/try_exception_test.cpp | 2 +- test/try_test.cpp | 2 +- 26 files changed, 68 insertions(+), 64 deletions(-) rename include/boost/leaf/{throw.hpp => exception.hpp} (100%) diff --git a/example/capture_eh.cpp b/example/capture_eh.cpp index d1d9cae..4a25aeb 100644 --- a/example/capture_eh.cpp +++ b/example/capture_eh.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include diff --git a/example/exception_to_result.cpp b/example/exception_to_result.cpp index 1f6666a..f42ec1c 100644 --- a/example/exception_to_result.cpp +++ b/example/exception_to_result.cpp @@ -12,7 +12,10 @@ // using a C++ library that throws exceptions. As demonstrated below, these exception objects are // intercepted and reported by leaf::result<>. -#include +#include +#include +#include +#include #include namespace leaf = boost::leaf; diff --git a/example/lua_callback_eh.cpp b/example/lua_callback_eh.cpp index 95375ca..9aeaa00 100644 --- a/example/lua_callback_eh.cpp +++ b/example/lua_callback_eh.cpp @@ -12,7 +12,9 @@ extern "C" { #include "lua.h" #include "lauxlib.h" } -#include +#include +#include +#include #include #include diff --git a/example/lua_callback_result.cpp b/example/lua_callback_result.cpp index f0d9b81..4e765aa 100644 --- a/example/lua_callback_result.cpp +++ b/example/lua_callback_result.cpp @@ -12,7 +12,9 @@ extern "C" { #include "lua.h" #include "lauxlib.h" } -#include +#include +#include +#include #include #include diff --git a/example/print_file_eh.cpp b/example/print_file_eh.cpp index 83cc071..f86ec92 100644 --- a/example/print_file_eh.cpp +++ b/example/print_file_eh.cpp @@ -7,7 +7,7 @@ // This is a short but complete program that reads a text file in a buffer and prints it to std::cout, // using LEAF to handle errors. It uses exception handling. -#include +#include #include #include #include diff --git a/example/print_file_result.cpp b/example/print_file_result.cpp index acb82ce..0f7ddd0 100644 --- a/example/print_file_result.cpp +++ b/example/print_file_result.cpp @@ -7,8 +7,8 @@ // This is a short but complete program that reads a text file in a buffer and prints it to std::cout, // using LEAF to handle errors. It does not use exception handling. -#include #include +#include #include #include #include diff --git a/include/boost/leaf/all.hpp b/include/boost/leaf/all.hpp index 47d7073..4a983fd 100644 --- a/include/boost/leaf/all.hpp +++ b/include/boost/leaf/all.hpp @@ -12,11 +12,11 @@ #include #include #include +#include #include #include #include #include -#include #include #endif diff --git a/include/boost/leaf/detail/optional.hpp b/include/boost/leaf/detail/optional.hpp index 255e5e5..410708f 100644 --- a/include/boost/leaf/detail/optional.hpp +++ b/include/boost/leaf/detail/optional.hpp @@ -62,8 +62,8 @@ namespace boost { namespace leaf { optional & operator=( optional const & x ) { reset(); - if( x.has_value() ) - put(x.value()); + if( auto pv = x.has_value() ) + put(*pv); return *this; } diff --git a/include/boost/leaf/detail/static_store.hpp b/include/boost/leaf/detail/static_store.hpp index 444bfc0..19d0fda 100644 --- a/include/boost/leaf/detail/static_store.hpp +++ b/include/boost/leaf/detail/static_store.hpp @@ -8,7 +8,6 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include #include #include @@ -195,10 +194,10 @@ namespace boost { namespace leaf { { assert(err_id); slot const & s = *this; - if( s.has_value() && s.value().err_id==err_id ) - return optional(std::move(*this).value().e); - else - return optional(); + if( auto pv = s.has_value() ) + if( pv->err_id==err_id ) + return optional(std::move(*this).value().e); + return optional(); } }; @@ -248,10 +247,9 @@ namespace boost { namespace leaf { template E const * peek( SlotsTuple const & tup, int err_id ) noexcept { - auto & opt = std::get,SlotsTuple>::value>(tup); - if( auto const * v = opt.has_value() ) - if( v->err_id==err_id ) - return &v->e; + if( auto pv = std::get,SlotsTuple>::value>(tup).has_value() ) + if( pv->err_id==err_id ) + return &pv->e; return 0; } } @@ -273,10 +271,7 @@ namespace boost { namespace leaf { template static match_type const * read( SlotsTuple const & tup, error_info const & ei ) noexcept { - if( auto const * v = std::get,SlotsTuple>::value>(tup).has_value() ) - if( v->err_id==ei.err_id() ) - return &v->e; - return 0; + return peek(tup, ei.err_id()); } }; @@ -290,10 +285,10 @@ namespace boost { namespace leaf { template static match_type const * read( SlotsTuple const & tup, error_info const & ei ) noexcept { - if( auto const * v = std::get,SlotsTuple>::value>(tup).has_value() ) - if( v->err_id==ei.err_id() ) - return &v->e.value; - return 0; + if( auto pv = peek(tup, ei.err_id()) ) + return &pv->value; + else + return 0; } }; @@ -324,10 +319,10 @@ namespace boost { namespace leaf { template static match_type const * read( SlotsTuple const & tup, error_info const & ei ) noexcept { - if( auto const * v = std::get,SlotsTuple>::value>(tup).has_value() ) - if( v->err_id==ei.err_id() ) - return &v->e.value; - return 0; + if( auto pv = peek(tup, ei.err_id()) ) + return &pv->value; + else + return 0; } }; diff --git a/include/boost/leaf/error.hpp b/include/boost/leaf/error.hpp index 467efa4..b20ca11 100644 --- a/include/boost/leaf/error.hpp +++ b/include/boost/leaf/error.hpp @@ -362,18 +362,15 @@ namespace boost { namespace leaf { template void put_unexpected_count( id_e_pair const & id_e ) noexcept { - if( slot * p = tl_slot_ptr() ) + if( slot * sl = tl_slot_ptr() ) { - if( p->has_value() ) - { - auto & p_id_e = p->value(); - if( p_id_e.err_id==id_e.err_id ) + if( auto pv = sl->has_value() ) + if( pv->err_id == id_e.err_id ) { - ++p_id_e.e.count; + ++pv->e.count; return; } - } - (void) p->put( id_e_pair(id_e.err_id,e_unexpected_count(&type)) ); + (void) sl->put( id_e_pair(id_e.err_id,e_unexpected_count(&type)) ); } } @@ -381,8 +378,7 @@ namespace boost { namespace leaf { void put_unexpected_info( id_e_pair const & id_e ) noexcept { if( slot * sl = tl_slot_ptr() ) - { - if( auto * pv = sl->has_value() ) + if( auto pv = sl->has_value() ) { if( pv->err_id!=id_e.err_id ) { @@ -393,7 +389,6 @@ namespace boost { namespace leaf { } else sl->emplace(id_e.err_id).e.add(id_e.e); - } } template @@ -440,8 +435,9 @@ namespace boost { namespace leaf { { int c = tl_unexpected_enabled_counter(); assert(c>=0); - if( c && has_value() ) - no_expect_slot(value()); + if( c ) + if( auto v = has_value() ) + no_expect_slot(*v); } tl_slot_ptr() = prev_; } diff --git a/include/boost/leaf/throw.hpp b/include/boost/leaf/exception.hpp similarity index 100% rename from include/boost/leaf/throw.hpp rename to include/boost/leaf/exception.hpp diff --git a/include/boost/leaf/exception_to_result.hpp b/include/boost/leaf/exception_to_result.hpp index c1c8232..d5b8c3e 100644 --- a/include/boost/leaf/exception_to_result.hpp +++ b/include/boost/leaf/exception_to_result.hpp @@ -7,13 +7,14 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -#include -#include -#include -#include +#include +#include namespace boost { namespace leaf { + template + class result; + namespace leaf_detail { inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<> ) diff --git a/include/boost/leaf/preload.hpp b/include/boost/leaf/preload.hpp index 0b85dc9..0549aef 100644 --- a/include/boost/leaf/preload.hpp +++ b/include/boost/leaf/preload.hpp @@ -55,8 +55,10 @@ namespace boost { namespace leaf { assert(err_id); if( s_ ) { - if( !s_->has_value() || s_->value().err_id!=err_id ) - s_->put( leaf_detail::id_e_pair(err_id,std::move(e_)) ); + if( auto pv = s_->has_value() ) + if( pv->err_id==err_id ) + return; + s_->put( leaf_detail::id_e_pair(err_id,std::move(e_)) ); } else { @@ -144,8 +146,10 @@ namespace boost { namespace leaf { assert(err_id); if( s_ ) { - if( !s_->has_value() || s_->value().err_id!=err_id ) - s_->put( leaf_detail::id_e_pair(err_id,f_()) ); + if( auto pv = s_->has_value() ) + if( pv->err_id==err_id ) + return; + s_->put( leaf_detail::id_e_pair(err_id,f_()) ); } else { diff --git a/include/boost/leaf/result.hpp b/include/boost/leaf/result.hpp index 0d36e6a..f0bdb89 100644 --- a/include/boost/leaf/result.hpp +++ b/include/boost/leaf/result.hpp @@ -8,7 +8,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include +#include #include #define LEAF_AUTO(v,r) auto _r_##v = r; if( !_r_##v ) return _r_##v.error(); auto & v = *_r_##v diff --git a/test/_hpp_throw_test.cpp b/test/_hpp_throw_test.cpp index 7fd4789..491caf5 100644 --- a/test/_hpp_throw_test.cpp +++ b/test/_hpp_throw_test.cpp @@ -4,6 +4,6 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -#include -#include +#include +#include int main() { return 0; } diff --git a/test/capture_exception_async_test.cpp b/test/capture_exception_async_test.cpp index d237acc..a2a1972 100644 --- a/test/capture_exception_async_test.cpp +++ b/test/capture_exception_async_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include "boost/core/lightweight_test.hpp" #include #include diff --git a/test/capture_exception_state_test.cpp b/test/capture_exception_state_test.cpp index 465bb54..80f3150 100644 --- a/test/capture_exception_state_test.cpp +++ b/test/capture_exception_state_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf; diff --git a/test/capture_exception_unload_test.cpp b/test/capture_exception_unload_test.cpp index 777e4e3..2d2c692 100644 --- a/test/capture_exception_unload_test.cpp +++ b/test/capture_exception_unload_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include "boost/core/lightweight_test.hpp" diff --git a/test/defer_nested_error_exception_test.cpp b/test/defer_nested_error_exception_test.cpp index 73ca5d8..4e72254 100644 --- a/test/defer_nested_error_exception_test.cpp +++ b/test/defer_nested_error_exception_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf; diff --git a/test/defer_nested_new_error_exception_test.cpp b/test/defer_nested_new_error_exception_test.cpp index c015616..3b05b48 100644 --- a/test/defer_nested_new_error_exception_test.cpp +++ b/test/defer_nested_new_error_exception_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf; diff --git a/test/diagnostic_info_test.cpp b/test/diagnostic_info_test.cpp index feac93b..30f1aef 100644 --- a/test/diagnostic_info_test.cpp +++ b/test/diagnostic_info_test.cpp @@ -5,7 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include +#include #include #include #include "boost/core/lightweight_test.hpp" diff --git a/test/exception_to_result_test.cpp b/test/exception_to_result_test.cpp index 1030ae3..b6040d2 100644 --- a/test/exception_to_result_test.cpp +++ b/test/exception_to_result_test.cpp @@ -5,6 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include +#include #include #include "boost/core/lightweight_test.hpp" diff --git a/test/preload_nested_error_exception_test.cpp b/test/preload_nested_error_exception_test.cpp index ca7c20d..2301549 100644 --- a/test/preload_nested_error_exception_test.cpp +++ b/test/preload_nested_error_exception_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf; diff --git a/test/preload_nested_new_error_exception_test.cpp b/test/preload_nested_new_error_exception_test.cpp index e352d95..248d2b7 100644 --- a/test/preload_nested_new_error_exception_test.cpp +++ b/test/preload_nested_new_error_exception_test.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf; diff --git a/test/try_exception_test.cpp b/test/try_exception_test.cpp index 9cc00b9..cfee9c8 100644 --- a/test/try_exception_test.cpp +++ b/test/try_exception_test.cpp @@ -5,7 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf; diff --git a/test/try_test.cpp b/test/try_test.cpp index 1b7c6ab..4b158e5 100644 --- a/test/try_test.cpp +++ b/test/try_test.cpp @@ -5,7 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include +#include #include "boost/core/lightweight_test.hpp" namespace leaf = boost::leaf;