2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/detail/TryDetail.h>
25 Try<T>::Try(Try<T>&& t) noexcept : contains_(t.contains_) {
26 if (contains_ == Contains::VALUE) {
27 new (&value_)T(std::move(t.value_));
28 } else if (contains_ == Contains::EXCEPTION) {
29 new (&e_) exception_wrapper(std::move(t.e_));
35 Try<T>::Try(typename std::enable_if<std::is_same<Unit, T2>::value,
36 Try<void> const&>::type t)
37 : contains_(Contains::NOTHING) {
39 contains_ = Contains::VALUE;
41 } else if (t.hasException()) {
42 contains_ = Contains::EXCEPTION;
43 new (&e_) exception_wrapper(t.exception());
48 Try<T>& Try<T>::operator=(Try<T>&& t) noexcept {
54 contains_ = t.contains_;
55 if (contains_ == Contains::VALUE) {
56 new (&value_)T(std::move(t.value_));
57 } else if (contains_ == Contains::EXCEPTION) {
58 new (&e_) exception_wrapper(std::move(t.e_));
64 Try<T>::Try(const Try<T>& t) {
66 std::is_copy_constructible<T>::value,
67 "T must be copyable for Try<T> to be copyable");
68 contains_ = t.contains_;
69 if (contains_ == Contains::VALUE) {
70 new (&value_)T(t.value_);
71 } else if (contains_ == Contains::EXCEPTION) {
72 new (&e_) exception_wrapper(t.e_);
77 Try<T>& Try<T>::operator=(const Try<T>& t) {
79 std::is_copy_constructible<T>::value,
80 "T must be copyable for Try<T> to be copyable");
82 contains_ = t.contains_;
83 if (contains_ == Contains::VALUE) {
84 new (&value_)T(t.value_);
85 } else if (contains_ == Contains::EXCEPTION) {
86 new (&e_) exception_wrapper(t.e_);
93 if (LIKELY(contains_ == Contains::VALUE)) {
95 } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
96 e_.~exception_wrapper();
101 T& Try<T>::value() & {
107 T&& Try<T>::value() && {
109 return std::move(value_);
113 const T& Try<T>::value() const & {
119 void Try<T>::throwIfFailed() const {
121 case Contains::VALUE:
123 case Contains::EXCEPTION:
124 e_.throw_exception();
126 try_detail::throwUsingUninitializedTry();
130 void Try<void>::throwIfFailed() const {
132 e_.throw_exception();
136 template <typename F>
137 typename std::enable_if<
138 !std::is_same<typename std::result_of<F()>::type, void>::value,
139 Try<typename std::result_of<F()>::type>>::type
141 typedef typename std::result_of<F()>::type ResultType;
143 return Try<ResultType>(f());
144 } catch (std::exception& e) {
145 return Try<ResultType>(exception_wrapper(std::current_exception(), e));
147 return Try<ResultType>(exception_wrapper(std::current_exception()));
151 template <typename F>
152 typename std::enable_if<
153 std::is_same<typename std::result_of<F()>::type, void>::value,
159 } catch (std::exception& e) {
160 return Try<void>(exception_wrapper(std::current_exception(), e));
162 return Try<void>(exception_wrapper(std::current_exception()));
166 template <typename... Ts>
167 std::tuple<Ts...> unwrapTryTuple(std::tuple<folly::Try<Ts>...>&& ts) {
168 return detail::TryTuple<Ts...>::unwrap(
169 std::forward<std::tuple<folly::Try<Ts>...>>(ts));