diff --git a/.travis.yml b/.travis.yml index 2bf489d..aac60a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -252,6 +252,8 @@ script: echo "using $TOOLSET : : $COMPILER ;" > ~/user-config.jam - ../../b2 variant=debug test toolset=$TOOLSET cxxstd=$CXXSTD - ../../b2 variant=release test toolset=$TOOLSET cxxstd=$CXXSTD + - ../../b2 variant=debug-discard-unexpected test toolset=$TOOLSET cxxstd=$CXXSTD + - ../../b2 variant=release-discard-unexpected test toolset=$TOOLSET cxxstd=$CXXSTD # - cd bld/debug && meson test notifications: diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 0fbc856..38f1fb8 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -23,6 +23,15 @@ "command": "${workspaceRoot}/.vscode/msvc.bat && cd ${workspaceRoot}/bld/debug && meson test" } }, + { + "label": "Run all unit tests (BOOST_LEAF_DISCARD_UNEXPECTED)", + "type": "shell", + "command": "cd ${workspaceRoot}/bld/debug_discard_unexpected && meson test", + "problemMatcher": { "base": "$gcc", "fileLocation": ["relative","${workspaceRoot}/bld/debug_discard_unexpected"] }, + "windows": { + "command": "${workspaceRoot}/.vscode/msvc.bat && cd ${workspaceRoot}/bld/debug_discard_unexpected && meson test" + } + }, { "label": "accumulate_basic_test", "type": "shell", diff --git a/include/boost/leaf/all.hpp b/include/boost/leaf/all.hpp index f23882a..5866c2c 100644 --- a/include/boost/leaf/all.hpp +++ b/include/boost/leaf/all.hpp @@ -877,6 +877,8 @@ namespace boost { namespace leaf { //////////////////////////////////////// +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED + namespace leaf_detail { class e_unexpected_count @@ -983,6 +985,8 @@ namespace boost { namespace leaf { } } +#endif + //////////////////////////////////////// namespace leaf_detail @@ -1113,6 +1117,8 @@ namespace boost { namespace leaf { } }; +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED + template inline void load_unexpected_count( int err_id, E const & e ) noexcept { @@ -1140,6 +1146,8 @@ namespace boost { namespace leaf { load_unexpected_info(err_id, e); } +#endif + template inline void slot::deactivate( bool propagate_errors ) noexcept { @@ -1154,6 +1162,7 @@ namespace boost { namespace leaf { prev_->err_id_ = err_id_; } } +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -1162,6 +1171,7 @@ namespace boost { namespace leaf { if( E const * e = impl::has_value() ) no_expect_slot(err_id_, *e); } +#endif *top_ = prev_; top_ = 0; slot_base::deactivate(); @@ -1174,6 +1184,7 @@ namespace boost { namespace leaf { assert(err_id); if( slot * p = tl_slot_ptr() ) (void) p->load(err_id, std::forward(e)); +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -1181,6 +1192,7 @@ namespace boost { namespace leaf { if( c ) no_expect_slot(err_id, std::forward(e)); } +#endif return 0; } @@ -1970,6 +1982,10 @@ namespace boost { namespace leaf { template <> struct does_not_participate_in_context_deduction: std::true_type { }; template <> struct does_not_participate_in_context_deduction: std::true_type { }; template <> struct does_not_participate_in_context_deduction: std::true_type { }; +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + template <> struct does_not_participate_in_context_deduction: std::true_type { }; + template <> struct does_not_participate_in_context_deduction: std::true_type { }; +#endif template struct transform_e_type_list_impl; @@ -2073,8 +2089,10 @@ namespace boost { namespace leaf { using namespace leaf_detail; assert(!is_active()); tuple_for_each::value,Tup>::activate(tup_); +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED if( unexpected_requested::value ) ++tl_unexpected_enabled_counter(); +#endif thread_id_ = std::this_thread::get_id(); is_active_ = true; } @@ -2085,8 +2103,10 @@ namespace boost { namespace leaf { assert(is_active()); is_active_ = false; thread_id_ = std::thread::id(); +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED if( unexpected_requested::value ) --tl_unexpected_enabled_counter(); +#endif tuple_for_each::value,Tup>::deactivate(tup_, propagate_errors); } @@ -2440,6 +2460,40 @@ namespace boost { namespace leaf { //////////////////////////////////////// +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + + class diagnostic_info: public error_info + { + public: + + diagnostic_info( error_info const & ei ) noexcept: + error_info(ei) + { + } + + friend std::ostream & operator<<( std::ostream & os, diagnostic_info const & x ) + { + return os << "leaf::diagnostic_info not available due to BOOST_LEAF_DISCARD_UNEXPECTED" << std::endl; + } + }; + + class verbose_diagnostic_info: public error_info + { + public: + + verbose_diagnostic_info( error_info const & ei ) noexcept: + error_info(ei) + { + } + + friend std::ostream & operator<<( std::ostream & os, verbose_diagnostic_info const & x ) + { + return os << "leaf::verbose_diagnostic_info not available due to BOOST_LEAF_DISCARD_UNEXPECTED" << std::endl; + } + }; + +#else + class diagnostic_info: public error_info { leaf_detail::e_unexpected_count const * e_uc_; @@ -2496,6 +2550,8 @@ namespace boost { namespace leaf { } }; +#endif + //////////////////////////////////////// namespace leaf_detail @@ -2784,6 +2840,30 @@ namespace boost { namespace leaf { } }; +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + + template <> + struct get_one_argument + { + template + static diagnostic_info get( SlotsTuple const & tup, error_info const & ei ) noexcept + { + return diagnostic_info(ei); + } + }; + + template <> + struct get_one_argument + { + template + static verbose_diagnostic_info get( SlotsTuple const & tup, error_info const & ei ) noexcept + { + return verbose_diagnostic_info(ei); + } + }; + +#else + template <> struct get_one_argument { @@ -2804,6 +2884,8 @@ namespace boost { namespace leaf { } }; +#endif + template <> struct get_one_argument { @@ -3840,6 +3922,7 @@ namespace boost { namespace leaf { if( !s_->has_value(err_id) ) s_->load(err_id, std::move(e_)); } +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -3847,6 +3930,7 @@ namespace boost { namespace leaf { if( c ) no_expect_slot(err_id, std::forward(e_)); } +#endif } }; @@ -3925,6 +4009,7 @@ namespace boost { namespace leaf { if( !s_->has_value(err_id) ) s_->load(err_id, f_()); } +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -3932,6 +4017,7 @@ namespace boost { namespace leaf { if( c ) no_expect_slot(err_id, std::forward(f_())); } +#endif } }; diff --git a/include/boost/leaf/context.hpp b/include/boost/leaf/context.hpp index ded690e..8f64f35 100644 --- a/include/boost/leaf/context.hpp +++ b/include/boost/leaf/context.hpp @@ -129,6 +129,10 @@ namespace boost { namespace leaf { template <> struct does_not_participate_in_context_deduction: std::true_type { }; template <> struct does_not_participate_in_context_deduction: std::true_type { }; template <> struct does_not_participate_in_context_deduction: std::true_type { }; +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + template <> struct does_not_participate_in_context_deduction: std::true_type { }; + template <> struct does_not_participate_in_context_deduction: std::true_type { }; +#endif template struct transform_e_type_list_impl; @@ -232,8 +236,10 @@ namespace boost { namespace leaf { using namespace leaf_detail; assert(!is_active()); tuple_for_each::value,Tup>::activate(tup_); +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED if( unexpected_requested::value ) ++tl_unexpected_enabled_counter(); +#endif thread_id_ = std::this_thread::get_id(); is_active_ = true; } @@ -244,8 +250,10 @@ namespace boost { namespace leaf { assert(is_active()); is_active_ = false; thread_id_ = std::thread::id(); +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED if( unexpected_requested::value ) --tl_unexpected_enabled_counter(); +#endif tuple_for_each::value,Tup>::deactivate(tup_, propagate_errors); } diff --git a/include/boost/leaf/detail/handle.hpp b/include/boost/leaf/detail/handle.hpp index 71ffd78..45b99d6 100644 --- a/include/boost/leaf/detail/handle.hpp +++ b/include/boost/leaf/detail/handle.hpp @@ -112,6 +112,40 @@ namespace boost { namespace leaf { //////////////////////////////////////// +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + + class diagnostic_info: public error_info + { + public: + + diagnostic_info( error_info const & ei ) noexcept: + error_info(ei) + { + } + + friend std::ostream & operator<<( std::ostream & os, diagnostic_info const & x ) + { + return os << "leaf::diagnostic_info not available due to BOOST_LEAF_DISCARD_UNEXPECTED" << std::endl; + } + }; + + class verbose_diagnostic_info: public error_info + { + public: + + verbose_diagnostic_info( error_info const & ei ) noexcept: + error_info(ei) + { + } + + friend std::ostream & operator<<( std::ostream & os, verbose_diagnostic_info const & x ) + { + return os << "leaf::verbose_diagnostic_info not available due to BOOST_LEAF_DISCARD_UNEXPECTED" << std::endl; + } + }; + +#else + class diagnostic_info: public error_info { leaf_detail::e_unexpected_count const * e_uc_; @@ -168,6 +202,8 @@ namespace boost { namespace leaf { } }; +#endif + //////////////////////////////////////// namespace leaf_detail @@ -456,6 +492,30 @@ namespace boost { namespace leaf { } }; +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + + template <> + struct get_one_argument + { + template + static diagnostic_info get( SlotsTuple const & tup, error_info const & ei ) noexcept + { + return diagnostic_info(ei); + } + }; + + template <> + struct get_one_argument + { + template + static verbose_diagnostic_info get( SlotsTuple const & tup, error_info const & ei ) noexcept + { + return verbose_diagnostic_info(ei); + } + }; + +#else + template <> struct get_one_argument { @@ -476,6 +536,8 @@ namespace boost { namespace leaf { } }; +#endif + template <> struct get_one_argument { diff --git a/include/boost/leaf/error.hpp b/include/boost/leaf/error.hpp index 37dfecd..a1fb251 100644 --- a/include/boost/leaf/error.hpp +++ b/include/boost/leaf/error.hpp @@ -157,6 +157,8 @@ namespace boost { namespace leaf { //////////////////////////////////////// +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED + namespace leaf_detail { class e_unexpected_count @@ -263,6 +265,8 @@ namespace boost { namespace leaf { } } +#endif + //////////////////////////////////////// namespace leaf_detail @@ -393,6 +397,8 @@ namespace boost { namespace leaf { } }; +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED + template inline void load_unexpected_count( int err_id, E const & e ) noexcept { @@ -420,6 +426,8 @@ namespace boost { namespace leaf { load_unexpected_info(err_id, e); } +#endif + template inline void slot::deactivate( bool propagate_errors ) noexcept { @@ -434,6 +442,7 @@ namespace boost { namespace leaf { prev_->err_id_ = err_id_; } } +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -442,6 +451,7 @@ namespace boost { namespace leaf { if( E const * e = impl::has_value() ) no_expect_slot(err_id_, *e); } +#endif *top_ = prev_; top_ = 0; slot_base::deactivate(); @@ -454,6 +464,7 @@ namespace boost { namespace leaf { assert(err_id); if( slot * p = tl_slot_ptr() ) (void) p->load(err_id, std::forward(e)); +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -461,6 +472,7 @@ namespace boost { namespace leaf { if( c ) no_expect_slot(err_id, std::forward(e)); } +#endif return 0; } diff --git a/include/boost/leaf/preload.hpp b/include/boost/leaf/preload.hpp index 0a15a8c..d7ece75 100644 --- a/include/boost/leaf/preload.hpp +++ b/include/boost/leaf/preload.hpp @@ -56,6 +56,7 @@ namespace boost { namespace leaf { if( !s_->has_value(err_id) ) s_->load(err_id, std::move(e_)); } +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -63,6 +64,7 @@ namespace boost { namespace leaf { if( c ) no_expect_slot(err_id, std::forward(e_)); } +#endif } }; @@ -141,6 +143,7 @@ namespace boost { namespace leaf { if( !s_->has_value(err_id) ) s_->load(err_id, f_()); } +#ifndef BOOST_LEAF_DISCARD_UNEXPECTED else { int c = tl_unexpected_enabled_counter(); @@ -148,6 +151,7 @@ namespace boost { namespace leaf { if( c ) no_expect_slot(err_id, std::forward(f_())); } +#endif } }; diff --git a/meson.build b/meson.build index aac5c6f..a73a8f5 100644 --- a/meson.build +++ b/meson.build @@ -44,11 +44,16 @@ endif boost_headers = declare_dependency(include_directories: '../..') -mp11 = [ ] defines = [ ] + +if get_option('discard_unexpected') + defines += [ '-DBOOST_LEAF_DISCARD_UNEXPECTED' ] +endif + +mp11 = [ ] if get_option('use_boost_mp11') mp11 = [ boost_headers ] - defines = [ '-DBOOST_LEAF_USE_BOOST_MP11' ] + defines += [ '-DBOOST_LEAF_USE_BOOST_MP11' ] endif thread_dep = dependency('threads') diff --git a/meson_options.txt b/meson_options.txt index 5dc3e6f..dd41272 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,3 +2,4 @@ option('lua_examples',type:'boolean',value:false,description:'Enable or disable option('boost_examples',type:'boolean',value:false,description:'Enable or disable building of examples that need Boost') option('all_hpp',type:'boolean',value:false,description:'Enable or disable the automatic generation of include/boost/leaf/all.hpp') option('use_boost_mp11',type:'boolean',value:false,description:'Use instead of internal copied&pasted mp11 code') +option('discard_unexpected',type:'boolean',value:false,description:'#define BOOST_LEAF_DISCARD_UNEXPECTED') diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 2d5038f..48624cb 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -5,6 +5,9 @@ import testing ; +variant debug-discard-unexpected : debug : BOOST_LEAF_DISCARD_UNEXPECTED ; +variant release-discard-unexpected : release : BOOST_LEAF_DISCARD_UNEXPECTED ; + project : requirements gcc:"-std=c++11 -Wno-delete-non-virtual-dtor -Wno-parentheses" diff --git a/test/context_deduction_test.cpp b/test/context_deduction_test.cpp index 0d2a1f0..3c0a599 100644 --- a/test/context_deduction_test.cpp +++ b/test/context_deduction_test.cpp @@ -92,9 +92,15 @@ void not_called_on_purpose() test< std::tuple,info<2>,info<3>> >( expd([]( info<1> const *, info<2> ){ }, []( info<1>, info<3> const * ){ }) ); test< std::tuple,info<2>,info<3>> >( expd([]( info<1> const, info<2> ){ }, []( info<1> const *, info<3> ){ }) ); +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + test< std::tuple,info<2>> >( expd([]( info<1>, info<2>, leaf::diagnostic_info const & ){ }, []( info<1>, info<2> ){ }) ); + test< std::tuple,info<2>> >( expd([]( info<1>, info<2> ){ }, []( info<1>, leaf::verbose_diagnostic_info const &, info<2> ){ }) ); + test< std::tuple,info<2>> >( expd([]( info<1>, info<2>, leaf::diagnostic_info const & ){ }, []( info<1>, leaf::verbose_diagnostic_info const &, info<2> ){ }) ); +#else test< std::tuple,info<2>,leaf::leaf_detail::e_unexpected_count> >( expd([]( info<1>, info<2>, leaf::diagnostic_info const & ){ }, []( info<1>, info<2> ){ }) ); test< std::tuple,info<2>,leaf::leaf_detail::e_unexpected_info> >( expd([]( info<1>, info<2> ){ }, []( info<1>, leaf::verbose_diagnostic_info const &, info<2> ){ }) ); test< std::tuple,info<2>,leaf::leaf_detail::e_unexpected_count,leaf::leaf_detail::e_unexpected_info> >( expd([]( info<1>, info<2>, leaf::diagnostic_info const & ){ }, []( info<1>, leaf::verbose_diagnostic_info const &, info<2> ){ }) ); +#endif } int main() diff --git a/test/defer_basic_test.cpp b/test/defer_basic_test.cpp index 958ff51..302ab05 100644 --- a/test/defer_basic_test.cpp +++ b/test/defer_basic_test.cpp @@ -49,7 +49,11 @@ int main() std::stringstream ss; ss << di; std::string s = ss.str(); std::cout << s; +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + BOOST_TEST(s.find("BOOST_LEAF_DISCARD_UNEXPECTED")!=s.npos); +#else BOOST_TEST(s.find("info<-42>")!=s.npos); +#endif return 1; }, [] diff --git a/test/diagnostic_info_test.cpp b/test/diagnostic_info_test.cpp index 89a2e54..c243c1e 100644 --- a/test/diagnostic_info_test.cpp +++ b/test/diagnostic_info_test.cpp @@ -141,6 +141,9 @@ int main() std::ostringstream st; st << unmatched; std::string s = st.str(); +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + BOOST_TEST(s.find("BOOST_LEAF_DISCARD_UNEXPECTED")!=s.npos); +#else BOOST_TEST(s.find("leaf::diagnostic_info:")!=s.npos); BOOST_TEST(s.find("std::exception::what(): my_error")!=s.npos); BOOST_TEST(s.find(": {Non-Printable}")!=s.npos); @@ -150,6 +153,7 @@ int main() BOOST_TEST(s.find(") in function")!=s.npos); BOOST_TEST(s.find("Detected 2 attempts")!=s.npos); BOOST_TEST(s.find("unexpected_test<1>")!=s.npos); +#endif BOOST_TEST_EQ(s.find("unexpected_test<2>"), s.npos); std::cout << s; } @@ -189,6 +193,9 @@ int main() std::ostringstream st; st << di; std::string s = st.str(); +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + BOOST_TEST(s.find("BOOST_LEAF_DISCARD_UNEXPECTED")!=s.npos); +#else BOOST_TEST(s.find("leaf::verbose_diagnostic_info:")!=s.npos); BOOST_TEST(s.find("std::exception::what(): my_error")!=s.npos); BOOST_TEST(s.find(": {Non-Printable}")!=s.npos); @@ -198,6 +205,7 @@ int main() BOOST_TEST(s.find(") in function")!=s.npos); BOOST_TEST(s.find("unexpected_test<1>")!=s.npos); BOOST_TEST(s.find("unexpected_test<2>")!=s.npos); +#endif std::cout << s; } std::cout << "polymorphic_context::print():" << std::endl; @@ -222,7 +230,7 @@ int main() std::ostringstream st; st << di; std::string s = st.str(); - BOOST_TEST(s.find("leaf::diagnostic_info:")!=s.npos); + BOOST_TEST(s.find("leaf::diagnostic_info")!=s.npos); std::cout << s; } std::cout << "polymorphic_context::print():" << std::endl; @@ -247,7 +255,7 @@ int main() std::ostringstream st; st << vdi; std::string s = st.str(); - BOOST_TEST(s.find("leaf::verbose_diagnostic_info:")!=s.npos); + BOOST_TEST(s.find("leaf::verbose_diagnostic_info")!=s.npos); std::cout << s; } std::cout << "polymorphic_context::print():" << std::endl; diff --git a/test/preload_basic_test.cpp b/test/preload_basic_test.cpp index 236f780..4406c86 100644 --- a/test/preload_basic_test.cpp +++ b/test/preload_basic_test.cpp @@ -40,7 +40,11 @@ int main() std::stringstream ss; ss << di; std::string s = ss.str(); std::cout << s; +#ifdef BOOST_LEAF_DISCARD_UNEXPECTED + BOOST_TEST(s.find("BOOST_LEAF_DISCARD_UNEXPECTED")!=s.npos); +#else BOOST_TEST(s.find("info<-42>")!=s.npos); +#endif return 1; }, []