2 * Copyright 2015 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.
21 #include <folly/futures/FutureException.h>
26 Try<T>::Try(Try<T>&& t) noexcept : contains_(t.contains_) {
27 if (contains_ == Contains::VALUE) {
28 new (&value_)T(std::move(t.value_));
29 } else if (contains_ == Contains::EXCEPTION) {
30 new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
36 Try<T>::Try(typename std::enable_if<std::is_same<Unit, T2>::value,
37 Try<void> const&>::type t)
38 : contains_(Contains::NOTHING) {
40 contains_ = Contains::VALUE;
42 } else if (t.hasException()) {
43 contains_ = Contains::EXCEPTION;
44 new (&e_) std::unique_ptr<exception_wrapper>(
45 folly::make_unique<exception_wrapper>(t.exception()));
50 Try<T>& Try<T>::operator=(Try<T>&& t) noexcept {
56 contains_ = t.contains_;
57 if (contains_ == Contains::VALUE) {
58 new (&value_)T(std::move(t.value_));
59 } else if (contains_ == Contains::EXCEPTION) {
60 new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
66 Try<T>::Try(const Try<T>& t) {
68 std::is_copy_constructible<T>::value,
69 "T must be copyable for Try<T> to be copyable");
70 contains_ = t.contains_;
71 if (contains_ == Contains::VALUE) {
72 new (&value_)T(t.value_);
73 } else if (contains_ == Contains::EXCEPTION) {
74 new (&e_)std::unique_ptr<exception_wrapper>();
75 e_ = folly::make_unique<exception_wrapper>(*(t.e_));
80 Try<T>& Try<T>::operator=(const Try<T>& t) {
82 std::is_copy_constructible<T>::value,
83 "T must be copyable for Try<T> to be copyable");
85 contains_ = t.contains_;
86 if (contains_ == Contains::VALUE) {
87 new (&value_)T(t.value_);
88 } else if (contains_ == Contains::EXCEPTION) {
89 new (&e_)std::unique_ptr<exception_wrapper>();
90 e_ = folly::make_unique<exception_wrapper>(*(t.e_));
97 if (LIKELY(contains_ == Contains::VALUE)) {
99 } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
100 e_.~unique_ptr<exception_wrapper>();
111 const T& Try<T>::value() const {
117 void Try<T>::throwIfFailed() const {
118 if (contains_ != Contains::VALUE) {
119 if (contains_ == Contains::EXCEPTION) {
120 e_->throwException();
122 throw UsingUninitializedTry();
127 void Try<void>::throwIfFailed() const {
129 e_->throwException();
133 template <typename T>
134 inline T moveFromTry(Try<T>& t) {
135 return std::move(t.value());
138 inline void moveFromTry(Try<void>& t) {
142 template <typename F>
143 typename std::enable_if<
144 !std::is_same<typename std::result_of<F()>::type, void>::value,
145 Try<typename std::result_of<F()>::type>>::type
147 typedef typename std::result_of<F()>::type ResultType;
149 return Try<ResultType>(f());
150 } catch (std::exception& e) {
151 return Try<ResultType>(exception_wrapper(std::current_exception(), e));
153 return Try<ResultType>(exception_wrapper(std::current_exception()));
157 template <typename F>
158 typename std::enable_if<
159 std::is_same<typename std::result_of<F()>::type, void>::value,
165 } catch (std::exception& e) {
166 return Try<void>(exception_wrapper(std::current_exception(), e));
168 return Try<void>(exception_wrapper(std::current_exception()));