proxygen
folly::Try< T > Class Template Reference

#include <Try.h>

Public Types

typedef T element_type
 

Public Member Functions

 Try () noexcept
 
 Try (const T &v) noexcept(std::is_nothrow_copy_constructible< T >::value)
 
 Try (T &&v) noexcept(std::is_nothrow_move_constructible< T >::value)
 
template<typename... Args>
 Try (in_place_t, Args &&...args) noexcept(std::is_nothrow_constructible< T, Args &&... >::value)
 
template<class T2 = T>
 Try (typename std::enable_if< std::is_same< Unit, T2 >::value, Try< void > const & >::type t) noexcept
 Implicit conversion from Try<void> to Try<Unit> More...
 
 Try (exception_wrapper e) noexcept
 
 Try (Try< T > &&t) noexcept(std::is_nothrow_move_constructible< T >::value)
 
Tryoperator= (Try< T > &&t) noexcept(std::is_nothrow_move_constructible< T >::value)
 
 Try (const Try &t) noexcept(std::is_nothrow_copy_constructible< T >::value)
 
Tryoperator= (const Try &t) noexcept(std::is_nothrow_copy_constructible< T >::value)
 
 ~Try ()
 
template<typename... Args>
Templace (Args &&...args) noexcept(std::is_nothrow_constructible< T, Args &&... >::value)
 
template<typename... Args>
exception_wrapperemplaceException (Args &&...args) noexcept(std::is_nothrow_constructible< exception_wrapper, Args &&... >::value)
 
Tvalue ()&
 
T && value ()&&
 
const Tvalue () const &
 
const T && value () const &&
 
void throwIfFailed () const
 
const Toperator* () const &
 
Toperator* ()&
 
T && operator* ()&&
 
const T && operator* () const &&
 
const Toperator-> () const
 
Toperator-> ()
 
bool hasValue () const
 
bool hasException () const
 
template<class Ex >
bool hasException () const
 
exception_wrapperexception ()&
 
exception_wrapper && exception ()&&
 
const exception_wrapperexception () const &
 
const exception_wrapper && exception () const &&
 
std::exception * tryGetExceptionObject ()
 
std::exception const * tryGetExceptionObject () const
 
template<class E >
EtryGetExceptionObject ()
 
template<class E >
E const * tryGetExceptionObject () const
 
template<class Ex , class F >
bool withException (F func)
 
template<class Ex , class F >
bool withException (F func) const
 
template<class F >
bool withException (F func)
 
template<class F >
bool withException (F func) const
 
template<bool isTry, typename R >
std::enable_if< isTry, R >::type get ()
 
template<bool isTry, typename R >
std::enable_if<!isTry, R >::type get ()
 

Private Types

enum  Contains { Contains::VALUE, Contains::EXCEPTION, Contains::NOTHING }
 

Private Member Functions

void destroy () noexcept
 

Private Attributes

Contains contains_
 
union {
   T   value_
 
   exception_wrapper   e_
 
}; 
 

Detailed Description

template<class T>
class folly::Try< T >

Definition at line 51 of file Try.h.

Member Typedef Documentation

template<class T>
typedef T folly::Try< T >::element_type

Definition at line 66 of file Try.h.

Member Enumeration Documentation

template<class T>
enum folly::Try::Contains
strongprivate
Enumerator
VALUE 
EXCEPTION 
NOTHING 

Definition at line 56 of file Try.h.

56  {
57  VALUE,
58  EXCEPTION,
59  NOTHING,
60  };

Constructor & Destructor Documentation

template<class T>
folly::Try< T >::Try ( )
inlinenoexcept

Definition at line 71 of file Try.h.

template<class T>
folly::Try< T >::Try ( const T v)
inlineexplicitnoexcept

Definition at line 78 of file Try.h.

template<class T>
folly::Try< T >::Try ( T &&  v)
inlineexplicitnoexcept

Definition at line 87 of file Try.h.

Contains contains_
Definition: Try.h:371
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
T value_
Definition: Try.h:373
template<class T>
template<typename... Args>
folly::Try< T >::Try ( in_place_t  ,
Args &&...  args 
)
inlineexplicitnoexcept

Definition at line 91 of file Try.h.

93  : contains_(Contains::VALUE), value_(static_cast<Args&&>(args)...) {}
Contains contains_
Definition: Try.h:371
T value_
Definition: Try.h:373
template<class T >
template<class T2>
folly::Try< T >::Try ( typename std::enable_if< std::is_same< Unit, T2 >::value, Try< void > const & >::type  t)
noexcept

