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/futures/Deprecated.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 explicit Try(std::exception_ptr ep) DEPRECATED
98 : contains_(Contains::EXCEPTION) {
100 std::rethrow_exception(ep);
101 } catch (const std::exception& e) {
102 e_ = folly::make_unique<exception_wrapper>(std::current_exception(), e);
104 e_ = folly::make_unique<exception_wrapper>(std::current_exception());
109 Try(Try<T>&& t) noexcept;
111 Try& operator=(Try<T>&& t) noexcept;
116 Try& operator=(const Try& t);
121 * Get a mutable reference to the contained value. If the Try contains an
122 * exception it will be rethrown.
124 * @returns mutable reference to the contained value
128 * Get a const reference to the contained value. If the Try contains an
129 * exception it will be rethrown.
131 * @returns const reference to the contained value
133 const T& value() const;
136 * If the Try contains an exception, rethrow it. Otherwise do nothing.
138 void throwIfFailed() const;
141 * Const dereference operator. If the Try contains an exception it will be
144 * @returns const reference to the contained value
146 const T& operator*() const { return value(); }
148 * Dereference operator. If the Try contains an exception it will be rethrown.
150 * @returns mutable reference to the contained value
152 T& operator*() { return value(); }
155 * Const arrow operator. If the Try contains an exception it will be
158 * @returns const reference to the contained value
160 const T* operator->() const { return &value(); }
162 * Arrow operator. If the Try contains an exception it will be rethrown.
164 * @returns mutable reference to the contained value
166 T* operator->() { return &value(); }
169 * @returns True if the Try contains a value, false otherwise
171 bool hasValue() const { return contains_ == Contains::VALUE; }
173 * @returns True if the Try contains an exception, false otherwise
175 bool hasException() const { return contains_ == Contains::EXCEPTION; }
178 * @returns True if the Try contains an exception of type Ex, false otherwise
181 bool hasException() const {
182 return hasException() && e_->is_compatible_with<Ex>();
185 exception_wrapper& exception() {
186 if (UNLIKELY(!hasException())) {
187 throw FutureException("exception(): Try does not contain an exception");
192 const exception_wrapper& exception() const {
193 if (UNLIKELY(!hasException())) {
194 throw FutureException("exception(): Try does not contain an exception");
200 * If the Try contains an exception and it is of type Ex, execute func(Ex)
202 * @param func a function that takes a single parameter of type const Ex&
204 * @returns True if the Try held an Ex and func was executed, false otherwise
206 template <class Ex, class F>
207 bool withException(F func) const {
208 if (!hasException()) {
211 return e_->with_exception<Ex>(std::move(func));
214 template <bool isTry, typename R>
215 typename std::enable_if<isTry, R>::type get() {
216 return std::forward<R>(*this);
219 template <bool isTry, typename R>
220 typename std::enable_if<!isTry, R>::type get() {
221 return std::forward<R>(value());
228 std::unique_ptr<exception_wrapper> e_;
233 * Specialization of Try for void value type. Encapsulates either success or an
239 // Construct a Try holding a successful and void result
240 Try() : hasValue_(true) {}
243 * Construct a Try with an exception_wrapper
245 * @param e The exception_wrapper
247 explicit Try(exception_wrapper e)
249 e_(folly::make_unique<exception_wrapper>(std::move(e))) {}
253 * Construct a Try with an exception_pointer
255 * @param ep The exception_pointer. Will be rethrown.
257 explicit Try(std::exception_ptr ep) DEPRECATED : hasValue_(false) {
259 std::rethrow_exception(ep);
260 } catch (const std::exception& e) {
261 e_ = folly::make_unique<exception_wrapper>(std::current_exception(), e);
263 e_ = folly::make_unique<exception_wrapper>(std::current_exception());
268 Try& operator=(const Try<void>& t) {
269 hasValue_ = t.hasValue_;
271 e_ = folly::make_unique<exception_wrapper>(*t.e_);
276 Try(const Try<void>& t) {
280 // If the Try contains an exception, throws it
281 void value() const { throwIfFailed(); }
282 // Dereference operator. If the Try contains an exception, throws it
283 void operator*() const { return value(); }
285 // If the Try contains an exception, throws it
286 inline void throwIfFailed() const;
288 // @returns False if the Try contains an exception, true otherwise
289 bool hasValue() const { return hasValue_; }
290 // @returns True if the Try contains an exception, false otherwise
291 bool hasException() const { return !hasValue_; }
293 // @returns True if the Try contains an exception of type Ex, false otherwise
295 bool hasException() const {
296 return hasException() && e_->is_compatible_with<Ex>();
300 * @throws FutureException if the Try doesn't contain an exception
302 * @returns mutable reference to the exception contained by this Try
304 exception_wrapper& exception() {
305 if (UNLIKELY(!hasException())) {
306 throw FutureException("exception(): Try does not contain an exception");
311 const exception_wrapper& exception() const {
312 if (UNLIKELY(!hasException())) {
313 throw FutureException("exception(): Try does not contain an exception");
319 * If the Try contains an exception and it is of type Ex, execute func(Ex)
321 * @param func a function that takes a single parameter of type const Ex&
323 * @returns True if the Try held an Ex and func was executed, false otherwise
325 template <class Ex, class F>
326 bool withException(F func) const {
327 if (!hasException()) {
330 return e_->with_exception<Ex>(std::move(func));
333 template <bool, typename R>
335 return std::forward<R>(*this);
340 std::unique_ptr<exception_wrapper> e_{nullptr};
344 * Extracts value from try and returns it. Throws if try contained an exception.
346 * @param t Try to extract value from
348 * @returns value contained in t
350 template <typename T>
351 T moveFromTry(Try<T>&& t);
354 * Throws if try contained an exception.
356 * @param t Try to move from
358 void moveFromTry(Try<void>&& t);
361 * @param f a function to execute and capture the result of (value or exception)
363 * @returns Try holding the result of f
365 template <typename F>
366 typename std::enable_if<
367 !std::is_same<typename std::result_of<F()>::type, void>::value,
368 Try<typename std::result_of<F()>::type>>::type
372 * Specialization of makeTryWith for void return
374 * @param f a function to execute and capture the result of
376 * @returns Try<void> holding the result of f
378 template <typename F>
379 typename std::enable_if<
380 std::is_same<typename std::result_of<F()>::type, void>::value,
386 #include <folly/futures/Try-inl.h>