X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FExceptionWrapper.cpp;h=97713ed307eeb35087519b307a9e5cce00478048;hb=79e5fd2fa293cf03269d4b7bba9ed7a31dda1cd8;hp=3beb92e8d2744ee4be363d343700b20ae62d0247;hpb=0bf2bc290b27e5567fcd5d23d02dd6a5c24dcb64;p=folly.git diff --git a/folly/ExceptionWrapper.cpp b/folly/ExceptionWrapper.cpp index 3beb92e8..97713ed3 100644 --- a/folly/ExceptionWrapper.cpp +++ b/folly/ExceptionWrapper.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,45 +15,76 @@ */ #include -#include #include +#include + namespace folly { -[[noreturn]] void exception_wrapper::throwException() const { - if (throwfn_) { - throwfn_(item_.get()); - } else if (eptr_) { - std::rethrow_exception(eptr_); +exception_wrapper::VTable const exception_wrapper::uninit_{ + &noop_, + &noop_, + &noop_, + &noop_, + &uninit_type_, + &noop_, + &noop_}; + +exception_wrapper::VTable const exception_wrapper::ExceptionPtr::ops_{ + copy_, + move_, + delete_, + throw_, + type_, + get_exception_, + get_exception_ptr_}; + +exception_wrapper::VTable const exception_wrapper::SharedPtr::ops_{ + copy_, + move_, + delete_, + throw_, + type_, + get_exception_, + get_exception_ptr_}; + +namespace { +std::exception const* get_std_exception_(std::exception_ptr eptr) noexcept { + try { + std::rethrow_exception(eptr); + } catch (const std::exception& ex) { + return &ex; + } catch (...) { + return nullptr; } - std::cerr - << "Cannot use `throwException` with an empty folly::exception_wrapper" - << std::endl; - std::terminate(); +} } -fbstring exception_wrapper::class_name() const { - if (item_) { - auto& i = *item_; - return demangle(typeid(i)); - } else if (eptr_) { - return ename_; - } else { - return fbstring(); +exception_wrapper::exception_wrapper(std::exception_ptr ptr) noexcept + : exception_wrapper{} { + if (ptr) { + if (auto e = get_std_exception_(ptr)) { + LOG(DFATAL) + << "Performance error: Please construct exception_wrapper with a " + "reference to the std::exception along with the " + "std::exception_ptr."; + *this = exception_wrapper{std::move(ptr), *e}; + } else { + Unknown uk; + *this = exception_wrapper{ptr, uk}; + } } } -fbstring exception_wrapper::what() const { - if (item_) { - return exceptionStr(*item_); - } else if (eptr_) { - return estr_; - } else { - return fbstring(); - } +[[noreturn]] void exception_wrapper::onNoExceptionError() { + std::ios_base::Init ioinit_; // ensure std::cerr is alive + std::cerr + << "Cannot use `throw_exception` with an empty folly::exception_wrapper" + << std::endl; + std::terminate(); } -fbstring exceptionStr(const exception_wrapper& ew) { +fbstring exceptionStr(exception_wrapper const& ew) { return ew.what(); }