Move folly/detail/Sleeper.h to folly/synchronization/detail/
[folly.git] / folly / ExceptionWrapper.cpp
index c1440a57a40a9f67d84d8301ba4babd081560be6..f2fa016437558dc3be01c13a29e970f272b087af 100644 (file)
 
 namespace folly {
 
-constexpr exception_wrapper::VTable const exception_wrapper::uninit_;
-constexpr exception_wrapper::VTable const exception_wrapper::ExceptionPtr::ops_;
-constexpr exception_wrapper::VTable const exception_wrapper::SharedPtr::ops_;
+exception_wrapper::VTable const exception_wrapper::uninit_{
+    &noop_<void, exception_wrapper const*, exception_wrapper*>,
+    &noop_<void, exception_wrapper*, exception_wrapper*>,
+    &noop_<void, exception_wrapper*>,
+    &noop_<void, exception_wrapper const*>,
+    &uninit_type_,
+    &noop_<std::exception const*, exception_wrapper const*>,
+    &noop_<exception_wrapper, exception_wrapper const*>};
+
+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 {
@@ -35,6 +58,20 @@ std::exception const* get_std_exception_(std::exception_ptr eptr) noexcept {
     return nullptr;
   }
 }
+} // namespace
+
+exception_wrapper exception_wrapper::from_exception_ptr(
+    std::exception_ptr const& ptr) noexcept {
+  if (!ptr) {
+    return exception_wrapper();
+  }
+  try {
+    std::rethrow_exception(ptr);
+  } catch (std::exception& e) {
+    return exception_wrapper(std::current_exception(), e);
+  } catch (...) {
+    return exception_wrapper(std::current_exception());
+  }
 }
 
 exception_wrapper::exception_wrapper(std::exception_ptr ptr) noexcept
@@ -53,11 +90,11 @@ exception_wrapper::exception_wrapper(std::exception_ptr ptr) noexcept
   }
 }
 
-[[noreturn]] void exception_wrapper::onNoExceptionError() {
+[[noreturn]] void exception_wrapper::onNoExceptionError(
+    char const* const name) {
   std::ios_base::Init ioinit_; // ensure std::cerr is alive
-  std::cerr
-      << "Cannot use `throwException` with an empty folly::exception_wrapper"
-      << std::endl;
+  std::cerr << "Cannot use `" << name
+            << "` with an empty folly::exception_wrapper" << std::endl;
   std::terminate();
 }
 
@@ -65,4 +102,4 @@ fbstring exceptionStr(exception_wrapper const& ew) {
   return ew.what();
 }
 
-} // folly
+} // namespace folly