2018-12-05 12:29:49 +03:00
|
|
|
/*
|
2019-05-29 14:32:35 +03:00
|
|
|
* Copyright 2014-present Facebook, Inc.
|
2018-12-05 12:29:49 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
#include <folly/ExceptionWrapper.h>
|
|
|
|
#include <folly/Likely.h>
|
|
|
|
#include <folly/Memory.h>
|
|
|
|
#include <folly/Portability.h>
|
|
|
|
#include <folly/Unit.h>
|
2019-05-29 14:32:35 +03:00
|
|
|
#include <folly/Utility.h>
|
|
|
|
#include <folly/functional/Invoke.h>
|
|
|
|
#include <folly/lang/Exception.h>
|
|
|
|
#include <exception>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
namespace folly {
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
class FOLLY_EXPORT TryException : public std::logic_error {
|
2018-12-05 12:29:49 +03:00
|
|
|
public:
|
2019-05-29 14:32:35 +03:00
|
|
|
using std::logic_error::logic_error;
|
2018-12-05 12:29:49 +03:00
|
|
|
};
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
class FOLLY_EXPORT UsingUninitializedTry : public TryException {
|
2018-12-05 12:29:49 +03:00
|
|
|
public:
|
2019-05-29 14:32:35 +03:00
|
|
|
UsingUninitializedTry() : TryException("Using uninitialized try") {}
|
2018-12-05 12:29:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try<T> is a wrapper that contains either an instance of T, an exception, or
|
|
|
|
* nothing. Exceptions are stored as exception_wrappers so that the user can
|
|
|
|
* minimize rethrows if so desired.
|
|
|
|
*
|
|
|
|
* To represent success or a captured exception, use Try<Unit>.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class Try {
|
2019-05-29 14:32:35 +03:00
|
|
|
static_assert(
|
|
|
|
!std::is_reference<T>::value,
|
|
|
|
"Try may not be used with reference types");
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
enum class Contains {
|
|
|
|
VALUE,
|
|
|
|
EXCEPTION,
|
|
|
|
NOTHING,
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* The value type for the Try
|
|
|
|
*/
|
|
|
|
typedef T element_type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct an empty Try
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
Try() noexcept : contains_(Contains::NOTHING) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a Try with a value by copy
|
|
|
|
*
|
|
|
|
* @param v The value to copy in
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
explicit Try(const T& v) noexcept(
|
|
|
|
std::is_nothrow_copy_constructible<T>::value)
|
|
|
|
: contains_(Contains::VALUE), value_(v) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a Try with a value by move
|
|
|
|
*
|
|
|
|
* @param v The value to move in
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
explicit Try(T&& v) noexcept(std::is_nothrow_move_constructible<T>::value)
|
|
|
|
: contains_(Contains::VALUE), value_(std::move(v)) {}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
explicit Try(in_place_t, Args&&... args) noexcept(
|
|
|
|
std::is_nothrow_constructible<T, Args&&...>::value)
|
|
|
|
: contains_(Contains::VALUE), value_(static_cast<Args&&>(args)...) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/// Implicit conversion from Try<void> to Try<Unit>
|
|
|
|
template <class T2 = T>
|
|
|
|
/* implicit */
|
2019-05-29 14:32:35 +03:00
|
|
|
Try(typename std::enable_if<std::is_same<Unit, T2>::value, Try<void> const&>::
|
|
|
|
type t) noexcept;
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a Try with an exception_wrapper
|
|
|
|
*
|
|
|
|
* @param e The exception_wrapper
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
explicit Try(exception_wrapper e) noexcept
|
|
|
|
: contains_(Contains::EXCEPTION), e_(std::move(e)) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
// Move constructor
|
2019-05-29 14:32:35 +03:00
|
|
|
Try(Try<T>&& t) noexcept(std::is_nothrow_move_constructible<T>::value);
|
2018-12-05 12:29:49 +03:00
|
|
|
// Move assigner
|
2019-05-29 14:32:35 +03:00
|
|
|
Try& operator=(Try<T>&& t) noexcept(
|
|
|
|
std::is_nothrow_move_constructible<T>::value);
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
// Copy constructor
|
2019-05-29 14:32:35 +03:00
|
|
|
Try(const Try& t) noexcept(std::is_nothrow_copy_constructible<T>::value);
|
2018-12-05 12:29:49 +03:00
|
|
|
// Copy assigner
|
2019-05-29 14:32:35 +03:00
|
|
|
Try& operator=(const Try& t) noexcept(
|
|
|
|
std::is_nothrow_copy_constructible<T>::value);
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
~Try();
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
/*
|
|
|
|
* In-place construct the value in the Try object.
|
|
|
|
*
|
|
|
|
* Destroys any previous value prior to constructing the new value.
|
|
|
|
* Leaves *this in an empty state if the construction of T throws.
|
|
|
|
*
|
|
|
|
* @returns reference to the newly constructed value.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
T& emplace(Args&&... args) noexcept(
|
|
|
|
std::is_nothrow_constructible<T, Args&&...>::value);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In-place construct an exception in the Try object.
|
|
|
|
*
|
|
|
|
* Destroys any previous value prior to constructing the new value.
|
|
|
|
* Leaves *this in an empty state if the construction of the exception_wrapper
|
|
|
|
* throws.
|
|
|
|
*
|
|
|
|
* Any arguments passed to emplaceException() are forwarded on to the
|
|
|
|
* exception_wrapper constructor.
|
|
|
|
*
|
|
|
|
* @returns reference to the newly constructed exception_wrapper.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
exception_wrapper& emplaceException(Args&&... args) noexcept(
|
|
|
|
std::is_nothrow_constructible<exception_wrapper, Args&&...>::value);
|
|
|
|
|
2018-12-05 12:29:49 +03:00
|
|
|
/*
|
|
|
|
* Get a mutable reference to the contained value. If the Try contains an
|
|
|
|
* exception it will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns mutable reference to the contained value
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
T& value() &;
|
2018-12-05 12:29:49 +03:00
|
|
|
/*
|
|
|
|
* Get a rvalue reference to the contained value. If the Try contains an
|
|
|
|
* exception it will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns rvalue reference to the contained value
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
T&& value() &&;
|
2018-12-05 12:29:49 +03:00
|
|
|
/*
|
|
|
|
* Get a const reference to the contained value. If the Try contains an
|
|
|
|
* exception it will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns const reference to the contained value
|
|
|
|
*/
|
|
|
|
const T& value() const&;
|
2019-05-29 14:32:35 +03:00
|
|
|
/*
|
|
|
|
* Get a const rvalue reference to the contained value. If the Try contains an
|
|
|
|
* exception it will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns const rvalue reference to the contained value
|
|
|
|
*/
|
|
|
|
const T&& value() const&&;
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the Try contains an exception, rethrow it. Otherwise do nothing.
|
|
|
|
*/
|
|
|
|
void throwIfFailed() const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Const dereference operator. If the Try contains an exception it will be
|
|
|
|
* rethrown.
|
|
|
|
*
|
|
|
|
* @returns const reference to the contained value
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
const T& operator*() const& {
|
|
|
|
return value();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
/*
|
|
|
|
* Dereference operator. If the Try contains an exception it will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns mutable reference to the contained value
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
T& operator*() & {
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Mutable rvalue dereference operator. If the Try contains an exception it
|
|
|
|
* will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns rvalue reference to the contained value
|
|
|
|
*/
|
|
|
|
T&& operator*() && {
|
|
|
|
return std::move(value());
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Const rvalue dereference operator. If the Try contains an exception it
|
|
|
|
* will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns rvalue reference to the contained value
|
|
|
|
*/
|
|
|
|
const T&& operator*() const&& {
|
|
|
|
return std::move(value());
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Const arrow operator. If the Try contains an exception it will be
|
|
|
|
* rethrown.
|
|
|
|
*
|
|
|
|
* @returns const reference to the contained value
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
const T* operator->() const {
|
|
|
|
return &value();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
/*
|
|
|
|
* Arrow operator. If the Try contains an exception it will be rethrown.
|
|
|
|
*
|
|
|
|
* @returns mutable reference to the contained value
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
T* operator->() {
|
|
|
|
return &value();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @returns True if the Try contains a value, false otherwise
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
bool hasValue() const {
|
|
|
|
return contains_ == Contains::VALUE;
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
/*
|
|
|
|
* @returns True if the Try contains an exception, false otherwise
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
bool hasException() const {
|
|
|
|
return contains_ == Contains::EXCEPTION;
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @returns True if the Try contains an exception of type Ex, false otherwise
|
|
|
|
*/
|
|
|
|
template <class Ex>
|
|
|
|
bool hasException() const {
|
2019-05-29 14:32:35 +03:00
|
|
|
return hasException() && e_.is_compatible_with<Ex>();
|
|
|
|
}
|
|
|
|
|
|
|
|
exception_wrapper& exception() & {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
|
|
|
}
|
|
|
|
return e_;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
exception_wrapper&& exception() && {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
|
|
|
}
|
|
|
|
return std::move(e_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const exception_wrapper& exception() const& {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
return e_;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
const exception_wrapper&& exception() const&& {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
return std::move(e_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @returns a pointer to the `std::exception` held by `*this`, if one is held;
|
|
|
|
* otherwise, returns `nullptr`.
|
|
|
|
*/
|
|
|
|
std::exception* tryGetExceptionObject() {
|
|
|
|
return hasException() ? e_.get_exception() : nullptr;
|
|
|
|
}
|
|
|
|
std::exception const* tryGetExceptionObject() const {
|
|
|
|
return hasException() ? e_.get_exception() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @returns a pointer to the `Ex` held by `*this`, if it holds an object whose
|
|
|
|
* type `From` permits `std::is_convertible<From*, Ex*>`; otherwise,
|
|
|
|
* returns `nullptr`.
|
|
|
|
*/
|
|
|
|
template <class E>
|
|
|
|
E* tryGetExceptionObject() {
|
|
|
|
return hasException() ? e_.get_exception<E>() : nullptr;
|
|
|
|
}
|
|
|
|
template <class E>
|
|
|
|
E const* tryGetExceptionObject() const {
|
|
|
|
return hasException() ? e_.get_exception<E>() : nullptr;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the Try contains an exception and it is of type Ex, execute func(Ex)
|
|
|
|
*
|
|
|
|
* @param func a function that takes a single parameter of type const Ex&
|
|
|
|
*
|
|
|
|
* @returns True if the Try held an Ex and func was executed, false otherwise
|
|
|
|
*/
|
|
|
|
template <class Ex, class F>
|
2019-05-29 14:32:35 +03:00
|
|
|
bool withException(F func) {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e_.with_exception<Ex>(std::move(func));
|
|
|
|
}
|
|
|
|
template <class Ex, class F>
|
|
|
|
bool withException(F func) const {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e_.with_exception<Ex>(std::move(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the Try contains an exception and it is of type compatible with Ex as
|
|
|
|
* deduced from the first parameter of func, execute func(Ex)
|
|
|
|
*
|
|
|
|
* @param func a function that takes a single parameter of type const Ex&
|
|
|
|
*
|
|
|
|
* @returns True if the Try held an Ex and func was executed, false otherwise
|
|
|
|
*/
|
|
|
|
template <class F>
|
|
|
|
bool withException(F func) {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e_.with_exception(std::move(func));
|
|
|
|
}
|
|
|
|
template <class F>
|
2018-12-05 12:29:49 +03:00
|
|
|
bool withException(F func) const {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
return e_.with_exception(std::move(func));
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool isTry, typename R>
|
|
|
|
typename std::enable_if<isTry, R>::type get() {
|
|
|
|
return std::forward<R>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool isTry, typename R>
|
|
|
|
typename std::enable_if<!isTry, R>::type get() {
|
|
|
|
return std::forward<R>(value());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-29 14:32:35 +03:00
|
|
|
void destroy() noexcept;
|
|
|
|
|
2018-12-05 12:29:49 +03:00
|
|
|
Contains contains_;
|
|
|
|
union {
|
|
|
|
T value_;
|
2019-05-29 14:32:35 +03:00
|
|
|
exception_wrapper e_;
|
2018-12-05 12:29:49 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specialization of Try for void value type. Encapsulates either success or an
|
|
|
|
* exception.
|
|
|
|
*/
|
|
|
|
template <>
|
|
|
|
class Try<void> {
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* The value type for the Try
|
|
|
|
*/
|
|
|
|
typedef void element_type;
|
|
|
|
|
|
|
|
// Construct a Try holding a successful and void result
|
2019-05-29 14:32:35 +03:00
|
|
|
Try() noexcept : hasValue_(true) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a Try with an exception_wrapper
|
|
|
|
*
|
|
|
|
* @param e The exception_wrapper
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
explicit Try(exception_wrapper e) noexcept
|
|
|
|
: hasValue_(false), e_(std::move(e)) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
// Copy assigner
|
|
|
|
inline Try& operator=(const Try<void>& t) noexcept;
|
|
|
|
|
|
|
|
// Copy constructor
|
|
|
|
Try(const Try<void>& t) noexcept : hasValue_(t.hasValue_) {
|
|
|
|
if (t.hasException()) {
|
|
|
|
new (&e_) exception_wrapper(t.e_);
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
~Try() {
|
|
|
|
if (hasException()) {
|
|
|
|
e_.~exception_wrapper();
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In-place construct a 'void' value into this Try object.
|
|
|
|
*
|
|
|
|
* This has the effect of clearing any existing exception stored in the
|
|
|
|
* Try object.
|
|
|
|
*/
|
|
|
|
void emplace() noexcept {
|
|
|
|
if (hasException()) {
|
|
|
|
e_.~exception_wrapper();
|
|
|
|
hasValue_ = true;
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
/*
|
|
|
|
* In-place construct an exception in the Try object.
|
|
|
|
*
|
|
|
|
* Destroys any previous value prior to constructing the new value.
|
|
|
|
* Leaves *this in an empty state if the construction of the exception_wrapper
|
|
|
|
* throws.
|
|
|
|
*
|
|
|
|
* Any arguments passed to emplaceException() are forwarded on to the
|
|
|
|
* exception_wrapper constructor.
|
|
|
|
*
|
|
|
|
* @returns reference to the newly constructed exception_wrapper.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
exception_wrapper& emplaceException(Args&&... args) noexcept(
|
|
|
|
std::is_nothrow_constructible<exception_wrapper, Args&&...>::value);
|
|
|
|
|
2018-12-05 12:29:49 +03:00
|
|
|
// If the Try contains an exception, throws it
|
2019-05-29 14:32:35 +03:00
|
|
|
void value() const {
|
|
|
|
throwIfFailed();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
// Dereference operator. If the Try contains an exception, throws it
|
2019-05-29 14:32:35 +03:00
|
|
|
void operator*() const {
|
|
|
|
return value();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
// If the Try contains an exception, throws it
|
|
|
|
inline void throwIfFailed() const;
|
|
|
|
|
|
|
|
// @returns False if the Try contains an exception, true otherwise
|
2019-05-29 14:32:35 +03:00
|
|
|
bool hasValue() const {
|
|
|
|
return hasValue_;
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
// @returns True if the Try contains an exception, false otherwise
|
2019-05-29 14:32:35 +03:00
|
|
|
bool hasException() const {
|
|
|
|
return !hasValue_;
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
// @returns True if the Try contains an exception of type Ex, false otherwise
|
|
|
|
template <class Ex>
|
|
|
|
bool hasException() const {
|
2019-05-29 14:32:35 +03:00
|
|
|
return hasException() && e_.is_compatible_with<Ex>();
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @throws TryException if the Try doesn't contain an exception
|
|
|
|
*
|
|
|
|
* @returns mutable reference to the exception contained by this Try
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
exception_wrapper& exception() & {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
|
|
|
}
|
|
|
|
return e_;
|
|
|
|
}
|
|
|
|
|
|
|
|
exception_wrapper&& exception() && {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
|
|
|
}
|
|
|
|
return std::move(e_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const exception_wrapper& exception() const& {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
return e_;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
const exception_wrapper&& exception() const&& {
|
|
|
|
if (!hasException()) {
|
|
|
|
throw_exception<TryException>("Try does not contain an exception");
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
return std::move(e_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @returns a pointer to the `std::exception` held by `*this`, if one is held;
|
|
|
|
* otherwise, returns `nullptr`.
|
|
|
|
*/
|
|
|
|
std::exception* tryGetExceptionObject() {
|
|
|
|
return hasException() ? e_.get_exception() : nullptr;
|
|
|
|
}
|
|
|
|
std::exception const* tryGetExceptionObject() const {
|
|
|
|
return hasException() ? e_.get_exception() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @returns a pointer to the `Ex` held by `*this`, if it holds an object whose
|
|
|
|
* type `From` permits `std::is_convertible<From*, Ex*>`; otherwise,
|
|
|
|
* returns `nullptr`.
|
|
|
|
*/
|
|
|
|
template <class E>
|
|
|
|
E* tryGetExceptionObject() {
|
|
|
|
return hasException() ? e_.get_exception<E>() : nullptr;
|
|
|
|
}
|
|
|
|
template <class E>
|
|
|
|
E const* tryGetExceptionObject() const {
|
|
|
|
return hasException() ? e_.get_exception<E>() : nullptr;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the Try contains an exception and it is of type Ex, execute func(Ex)
|
|
|
|
*
|
|
|
|
* @param func a function that takes a single parameter of type const Ex&
|
|
|
|
*
|
|
|
|
* @returns True if the Try held an Ex and func was executed, false otherwise
|
|
|
|
*/
|
|
|
|
template <class Ex, class F>
|
2019-05-29 14:32:35 +03:00
|
|
|
bool withException(F func) {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e_.with_exception<Ex>(std::move(func));
|
|
|
|
}
|
|
|
|
template <class Ex, class F>
|
|
|
|
bool withException(F func) const {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e_.with_exception<Ex>(std::move(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the Try contains an exception and it is of type compatible with Ex as
|
|
|
|
* deduced from the first parameter of func, execute func(Ex)
|
|
|
|
*
|
|
|
|
* @param func a function that takes a single parameter of type const Ex&
|
|
|
|
*
|
|
|
|
* @returns True if the Try held an Ex and func was executed, false otherwise
|
|
|
|
*/
|
|
|
|
template <class F>
|
|
|
|
bool withException(F func) {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e_.with_exception(std::move(func));
|
|
|
|
}
|
|
|
|
template <class F>
|
2018-12-05 12:29:49 +03:00
|
|
|
bool withException(F func) const {
|
|
|
|
if (!hasException()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
return e_.with_exception(std::move(func));
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool, typename R>
|
|
|
|
R get() {
|
|
|
|
return std::forward<R>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool hasValue_;
|
2019-05-29 14:32:35 +03:00
|
|
|
union {
|
|
|
|
exception_wrapper e_;
|
|
|
|
};
|
2018-12-05 12:29:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @param f a function to execute and capture the result of (value or exception)
|
|
|
|
*
|
|
|
|
* @returns Try holding the result of f
|
|
|
|
*/
|
|
|
|
template <typename F>
|
|
|
|
typename std::enable_if<
|
2019-05-29 14:32:35 +03:00
|
|
|
!std::is_same<invoke_result_t<F>, void>::value,
|
|
|
|
Try<invoke_result_t<F>>>::type
|
2018-12-05 12:29:49 +03:00
|
|
|
makeTryWith(F&& f);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specialization of makeTryWith for void return
|
|
|
|
*
|
|
|
|
* @param f a function to execute and capture the result of
|
|
|
|
*
|
|
|
|
* @returns Try<void> holding the result of f
|
|
|
|
*/
|
|
|
|
template <typename F>
|
2019-05-29 14:32:35 +03:00
|
|
|
typename std::
|
|
|
|
enable_if<std::is_same<invoke_result_t<F>, void>::value, Try<void>>::type
|
|
|
|
makeTryWith(F&& f);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to in-place construct a new value from the specified arguments.
|
|
|
|
*
|
|
|
|
* If T's constructor throws an exception then this is caught and the Try<T>
|
|
|
|
* object is initialised to hold that exception.
|
|
|
|
*
|
|
|
|
* @param args Are passed to T's constructor.
|
|
|
|
*/
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
T* tryEmplace(Try<T>& t, Args&&... args) noexcept;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overload of tryEmplace() for Try<void>.
|
|
|
|
*/
|
|
|
|
inline void tryEmplace(Try<void>& t) noexcept;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to in-place construct a new value from the result of a function.
|
|
|
|
*
|
|
|
|
* If the function completes successfully then attempts to in-place construct
|
|
|
|
* a value of type, T, passing the result of the function as the only parameter.
|
|
|
|
*
|
|
|
|
* If either the call to the function completes with an exception or the
|
|
|
|
* constructor completes with an exception then the exception is caught and
|
|
|
|
* stored in the Try object.
|
|
|
|
*
|
|
|
|
* @returns A pointer to the newly constructed object if it completed
|
|
|
|
* successfully, otherwise returns nullptr if the operation completed with
|
|
|
|
* an exception.
|
|
|
|
*/
|
|
|
|
template <typename T, typename Func>
|
|
|
|
T* tryEmplaceWith(Try<T>& t, Func&& func) noexcept;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specialization of tryEmplaceWith() for Try<void>.
|
|
|
|
*
|
|
|
|
* Calls func() and if it doesn't throw an exception then calls t.emplace().
|
|
|
|
* If func() throws then captures the exception in t using t.emplaceException().
|
|
|
|
*
|
|
|
|
* Func must be callable with zero parameters and must return void.
|
|
|
|
*
|
|
|
|
* @returns true if func() completed without an exception, false if func()
|
|
|
|
* threw an exception.
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
bool tryEmplaceWith(Try<void>& t, Func&& func) noexcept;
|
2018-12-05 12:29:49 +03:00
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
/**
|
|
|
|
* Tuple<Try<Type>...> -> std::tuple<Type...>
|
|
|
|
*
|
|
|
|
* Unwraps a tuple-like type containing a sequence of Try<Type> instances to
|
|
|
|
* std::tuple<Type>
|
|
|
|
*/
|
|
|
|
template <typename Tuple>
|
|
|
|
auto unwrapTryTuple(Tuple&&);
|
2018-12-05 12:29:49 +03:00
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
} // namespace folly
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
#include <folly/Try-inl.h>
|