2 * Copyright 2014 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 "WangleException.h"
23 namespace folly { namespace wangle {
26 Try<T>::Try(Try<T>&& t) : 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::exception_ptr(t.e_);
35 Try<T>& Try<T>::operator=(Try<T>&& t) {
37 contains_ = t.contains_;
38 if (contains_ == Contains::VALUE) {
39 new (&value_)T(std::move(t.value_));
40 } else if (contains_ == Contains::EXCEPTION) {
41 new (&e_)std::exception_ptr(t.e_);
48 if (contains_ == Contains::VALUE) {
50 } else if (contains_ == Contains::EXCEPTION) {
62 const T& Try<T>::value() const {
68 void Try<T>::throwIfFailed() const {
69 if (contains_ != Contains::VALUE) {
70 if (contains_ == Contains::EXCEPTION) {
71 std::rethrow_exception(e_);
73 throw UsingUninitializedTry();
78 void Try<void>::throwIfFailed() const {
80 std::rethrow_exception(e_);
85 inline T moveFromTry(wangle::Try<T>&& t) {
86 return std::move(t.value());
89 inline void moveFromTry(wangle::Try<void>&& t) {
94 typename std::enable_if<
95 !std::is_same<typename std::result_of<F()>::type, void>::value,
96 Try<typename std::result_of<F()>::type>>::type
97 makeTryFunction(F&& f) {
98 typedef typename std::result_of<F()>::type ResultType;
101 return Try<ResultType>(std::move(value));
103 return Try<ResultType>(std::current_exception());
107 template <typename F>
108 typename std::enable_if<
109 std::is_same<typename std::result_of<F()>::type, void>::value,
111 makeTryFunction(F&& f) {
116 return Try<void>(std::current_exception());