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_)std::unique_ptr<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_) std::unique_ptr<exception_wrapper>(
44 folly::make_unique<exception_wrapper>(t.exception()));
49 Try<T>& Try<T>::operator=(Try<T>&& t) noexcept {
55 contains_ = t.contains_;
56 if (contains_ == Contains::VALUE) {
57 new (&value_)T(std::move(t.value_));
58 } else if (contains_ == Contains::EXCEPTION) {
59 new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
65 Try<T>::Try(const Try<T>& t) {
67 std::is_copy_constructible<T>::value,
68 "T must be copyable for Try<T> to be copyable");
69 contains_ = t.contains_;
70 if (contains_ == Contains::VALUE) {
71 new (&value_)T(t.value_);
72 } else if (contains_ == Contains::EXCEPTION) {
73 new (&e_)std::unique_ptr<exception_wrapper>();
74 e_ = folly::make_unique<exception_wrapper>(*(t.e_));
79 Try<T>& Try<T>::operator=(const Try<T>& t) {
81 std::is_copy_constructible<T>::value,
82 "T must be copyable for Try<T> to be copyable");
84 contains_ = t.contains_;
85 if (contains_ == Contains::VALUE) {
86 new (&value_)T(t.value_);
87 } else if (contains_ == Contains::EXCEPTION) {
88 new (&e_)std::unique_ptr<exception_wrapper>();
89 e_ = folly::make_unique<exception_wrapper>(*(t.e_));
96 if (LIKELY(contains_ == Contains::VALUE)) {
98 } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
99 e_.~unique_ptr<exception_wrapper>();
104 T& Try<T>::value() & {
110 T&& Try<T>::value() && {
112 return std::move(value_);
116 const T& Try<T>::value() const & {
122 void Try<T>::throwIfFailed() const {
123 if (contains_ != Contains::VALUE) {
124 if (contains_ == Contains::EXCEPTION) {
125 e_->throwException();
127 throw UsingUninitializedTry();
132 void Try<void>::throwIfFailed() const {
134 e_->throwException();
138 template <typename T>
139 inline T moveFromTry(Try<T>& t) {
140 return std::move(t.value());
143 inline void moveFromTry(Try<void>& t) {
147 template <typename F>
148 typename std::enable_if<
149 !std::is_same<typename std::result_of<F()>::type, void>::value,
150 Try<typename std::result_of<F()>::type>>::type
152 typedef typename std::result_of<F()>::type ResultType;
154 return Try<ResultType>(f());
155 } catch (std::exception& e) {
156 return Try<ResultType>(exception_wrapper(std::current_exception(), e));
158 return Try<ResultType>(exception_wrapper(std::current_exception()));
162 template <typename F>
163 typename std::enable_if<
164 std::is_same<typename std::result_of<F()>::type, void>::value,
170 } catch (std::exception& e) {
171 return Try<void>(exception_wrapper(std::current_exception(), e));
173 return Try<void>(exception_wrapper(std::current_exception()));
177 template <typename... Ts>
178 std::tuple<Ts...> unwrapTryTuple(std::tuple<folly::Try<Ts>...>&& ts) {
179 return detail::TryTuple<Ts...>::unwrap(
180 std::forward<std::tuple<folly::Try<Ts>...>>(ts));