Implicit conversion from Try<void> to Try<Unit>

Definition at line 39 of file Try-inl.h.

References cpp.keywords::EXCEPTION, folly::pushmi::detail::t, and folly::T.

43  if (t.hasValue()) {
45  new (&value_) T();
46  } else if (t.hasException()) {
48  new (&e_) exception_wrapper(t.exception());
49  }
50 }
Contains contains_
Definition: Try.h:371
exception_wrapper e_
Definition: Try.h:374
folly::std T
T value_
Definition: Try.h:373
template<class T>
folly::Try< T >::Try ( exception_wrapper  e)
inlineexplicitnoexcept

Definition at line 106 of file Try.h.

Contains contains_
Definition: Try.h:371
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
template<class T>
folly::Try< T >::Try ( Try< T > &&  t)
noexcept

Definition at line 28 of file Try-inl.h.

References cpp.keywords::EXCEPTION, folly::gen::move, folly::pushmi::detail::t, and folly::T.

29  : contains_(t.contains_) {
30  if (contains_ == Contains::VALUE) {
31  new (&value_) T(std::move(t.value_));
32  } else if (contains_ == Contains::EXCEPTION) {
33  new (&e_) exception_wrapper(std::move(t.e_));
34  }
35 }
Contains contains_
Definition: Try.h:371
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
folly::std T
T value_
Definition: Try.h:373
template<class T>
folly::Try< T >::Try ( const Try< T > &  t)
noexcept

Definition at line 73 of file Try-inl.h.

References cpp.keywords::EXCEPTION, folly::pushmi::detail::t, folly::T, and value.

74  {
75  static_assert(
77  "T must be copyable for Try<T> to be copyable");
78  contains_ = t.contains_;
79  if (contains_ == Contains::VALUE) {
80  new (&value_) T(t.value_);
81  } else if (contains_ == Contains::EXCEPTION) {
82  new (&e_) exception_wrapper(t.e_);
83  }
84 }
Contains contains_
Definition: Try.h:371
exception_wrapper e_
Definition: Try.h:374
folly::std T
T value_
Definition: Try.h:373
static const char *const value
Definition: Conv.cpp:50
template<class T >
folly::Try< T >::~Try ( )

Definition at line 111 of file Try-inl.h.

References testing::Args(), cpp.keywords::EXCEPTION, LIKELY, and UNLIKELY.

111  {
112  if (LIKELY(contains_ == Contains::VALUE)) {
113  value_.~T();
114  } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
116  }
117 }
Contains contains_
Definition: Try.h:371
#define LIKELY(x)
Definition: Likely.h:47
exception_wrapper e_
Definition: Try.h:374
T value_
Definition: Try.h:373
#define UNLIKELY(x)
Definition: Likely.h:48

Member Function Documentation

template<class T >
void folly::Try< T >::destroy ( )
privatenoexcept

Definition at line 176 of file Try-inl.h.

References cpp.keywords::EXCEPTION, folly::exchange(), LIKELY, NOTHING, and UNLIKELY.

176  {
177  auto oldContains = folly::exchange(contains_, Contains::NOTHING);
178  if (LIKELY(oldContains == Contains::VALUE)) {
179  value_.~T();
180  } else if (UNLIKELY(oldContains == Contains::EXCEPTION)) {
182  }
183 }
Contains contains_
Definition: Try.h:371
#define LIKELY(x)
Definition: Likely.h:47
exception_wrapper e_
Definition: Try.h:374
T value_
Definition: Try.h:373
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
#define UNLIKELY(x)
Definition: Likely.h:48
template<typename T >
template<typename... Args>
T & folly::Try< T >::emplace ( Args &&...  args)
noexcept

Definition at line 121 of file Try-inl.h.

References testing::Args(), destroy(), and folly::T.

Referenced by TEST(), folly::tryEmplace(), and folly::tryEmplaceWith().

122  {
123  this->destroy();
124  new (&value_) T(static_cast<Args&&>(args)...);
126  return value_;
127 }
Contains contains_
Definition: Try.h:371
void destroy() noexcept
Definition: Try-inl.h:176
folly::std T
T value_
Definition: Try.h:373
template<typename T >
template<typename... Args>
exception_wrapper & folly::Try< T >::emplaceException ( Args &&...  args)
noexcept

Definition at line 131 of file Try-inl.h.

References destroy(), and cpp.keywords::EXCEPTION.

Referenced by folly::tryEmplace(), and folly::tryEmplaceWith().

