From f4dd104c297892b176b85723f0c6b0fd9214f091 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 28 Dec 2016 09:57:15 -0800 Subject: [PATCH] A smaller implementation of try_and_catch Summary: [Folly] A smaller implementation of `try_and_catch`. No longer done as a derived class. This lets us make the protected members of `exception_wrapper` into private members. Reviewed By: ericniebler Differential Revision: D4366065 fbshipit-source-id: ae4763b55e431ac08260f74f6a23a257581247c9 --- folly/ExceptionWrapper.h | 85 +++++++++++++++------------------------- 1 file changed, 31 insertions(+), 54 deletions(-) diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index 1086a8a0..c6003818 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -105,7 +106,7 @@ namespace folly { * */ class exception_wrapper { - protected: + private: template struct optimize; @@ -269,7 +270,7 @@ class exception_wrapper { return std::exception_ptr(); } - protected: + private: template struct optimize { static const bool value = @@ -408,67 +409,43 @@ fbstring exceptionStr(const exception_wrapper& ew); * }); */ -namespace detail { - -template -class try_and_catch; - -template -class try_and_catch : - public try_and_catch { - public: - template - explicit try_and_catch(F&& fn) : Base() { - call_fn(fn); - } +namespace try_and_catch_detail { - protected: - typedef try_and_catch Base; +template +using enable_if_t_ = typename std::enable_if::type; - try_and_catch() : Base() {} +template +using is_wrap_ctor = std::is_constructible; - template - typename std::enable_if::value>::type - assign_exception(Ex& e, std::exception_ptr eptr) { - exception_wrapper::assign_eptr(eptr, e); - } - - template - typename std::enable_if::value>::type - assign_exception(Ex& e, std::exception_ptr /*eptr*/) { - exception_wrapper::assign_sptr(std::make_shared(e)); - } +template +inline enable_if_t_::value, exception_wrapper> make(Ex& ex) { + return exception_wrapper(std::current_exception(), ex); +} - template - void call_fn(F&& fn) { - try { - Base::call_fn(std::move(fn)); - } catch (LastException& e) { - if (typeid(e) == typeid(LastException&)) { - assign_exception(e, std::current_exception()); - } else { - exception_wrapper::assign_eptr(std::current_exception(), e); - } - } - } -}; +template +inline enable_if_t_::value, exception_wrapper> make(Ex& ex) { + return typeid(Ex&) == typeid(ex) + ? exception_wrapper(ex) + : exception_wrapper(std::current_exception(), ex); +} -template<> -class try_and_catch<> : public exception_wrapper { - public: - try_and_catch() = default; +template +inline exception_wrapper impl(F&& f) { + return (f(), exception_wrapper()); +} - protected: - template - void call_fn(F&& fn) { - fn(); +template +inline exception_wrapper impl(F&& f) { + try { + return impl(std::forward(f)); + } catch (Ex& ex) { + return make(ex); } -}; } +} // try_and_catch_detail template exception_wrapper try_and_catch(F&& fn) { - return detail::try_and_catch(std::forward(fn)); -} // detail - + return try_and_catch_detail::impl(std::forward(fn)); +} } // folly -- 2.34.1