/*
- * Copyright 2017-present Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <typeinfo>
#include <utility>
-#include <folly/Assume.h>
#include <folly/CPortability.h>
#include <folly/Demangle.h>
#include <folly/ExceptionString.h>
#include <folly/Portability.h>
#include <folly/Traits.h>
#include <folly/Utility.h>
+#include <folly/lang/Assume.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
//! \endcode
class exception_wrapper final {
private:
- struct AnyException : std::exception {
+ struct FOLLY_EXPORT AnyException : std::exception {
std::type_info const* typeinfo_;
template <class T>
/* implicit */ AnyException(T&& t) noexcept : typeinfo_(&typeid(t)) {}
Ex const& as() const noexcept;
};
- enum class Placement { kInSitu, kOnHeap };
+ struct ThrownTag {};
+ struct InSituTag {};
+ struct OnHeapTag {};
+
template <class T>
- using PlacementOf = std::integral_constant<
- Placement,
- sizeof(T) <= sizeof(Buffer::Storage) &&
+ using PlacementOf = _t<std::conditional<
+ !IsStdException<T>::value,
+ ThrownTag,
+ _t<std::conditional<
+ sizeof(T) <= sizeof(Buffer::Storage) &&
alignof(T) <= alignof(Buffer::Storage) &&
- noexcept(T(std::declval<T&&>()))
- ? Placement::kInSitu
- : Placement::kOnHeap>;
-
- using InSituTag = std::integral_constant<Placement, Placement::kInSitu>;
- using OnHeapTag = std::integral_constant<Placement, Placement::kOnHeap>;
+ noexcept(T(std::declval<T&&>())),
+ InSituTag,
+ OnHeapTag>>>>;
static std::exception const* as_exception_or_null_(std::exception const& ex);
static std::exception const* as_exception_or_null_(AnyException);
template <class Ex>
struct InPlace {
+ static_assert(IsStdException<Ex>::value, "only deriving std::exception");
static void copy_(exception_wrapper const* from, exception_wrapper* to);
static void move_(exception_wrapper* from, exception_wrapper* to);
static void delete_(exception_wrapper* that);
};
template <class Ex>
struct Impl final : public Base {
+ static_assert(IsStdException<Ex>::value, "only deriving std::exception");
Ex ex_;
Impl() = default;
template <typename... As>
};
VTable const* vptr_{&uninit_};
+ template <class Ex, typename... As>
+ exception_wrapper(ThrownTag, in_place_type_t<Ex>, As&&... as);
+
template <class Ex, typename... As>
exception_wrapper(OnHeapTag, in_place_type_t<Ex>, As&&... as);
static bool with_exception_(This& this_, Fn fn_);
public:
+ static exception_wrapper from_exception_ptr(
+ std::exception_ptr const& eptr) noexcept;
+
//! Default-constructs an empty `exception_wrapper`
//! \post `type() == none()`
exception_wrapper() noexcept {}
return exception_wrapper(std::current_exception(), ex);
}
}
-} // detail
+} // namespace detail
//! `try_and_catch` is a simple replacement for `try {} catch(){}`` that allows
//! you to specify which derived exceptions you would like to catch and store in
exception_wrapper try_and_catch(F&& fn) {
return detail::try_and_catch_<F, Exceptions...>(std::forward<F>(fn));
}
-} // folly
+} // namespace folly
#include <folly/ExceptionWrapper-inl.h>