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.
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>
31 * Try<T> is a wrapper that contains either an instance of T, an exception, or
32 * nothing. Its primary use case is as a representation of a Promise or Future's
33 * result and so it provides a number of methods that are useful in that
34 * context. Exceptions are stored as exception_wrappers so that the user can
35 * minimize rethrows if so desired.
37 * There is a specialization, Try<void>, which represents either success
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)) {}
77 * Construct a Try with an exception_wrapper
79 * @param e The exception_wrapper
81 explicit Try(exception_wrapper e)
82 : contains_(Contains::EXCEPTION),
83 e_(folly::make_unique<exception_wrapper>(std::move(e))) {}
87 * Construct a Try with an exception_pointer
89 * @param ep The exception_pointer. Will be rethrown.
91 explicit Try(std::exception_ptr ep) DEPRECATED
92 : contains_(Contains::EXCEPTION) {
94 std::rethrow_exception(ep);
95 } catch (const std::exception& e) {
96 e_ = folly::make_unique<exception_wrapper>(std::current_exception(), e);
98 e_ = folly::make_unique<exception_wrapper>(std::current_exception());
105 Try& operator=(Try<T>&& t);
108 Try(const Try<T>& t) = delete;
110 Try& operator=(const Try<T>& t) = delete;
115 * Get a mutable reference to the contained value. If the Try contains an
116 * exception it will be rethrown.
118 * @returns mutable reference to the contained value
122 * Get a const reference to the contained value. If the Try contains an
123 * exception it will be rethrown.
125 * @returns const reference to the contained value
127 const T& value() const;
130 * If the Try contains an exception, rethrow it. Otherwise do nothing.
132 void throwIfFailed() const;
135 * Const dereference operator. If the Try contains an exception it will be
138 * @returns const reference to the contained value
140 const T& operator*() const { return value(); }
142 * Dereference operator. If the Try contains an exception it will be rethrown.
144 * @returns mutable reference to the contained value
146 T& operator*() { return value(); }
149 * Const arrow operator. If the Try contains an exception it will be
152 * @returns const reference to the contained value
154 const T* operator->() const { return &value(); }
156 * Arrow operator. If the Try contains an exception it will be rethrown.
158 * @returns mutable reference to the contained value
160 T* operator->() { return &value(); }
163 * @returns True if the Try contains a value, false otherwise
165 bool hasValue() const { return contains_ == Contains::VALUE; }
167 * @returns True if the Try contains an exception, false otherwise
169 bool hasException() const { return contains_ == Contains::EXCEPTION; }
172 * @returns True if the Try contains an exception of type Ex, false otherwise
175 bool hasException() const {
176 return hasException() && e_->is_compatible_with<Ex>();
179 exception_wrapper& exception() {
180 if (UNLIKELY(!hasException())) {
181 throw FutureException("exception(): Try does not contain an exception");
186 const exception_wrapper& exception() const {
187 if (UNLIKELY(!hasException())) {
188 throw FutureException("exception(): Try does not contain an exception");
194 * If the Try contains an exception and it is of type Ex, execute func(Ex)
196 * @param func a function that takes a single parameter of type const Ex&
198 * @returns True if the Try held an Ex and func was executed, false otherwise
200 template <class Ex, class F>
201 bool withException(F func) const {
202 if (!hasException()) {
205 return e_->with_exception<Ex>(std::move(func));
208 template <bool isTry, typename R>
209 typename std::enable_if<isTry, R>::type get() {
210 return std::forward<R>(*this);
213 template <bool isTry, typename R>
214 typename std::enable_if<!isTry, R>::type get() {
215 return std::forward<R>(value());
222 std::unique_ptr<exception_wrapper> e_;
227 * Specialization of Try for void value type. Encapsulates either success or an
233 // Construct a Try holding a successful and void result
234 Try() : hasValue_(true) {}
237 * Construct a Try with an exception_wrapper
239 * @param e The exception_wrapper
241 explicit Try(exception_wrapper e)
243 e_(folly::make_unique<exception_wrapper>(std::move(e))) {}
247 * Construct a Try with an exception_pointer
249 * @param ep The exception_pointer. Will be rethrown.
251 explicit Try(std::exception_ptr ep) DEPRECATED : hasValue_(false) {
253 std::rethrow_exception(ep);
254 } catch (const std::exception& e) {
255 e_ = folly::make_unique<exception_wrapper>(std::current_exception(), e);
257 e_ = folly::make_unique<exception_wrapper>(std::current_exception());
262 Try& operator=(const Try<void>& t) {
263 hasValue_ = t.hasValue_;
265 e_ = folly::make_unique<exception_wrapper>(*t.e_);
270 Try(const Try<void>& t) {
274 // If the Try contains an exception, throws it
275 void value() const { throwIfFailed(); }
276 // Dereference operator. If the Try contains an exception, throws it
277 void operator*() const { return value(); }
279 // If the Try contains an exception, throws it
280 inline void throwIfFailed() const;
282 // @returns False if the Try contains an exception, true otherwise
283 bool hasValue() const { return hasValue_; }
284 // @returns True if the Try contains an exception, false otherwise
285 bool hasException() const { return !hasValue_; }
287 // @returns True if the Try contains an exception of type Ex, false otherwise
289 bool hasException() const {
290 return hasException() && e_->is_compatible_with<Ex>();
294 * @throws FutureException if the Try doesn't contain an exception
296 * @returns mutable reference to the exception contained by this Try
298 exception_wrapper& exception() {
299 if (UNLIKELY(!hasException())) {
300 throw FutureException("exception(): Try does not contain an exception");
305 const exception_wrapper& exception() const {
306 if (UNLIKELY(!hasException())) {
307 throw FutureException("exception(): Try does not contain an exception");
313 * If the Try contains an exception and it is of type Ex, execute func(Ex)
315 * @param func a function that takes a single parameter of type const Ex&
317 * @returns True if the Try held an Ex and func was executed, false otherwise
319 template <class Ex, class F>
320 bool withException(F func) const {
321 if (!hasException()) {
324 return e_->with_exception<Ex>(std::move(func));
327 template <bool, typename R>
329 return std::forward<R>(*this);
334 std::unique_ptr<exception_wrapper> e_{nullptr};
338 * Extracts value from try and returns it. Throws if try contained an exception.
340 * @param t Try to extract value from
342 * @returns value contained in t
344 template <typename T>
345 T moveFromTry(Try<T>&& t);
348 * Throws if try contained an exception.
350 * @param t Try to move from
352 void moveFromTry(Try<void>&& t);
355 * @param f a function to execute and capture the result of (value or exception)
357 * @returns Try holding the result of f
359 template <typename F>
360 typename std::enable_if<
361 !std::is_same<typename std::result_of<F()>::type, void>::value,
362 Try<typename std::result_of<F()>::type>>::type
363 makeTryFunction(F&& f);
366 * Specialization of makeTryFunction for void
368 * @param f a function to execute and capture the result of
370 * @returns Try<void> holding the result of f
372 template <typename F>
373 typename std::enable_if<
374 std::is_same<typename std::result_of<F()>::type, void>::value,
376 makeTryFunction(F&& f);
380 #include <folly/futures/Try-inl.h>