X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FExceptionWrapper.h;h=16f1b03639f4b42fc31a9cea988a68f3d02c0723;hp=c444d1fe3015a3290f693936bc6b948bd73fd3e3;hb=6e0964d9e0d6fda080e255720ed3e6829ecc0ace;hpb=c60d131b3e5942b31caa8b4b13c7f9bfa61b986a diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index c444d1fe..16f1b036 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -24,7 +24,6 @@ #include #include -#include namespace folly { @@ -118,8 +117,7 @@ class exception_wrapper { ::type> /* implicit */ exception_wrapper(Ex&& exn) { typedef typename std::decay::type DEx; - item_ = std::make_shared(std::forward(exn)); - throwfn_ = folly::detail::Thrower::doThrow; + assign_sptr(std::make_shared(std::forward(exn))); } // The following two constructors are meant to emulate the behavior of @@ -269,7 +267,7 @@ class exception_wrapper { return std::exception_ptr(); } -protected: + protected: template struct optimize { static const bool value = @@ -278,6 +276,12 @@ protected: !std::is_abstract::value; }; + template + void assign_sptr(std::shared_ptr sptr) { + this->item_ = std::move(sptr); + this->throwfn_ = Thrower::doThrow; + } + template void assign_eptr(std::exception_ptr eptr, Ex& e) { this->eptr_ = eptr; @@ -293,7 +297,7 @@ protected: // store a copy of the concrete type, and a helper function so we // can rethrow it. std::shared_ptr item_; - void (*throwfn_)(std::exception*){nullptr}; + void (*throwfn_)(std::exception&){nullptr}; // Fallback case: store the library wrapper, which is less efficient // but gets the job done. Also store exceptionPtr() the name of the // exception type, so we can at least get those back out without @@ -305,7 +309,7 @@ protected: template friend exception_wrapper make_exception_wrapper(Args&&... args); -private: + private: template struct functor_traits { template @@ -320,6 +324,14 @@ private: using arg_type_decayed = typename std::decay::type; }; + template + class Thrower { + public: + static void doThrow(std::exception& obj) { + throw static_cast(obj); + } + }; + // What makes this useful is that T can be exception_wrapper* or // const exception_wrapper*, and the compiler will use the // instantiation which works with F. @@ -347,8 +359,7 @@ private: template exception_wrapper make_exception_wrapper(Args&&... args) { exception_wrapper ew; - ew.item_ = std::make_shared(std::forward(args)...); - ew.throwfn_ = folly::detail::Thrower::doThrow; + ew.assign_sptr(std::make_shared(std::forward(args)...)); return ew; } @@ -423,8 +434,7 @@ class try_and_catch : template typename std::enable_if::value>::type assign_exception(Ex& e, std::exception_ptr /*eptr*/) { - this->item_ = std::make_shared(e); - this->throwfn_ = folly::detail::Thrower::doThrow; + exception_wrapper::assign_sptr(std::make_shared(e)); } template