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.
19 #include <type_traits>
22 #include <folly/ExceptionWrapper.h>
23 #include <folly/Likely.h>
24 #include <folly/Memory.h>
25 #include <folly/Portability.h>
26 #include <folly/futures/FutureException.h>
27 #include <folly/futures/Unit.h>
32 * Try<T> is a wrapper that contains either an instance of T, an exception, or
33 * nothing. Its primary use case is as a representation of a Promise or Future's
34 * result and so it provides a number of methods that are useful in that
35 * context. Exceptions are stored as exception_wrappers so that the user can
36 * minimize rethrows if so desired.
38 * To represent success or a captured exception, use Try<Unit>
42 static_assert(!std::is_reference<T>::value,
43 "Try may not be used with reference types");
53 * The value type for the Try
55 typedef T element_type;
58 * Construct an empty Try
60 Try() : contains_(Contains::NOTHING) {}
63 * Construct a Try with a value by copy
65 * @param v The value to copy in
67 explicit Try(const T& v) : contains_(Contains::VALUE), value_(v) {}
70 * Construct a Try with a value by move
72 * @param v The value to move in
74 explicit Try(T&& v) : contains_(Contains::VALUE), value_(std::move(v)) {}
76 /// Implicit conversion from Try<void> to Try<Unit>
77 template <class T2 = T>
79 Try(typename std::enable_if<std::is_same<Unit, T2>::value,
80 Try<void> const&>::type t);
83 * Construct a Try with an exception_wrapper
85 * @param e The exception_wrapper
87 explicit Try(exception_wrapper e)
88 : contains_(Contains::EXCEPTION),
89 e_(folly::make_unique<exception_wrapper>(std::move(e))) {}
93 * Construct a Try with an exception_pointer
95 * @param ep The exception_pointer. Will be rethrown.
97 FOLLY_DEPRECATED("use Try(exception_wrapper)")
98 explicit Try(std::exception_ptr ep)
99 : contains_(Contains::EXCEPTION) {
101 std::rethrow_exception(ep);
102 } catch (const std::exception& e) {
103 e_ = folly::make_unique<exception_wrapper>(std::current_exception(), e);
105 e_ = folly::make_unique<exception_wrapper>(std::current_exception());
110 Try(Try<T>&& t) noexcept;
112 Try& operator=(Try<T>&& t) noexcept;
117 Try& operator=(const Try& t);
122 * Get a mutable reference to the contained value. If the Try contains an
123 * exception it will be rethrown.
125 * @returns mutable reference to the contained value
129 * Get a const reference to the contained value. If the Try contains an
130 * exception it will be rethrown.
132 * @returns const reference to the contained value
134 const T& value() const;
137 * If the Try contains an exception, rethrow it. Otherwise do nothing.
139 void throwIfFailed() const;
142 * Const dereference operator. If the Try contains an exception it will be
145 * @returns const reference to the contained value
147 const T& operator*() const { return value(); }
149 * Dereference operator. If the Try contains an exception it will be rethrown.
151 * @returns mutable reference to the contained value
153 T& operator*() { return value(); }
156 * Const arrow operator. If the Try contains an exception it will be
159 * @returns const reference to the contained value
161 const T* operator->() const { return &value(); }
163 * Arrow operator. If the Try contains an exception it will be rethrown.
165 * @returns mutable reference to the contained value
167 T* operator->() { return &value(); }
170 * @returns True if the Try contains a value, false otherwise
172 bool hasValue() const { return contains_ == Contains::VALUE; }
174 * @returns True if the Try contains an exception, false otherwise
176 bool hasException() const { return contains_ == Contains::EXCEPTION; }
179 * @returns True if the Try contains an exception of type Ex, false otherwise
182 bool hasException() const {
183 return hasException() && e_->is_compatible_with<Ex>();
186 exception_wrapper& exception() {
187 if (UNLIKELY(!hasException())) {
188 throw FutureException("exception(): Try does not contain an exception");
193 const exception_wrapper& exception() const {
194 if (UNLIKELY(!hasException())) {
195 throw FutureException("exception(): Try does not contain an exception");
201 * If the Try contains an exception and it is of type Ex, execute func(Ex)
203 * @param func a function that takes a single parameter of type const Ex&
205 * @returns True if the Try held an Ex and func was executed, false otherwise
207 template <class Ex, class F>
208 bool withException(F func) const {
209 if (!hasException()) {
212 return e_->with_exception<Ex>(std::move(func));
215 template <bool isTry, typename R>
216 typename std::enable_if<isTry, R>::type get() {
217 return std::forward<R>(*this);
220 template <bool isTry, typename R>
221 typename std::enable_if<!isTry, R>::type get() {
222 return std::forward<R>(value());
229 std::unique_ptr<exception_wrapper> e_;
234 * Specialization of Try for void value type. Encapsulates either success or an
240 // Construct a Try holding a successful and void result
241 Try() : hasValue_(true) {}
244 * Construct a Try with an exception_wrapper
246 * @param e The exception_wrapper
248 explicit Try(exception_wrapper e)
250 e_(folly::make_unique<exception_wrapper>(std::move(e))) {}
254 * Construct a Try with an exception_pointer
256 * @param ep The exception_pointer. Will be rethrown.
258 FOLLY_DEPRECATED("use Try(exception_wrapper)")
259 explicit Try(std::exception_ptr ep) : hasValue_(false) {
261 std::rethrow_exception(ep);
262 } catch (const std::exception& e) {
263 e_ = folly::make_unique<exception_wrapper>(std::current_exception(), e);
265 e_ = folly::make_unique<exception_wrapper>(std::current_exception());
270 Try& operator=(const Try<void>& t) {
271 hasValue_ = t.hasValue_;
273 e_ = folly::make_unique<exception_wrapper>(*t.e_);
278 Try(const Try<void>& t) {
282 // If the Try contains an exception, throws it
283 void value() const { throwIfFailed(); }
284 // Dereference operator. If the Try contains an exception, throws it
285 void operator*() const { return value(); }
287 // If the Try contains an exception, throws it
288 inline void throwIfFailed() const;
290 // @returns False if the Try contains an exception, true otherwise
291 bool hasValue() const { return hasValue_; }
292 // @returns True if the Try contains an exception, false otherwise
293 bool hasException() const { return !hasValue_; }
295 // @returns True if the Try contains an exception of type Ex, false otherwise
297 bool hasException() const {
298 return hasException() && e_->is_compatible_with<Ex>();
302 * @throws FutureException if the Try doesn't contain an exception
304 * @returns mutable reference to the exception contained by this Try
306 exception_wrapper& exception() {
307 if (UNLIKELY(!hasException())) {
308 throw FutureException("exception(): Try does not contain an exception");
313 const exception_wrapper& exception() const {
314 if (UNLIKELY(!hasException())) {
315 throw FutureException("exception(): Try does not contain an exception");
321 * If the Try contains an exception and it is of type Ex, execute func(Ex)
323 * @param func a function that takes a single parameter of type const Ex&
325 * @returns True if the Try held an Ex and func was executed, false otherwise
327 template <class Ex, class F>
328 bool withException(F func) const {
329 if (!hasException()) {
332 return e_->with_exception<Ex>(std::move(func));
335 template <bool, typename R>
337 return std::forward<R>(*this);
342 std::unique_ptr<exception_wrapper> e_{nullptr};
346 * Extracts value from try and returns it. Throws if try contained an exception.
348 * @param t Try to extract value from
350 * @returns value contained in t
352 template <typename T>
353 T moveFromTry(Try<T>&& t);
356 * Throws if try contained an exception.
358 * @param t Try to move from
360 void moveFromTry(Try<void>&& t);
363 * @param f a function to execute and capture the result of (value or exception)
365 * @returns Try holding the result of f
367 template <typename F>
368 typename std::enable_if<
369 !std::is_same<typename std::result_of<F()>::type, void>::value,
370 Try<typename std::result_of<F()>::type>>::type
374 * Specialization of makeTryWith for void return
376 * @param f a function to execute and capture the result of
378 * @returns Try<void> holding the result of f
380 template <typename F>
381 typename std::enable_if<
382 std::is_same<typename std::result_of<F()>::type, void>::value,
388 #include <folly/futures/Try-inl.h>