Outline the throw statements in Try
[folly.git] / folly / Try-inl.h
1 /*
2  * Copyright 2017 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 <folly/detail/TryDetail.h>
20 #include <stdexcept>
21
22 namespace folly {
23
24 template <class T>
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_));
30   }
31 }
32
33 template <class T>
34 template <class T2>
35 Try<T>::Try(typename std::enable_if<std::is_same<Unit, T2>::value,
36                                     Try<void> const&>::type t)
37     : contains_(Contains::NOTHING) {
38   if (t.hasValue()) {
39     contains_ = Contains::VALUE;
40     new (&value_) T();
41   } else if (t.hasException()) {
42     contains_ = Contains::EXCEPTION;
43     new (&e_) exception_wrapper(t.exception());
44   }
45 }
46
47 template <class T>
48 Try<T>& Try<T>::operator=(Try<T>&& t) noexcept {
49   if (this == &t) {
50     return *this;
51   }
52
53   this->~Try();
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_));
59   }
60   return *this;
61 }
62
63 template <class T>
64 Try<T>::Try(const Try<T>& t) {
65   static_assert(
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_);
73   }
74 }
75
76 template <class T>
77 Try<T>& Try<T>::operator=(const Try<T>& t) {
78   static_assert(
79       std::is_copy_constructible<T>::value,
80       "T must be copyable for Try<T> to be copyable");
81   this->~Try();
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_);
87   }
88   return *this;
89 }
90
91 template <class T>
92 Try<T>::~Try() {
93   if (LIKELY(contains_ == Contains::VALUE)) {
94     value_.~T();
95   } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
96     e_.~exception_wrapper();
97   }
98 }
99
100 template <class T>
101 T& Try<T>::value() & {
102   throwIfFailed();
103   return value_;
104 }
105
106 template <class T>
107 T&& Try<T>::value() && {
108   throwIfFailed();
109   return std::move(value_);
110 }
111
112 template <class T>
113 const T& Try<T>::value() const & {
114   throwIfFailed();
115   return value_;
116 }
117
118 template <class T>
119 void Try<T>::throwIfFailed() const {
120   switch (contains_) {
121     case Contains::VALUE:
122       return;
123     case Contains::EXCEPTION:
124       e_.throw_exception();
125     default:
126       try_detail::throwUsingUninitializedTry();
127   }
128 }
129
130 void Try<void>::throwIfFailed() const {
131   if (!hasValue_) {
132     e_.throw_exception();
133   }
134 }
135
136 template <typename T>
137 inline T moveFromTry(Try<T>& t) {
138   return std::move(t.value());
139 }
140
141 inline void moveFromTry(Try<void>& t) {
142   return t.value();
143 }
144
145 template <typename F>
146 typename std::enable_if<
147   !std::is_same<typename std::result_of<F()>::type, void>::value,
148   Try<typename std::result_of<F()>::type>>::type
149 makeTryWith(F&& f) {
150   typedef typename std::result_of<F()>::type ResultType;
151   try {
152     return Try<ResultType>(f());
153   } catch (std::exception& e) {
154     return Try<ResultType>(exception_wrapper(std::current_exception(), e));
155   } catch (...) {
156     return Try<ResultType>(exception_wrapper(std::current_exception()));
157   }
158 }
159
160 template <typename F>
161 typename std::enable_if<
162   std::is_same<typename std::result_of<F()>::type, void>::value,
163   Try<void>>::type
164 makeTryWith(F&& f) {
165   try {
166     f();
167     return Try<void>();
168   } catch (std::exception& e) {
169     return Try<void>(exception_wrapper(std::current_exception(), e));
170   } catch (...) {
171     return Try<void>(exception_wrapper(std::current_exception()));
172   }
173 }
174
175 template <typename... Ts>
176 std::tuple<Ts...> unwrapTryTuple(std::tuple<folly::Try<Ts>...>&& ts) {
177   return detail::TryTuple<Ts...>::unwrap(
178       std::forward<std::tuple<folly::Try<Ts>...>>(ts));
179 }
180
181 } // folly