612f5e9e663a40b7aea65a1c7b9c813de3e07223
[folly.git] / folly / futures / Try-inl.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <stdexcept>
20
21 #include <folly/futures/FutureException.h>
22
23 namespace folly {
24
25 template <class T>
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::unique_ptr<exception_wrapper>(std::move(t.e_));
31   }
32 }
33
34 template <class T>
35 Try<T>& Try<T>::operator=(Try<T>&& t) {
36   this->~Try();
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::unique_ptr<exception_wrapper>(std::move(t.e_));
42   }
43   return *this;
44 }
45
46 template <class T>
47 Try<T>::~Try() {
48   if (contains_ == Contains::VALUE) {
49     value_.~T();
50   } else if (contains_ == Contains::EXCEPTION) {
51     e_.~unique_ptr<exception_wrapper>();
52   }
53 }
54
55 template <class T>
56 T& Try<T>::value() {
57   throwIfFailed();
58   return value_;
59 }
60
61 template <class T>
62 const T& Try<T>::value() const {
63   throwIfFailed();
64   return value_;
65 }
66
67 template <class T>
68 void Try<T>::throwIfFailed() const {
69   if (contains_ != Contains::VALUE) {
70     if (contains_ == Contains::EXCEPTION) {
71       e_->throwException();
72     } else {
73       throw UsingUninitializedTry();
74     }
75   }
76 }
77
78 void Try<void>::throwIfFailed() const {
79   if (!hasValue_) {
80     e_->throwException();
81   }
82 }
83
84 template <typename T>
85 inline T moveFromTry(Try<T>&& t) {
86   return std::move(t.value());
87 }
88
89 inline void moveFromTry(Try<void>&& t) {
90   return t.value();
91 }
92
93 template <typename F>
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;
99   try {
100     return Try<ResultType>(f());
101   } catch (std::exception& e) {
102     return Try<ResultType>(exception_wrapper(std::current_exception(), e));
103   } catch (...) {
104     return Try<ResultType>(exception_wrapper(std::current_exception()));
105   }
106 }
107
108 template <typename F>
109 typename std::enable_if<
110   std::is_same<typename std::result_of<F()>::type, void>::value,
111   Try<void>>::type
112 makeTryFunction(F&& f) {
113   try {
114     f();
115     return Try<void>();
116   } catch (std::exception& e) {
117     return Try<void>(exception_wrapper(std::current_exception(), e));
118   } catch (...) {
119     return Try<void>(exception_wrapper(std::current_exception()));
120   }
121 }
122
123 } // folly