132  {
133  this->destroy();
134  new (&e_) exception_wrapper(static_cast<Args&&>(args)...);
136  return e_;
137 }
Contains contains_
Definition: Try.h:371
void destroy() noexcept
Definition: Try-inl.h:176
exception_wrapper e_
Definition: Try.h:374
template<class T>
exception_wrapper& folly::Try< T >::exception ( )
inline

Definition at line 260 of file Try.h.

Referenced by folly::Future< T >::onTimeout(), and folly::futures::detail::retryingImpl().

260  {
261  if (!hasException()) {
262  throw_exception<TryException>("Try does not contain an exception");
263  }
264  return e_;
265  }
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
exception_wrapper&& folly::Try< T >::exception ( )
inline

Definition at line 267 of file Try.h.

267  {
268  if (!hasException()) {
269  throw_exception<TryException>("Try does not contain an exception");
270  }
271  return std::move(e_);
272  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
const exception_wrapper& folly::Try< T >::exception ( ) const
inline

Definition at line 274 of file Try.h.

274  {
275  if (!hasException()) {
276  throw_exception<TryException>("Try does not contain an exception");
277  }
278  return e_;
279  }
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
const exception_wrapper&& folly::Try< T >::exception ( ) const
inline

Definition at line 281 of file Try.h.

281  {
282  if (!hasException()) {
283  throw_exception<TryException>("Try does not contain an exception");
284  }
285  return std::move(e_);
286  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
template<bool isTry, typename R >
std::enable_if<isTry, R>::type folly::Try< T >::get ( )
inline

Definition at line 359 of file Try.h.

359  {
360  return std::forward<R>(*this);
361  }
template<class T>
template<bool isTry, typename R >
std::enable_if<!isTry, R>::type folly::Try< T >::get ( )
inline

Definition at line 364 of file Try.h.

364  {
365  return std::forward<R>(value());
366  }
T & value()&
Definition: Try-inl.h:140
template<class T>
bool folly::Try< T >::hasException ( ) const
inline
template<class T>
template<class Ex >
bool folly::Try< T >::hasException ( ) const
inline

Definition at line 256 of file Try.h.

256  {
257  return hasException() && e_.is_compatible_with<Ex>();
258  }
bool is_compatible_with() const noexcept
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
bool folly::Try< T >::hasValue ( ) const
inline

Definition at line 242 of file Try.h.

Referenced by folly::futures::detail::retryingImpl(), and TEST().

242  {
243  return contains_ == Contains::VALUE;
244  }
Contains contains_
Definition: Try.h:371
template<class T>
const T& folly::Try< T >::operator* ( ) const
inline

Definition at line 191 of file Try.h.

191  {
192  return value();
193  }
T & value()&
Definition: Try-inl.h:140
template<class T>
T& folly::Try< T >::operator* ( )
inline

Definition at line 199 of file Try.h.

199  {
200  return value();
201  }
T & value()&
Definition: Try-inl.h:140
template<class T>
T&& folly::Try< T >::operator* ( )
inline

Definition at line 208 of file Try.h.

208  {
209  return std::move(value());
210  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
T & value()&
Definition: Try-inl.h:140
template<class T>
const T&& folly::Try< T >::operator* ( ) const
inline

Definition at line 217 of file Try.h.

217  {
218  return std::move(value());
219  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
T & value()&
Definition: Try-inl.h:140
template<class T>
const T* folly::Try< T >::operator-> ( ) const
inline

Definition at line 227 of file Try.h.

227  {
228  return &value();
229  }
T & value()&
Definition: Try-inl.h:140
template<class T>
T* folly::Try< T >::operator-> ( )
inline

Definition at line 235 of file Try.h.

235  {
236  return &value();
237  }
T & value()&
Definition: Try-inl.h:140
template<class T>
Try< T > & folly::Try< T >::operator= ( Try< T > &&  t)
noexcept

Definition at line 53 of file Try-inl.h.

References destroy(), cpp.keywords::EXCEPTION, folly::gen::move, folly::pushmi::detail::t, and folly::T.

54  {
55  if (this == &t) {
56  return *this;
57  }
58 
59  destroy();
60 
61  if (t.contains_ == Contains::VALUE) {
62  new (&value_) T(std::move(t.value_));
63  } else if (t.contains_ == Contains::EXCEPTION) {
64  new (&e_) exception_wrapper(std::move(t.e_));
65  }
66 
67  contains_ = t.contains_;
68 
69  return *this;
70 }
Contains contains_
Definition: Try.h:371
void destroy() noexcept
Definition: Try-inl.h:176
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
folly::std T
T value_
Definition: Try.h:373
template<class T>
Try< T > & folly::Try< T >::operator= ( const Try< T > &  t)
noexcept

Definition at line 87 of file Try-inl.h.

References destroy(), cpp.keywords::EXCEPTION, folly::pushmi::detail::t, folly::T, and value.

88  {
89  static_assert(
91  "T must be copyable for Try<T> to be copyable");
92 
93  if (this == &t) {
94  return *this;
95  }
96 
97  destroy();
98 
99  if (t.contains_ == Contains::VALUE) {
100  new (&value_) T(t.value_);
101  } else if (t.contains_ == Contains::EXCEPTION) {
102  new (&e_) exception_wrapper(t.e_);
103  }
104 
105  contains_ = t.contains_;
106 
107  return *this;
108 }
Contains contains_
Definition: Try.h:371
void destroy() noexcept
Definition: Try-inl.h:176
exception_wrapper e_
Definition: Try.h:374
folly::std T
T value_
Definition: Try.h:373
static const char *const value
Definition: Conv.cpp:50
template<class T >
void folly::Try< T >::throwIfFailed ( ) const

Definition at line 164 of file Try-inl.h.

References cpp.keywords::EXCEPTION.

164  {
165  switch (contains_) {
166  case Contains::VALUE:
167  return;
168  case Contains::EXCEPTION:
170  default:
171  throw_exception<UsingUninitializedTry>();
172  }
173 }
Contains contains_
Definition: Try.h:371
exception_wrapper e_
Definition: Try.h:374
template<class T>
std::exception* folly::Try< T >::tryGetExceptionObject ( )
inline

Definition at line 292 of file Try.h.

Referenced by TEST().

292  {
293  return hasException() ? e_.get_exception() : nullptr;
294  }
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
std::exception * get_exception() noexcept
template<class T>
std::exception const* folly::Try< T >::tryGetExceptionObject ( ) const
inline

Definition at line 295 of file Try.h.

295  {
296  return hasException() ? e_.get_exception() : nullptr;
297  }
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
std::exception * get_exception() noexcept
template<class T>
template<class E >
E* folly::Try< T >::tryGetExceptionObject ( )
inline

Definition at line 305 of file Try.h.

305  {
306  return hasException() ? e_.get_exception<E>() : nullptr;
307  }
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
std::exception * get_exception() noexcept
template<class T>
template<class E >
E const* folly::Try< T >::tryGetExceptionObject ( ) const
inline

Definition at line 309 of file Try.h.

309  {
310  return hasException() ? e_.get_exception<E>() : nullptr;
311  }
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
std::exception * get_exception() noexcept
template<class T >
T && folly::Try< T >::value ( )
template<class T>
T&& folly::Try< T >::value ( )
template<class T >
const T && folly::Try< T >::value ( ) const

Definition at line 152 of file Try-inl.h.

References folly::gen::move, folly::T, and folly::Try< T >::value().

152  {
153  throwIfFailed();
154  return value_;
155 }
T value_
Definition: Try.h:373
void throwIfFailed() const
Definition: Try-inl.h:164
template<class T>
const T&& folly::Try< T >::value ( ) const
template<class T>
template<class Ex , class F >
bool folly::Try< T >::withException ( func)
inline

Definition at line 321 of file Try.h.

321  {
322  if (!hasException()) {
323  return false;
324  }
325  return e_.with_exception<Ex>(std::move(func));
326  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
template<class Ex , class F >
bool folly::Try< T >::withException ( func) const
inline

Definition at line 328 of file Try.h.

328  {
329  if (!hasException()) {
330  return false;
331  }
332  return e_.with_exception<Ex>(std::move(func));
333  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
template<class F >
bool folly::Try< T >::withException ( func)
inline

Definition at line 344 of file Try.h.

344  {
345  if (!hasException()) {
346  return false;
347  }
348  return e_.with_exception(std::move(func));
349  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248
template<class T>
template<class F >
bool folly::Try< T >::withException ( func) const
inline

Definition at line 351 of file Try.h.

351  {
352  if (!hasException()) {
353  return false;
354  }
355  return e_.with_exception(std::move(func));
356  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
exception_wrapper e_
Definition: Try.h:374
bool hasException() const
Definition: Try.h:248

Member Data Documentation

union { ... }
template<class T>
Contains folly::Try< T >::contains_
private

Definition at line 371 of file Try.h.

template<class T>
exception_wrapper folly::Try< T >::e_
template<class T>
T folly::Try< T >::value_

Definition at line 373 of file Try.h.


The documentation for this class was generated from the following files: