From 6e0964d9e0d6fda080e255720ed3e6829ecc0ace Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Thu, 22 Dec 2016 12:13:29 -0800 Subject: [PATCH] Move Thrower into exception_wrapper Summary: [Folly] Move `Thrower` into `exception_wrapper`. It is only used by `exception_wrapper`, so it might as well be a member type. Also, make it private. That means we need to tweak `try_and_catch`. Since we are tweaking `try_and_catch`, tweak all similar cases. Reviewed By: luciang Differential Revision: D4361815 fbshipit-source-id: c5025894465a2c7760bd79dbbd272079fd34dd79 --- folly/ExceptionWrapper.cpp | 2 +- folly/ExceptionWrapper.h | 30 ++++++++++++++++++++---------- folly/Makefile.am | 1 - folly/detail/ExceptionWrapper.h | 29 ----------------------------- 4 files changed, 21 insertions(+), 41 deletions(-) delete mode 100644 folly/detail/ExceptionWrapper.h diff --git a/folly/ExceptionWrapper.cpp b/folly/ExceptionWrapper.cpp index 3beb92e8..8fce7574 100644 --- a/folly/ExceptionWrapper.cpp +++ b/folly/ExceptionWrapper.cpp @@ -22,7 +22,7 @@ namespace folly { [[noreturn]] void exception_wrapper::throwException() const { if (throwfn_) { - throwfn_(item_.get()); + throwfn_(*item_); } else if (eptr_) { std::rethrow_exception(eptr_); } 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 diff --git a/folly/Makefile.am b/folly/Makefile.am index c8518240..0277a341 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -60,7 +60,6 @@ nobase_follyinclude_HEADERS = \ detail/CachelinePaddedImpl.h \ detail/ChecksumDetail.h \ detail/DiscriminatedPtrDetail.h \ - detail/ExceptionWrapper.h \ detail/FileUtilDetail.h \ detail/FingerprintPolynomial.h \ detail/Futex.h \ diff --git a/folly/detail/ExceptionWrapper.h b/folly/detail/ExceptionWrapper.h deleted file mode 100644 index 8d5071a6..00000000 --- a/folly/detail/ExceptionWrapper.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2016 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -namespace folly { namespace detail { - -template -class Thrower { - public: - static void doThrow(std::exception* obj) { - throw *static_cast(obj); - } -}; - -}} -- 2.34.1