proxygen
folly::Optional< Value > Class Template Reference

#include <Optional.h>

Classes

struct  PrivateConstructor
 
struct  StorageNonTriviallyDestructible
 
struct  StorageTriviallyDestructible
 

Public Types

typedef Value value_type
 

Public Member Functions

FOLLY_CPP14_CONSTEXPR Optional () noexcept
 
 Optional (const Optional &src) noexcept(std::is_nothrow_copy_constructible< Value >::value)
 
 Optional (Optional &&src) noexcept(std::is_nothrow_move_constructible< Value >::value)
 
FOLLY_CPP14_CONSTEXPR Optional (const None &) noexcept
 
FOLLY_CPP14_CONSTEXPR Optional (Value &&newValue) noexcept(std::is_nothrow_move_constructible< Value >::value)
 
FOLLY_CPP14_CONSTEXPR Optional (const Value &newValue) noexcept(std::is_nothrow_copy_constructible< Value >::value)
 
template<typename Null = std::nullptr_t>
FOLLY_CPP14_CONSTEXPR Optional (typename std::enable_if< !std::is_pointer< Value >::value &&std::is_same< Null, std::nullptr_t >::value, Null >::type) noexcept
 
template<typename... Args>
FOLLY_CPP14_CONSTEXPR Optional (in_place_t, Args &&...args) noexcept(std::is_nothrow_constructible< Value, Args... >::value)
 
template<typename U , typename... Args>
FOLLY_CPP14_CONSTEXPR Optional (in_place_t, std::initializer_list< U > il, Args &&...args) noexcept(std::is_nothrow_constructible< Value, std::initializer_list< U >, Args... >::value)
 
 Optional (const detail::OptionalPromiseReturn< Value > &p)
 
void assign (const None &)
 
void assign (Optional &&src)
 
void assign (const Optional &src)
 
void assign (Value &&newValue)
 
void assign (const Value &newValue)
 
Optionaloperator= (None) noexcept
 
template<class Arg >
Optionaloperator= (Arg &&arg)
 
Optionaloperator= (Optional &&other) noexcept(std::is_nothrow_move_assignable< Value >::value)
 
Optionaloperator= (const Optional &other) noexcept(std::is_nothrow_copy_assignable< Value >::value)
 
template<class... Args>
Value & emplace (Args &&...args)
 
template<class U , class... Args>
std::enable_if< std::is_constructible< Value, std::initializer_list< U > &, Args &&... >::value, Value & >::type emplace (std::initializer_list< U > ilist, Args &&...args)
 
void reset () noexcept
 
void clear () noexcept
 
void swap (Optional &that) noexcept(IsNothrowSwappable< Value >::value)
 
FOLLY_CPP14_CONSTEXPR const Value & value () const &
 
FOLLY_CPP14_CONSTEXPR Value & value ()&
 
FOLLY_CPP14_CONSTEXPR Value && value ()&&
 
FOLLY_CPP14_CONSTEXPR const Value && value () const &&
 
const Value * get_pointer () const &
 
Value * get_pointer ()&
 
Value * get_pointer ()&&=delete
 
FOLLY_CPP14_CONSTEXPR bool has_value () const noexcept
 
FOLLY_CPP14_CONSTEXPR bool hasValue () const noexcept
 
FOLLY_CPP14_CONSTEXPR operator bool () const noexcept
 
FOLLY_CPP14_CONSTEXPR const Value & operator* () const &
 
FOLLY_CPP14_CONSTEXPR Value & operator* ()&
 
FOLLY_CPP14_CONSTEXPR const Value && operator* () const &&
 
FOLLY_CPP14_CONSTEXPR Value && operator* ()&&
 
FOLLY_CPP14_CONSTEXPR const Value * operator-> () const
 
FOLLY_CPP14_CONSTEXPR Value * operator-> ()
 
template<class U >
FOLLY_CPP14_CONSTEXPR Value value_or (U &&dflt) const &
 
template<class U >
FOLLY_CPP14_CONSTEXPR Value value_or (U &&dflt)&&
 

Private Types

using Storage = typename std::conditional< std::is_trivially_destructible< Value >::value, StorageTriviallyDestructible, StorageNonTriviallyDestructible >::type
 

Private Member Functions

template<typename... Args>
FOLLY_CPP14_CONSTEXPR Optional (PrivateConstructor, Args &&...args) noexcept(std::is_constructible< Value, Args &&... >::value)
 
void require_value () const
 
template<class... Args>
void construct (Args &&...args)
 

Private Attributes

Storage storage_
 

Friends

template<class T >
constexpr Optional< _t< std::decay< T > > > make_optional (T &&)
 
template<class T , class... Args>
constexpr Optional< Tmake_optional (Args &&...args)
 
template<class T , class U , class... As>
constexpr Optional< Tmake_optional (std::initializer_list< U >, As &&...)
 

Detailed Description

template<class Value>
class folly::Optional< Value >

Definition at line 71 of file Optional.h.

Member Typedef Documentation

template<class Value>
using folly::Optional< Value >::Storage = typename std::conditional< std::is_trivially_destructible<Value>::value, StorageTriviallyDestructible, StorageNonTriviallyDestructible>::type
private

Definition at line 429 of file Optional.h.

template<class Value>
typedef Value folly::Optional< Value >::value_type

Definition at line 98 of file Optional.h.

Constructor & Destructor Documentation

template<class Value>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( )
inlinenoexcept

Definition at line 107 of file Optional.h.

107 {}
template<class Value>
folly::Optional< Value >::Optional ( const Optional< Value > &  src)
inlinenoexcept

Definition at line 109 of file Optional.h.

110  {
111  if (src.hasValue()) {
112  construct(src.value());
113  }
114  }
void construct(Args &&...args)
Definition: Optional.h:379
template<class Value>
folly::Optional< Value >::Optional ( Optional< Value > &&  src)
inlinenoexcept

Definition at line 116 of file Optional.h.

117  {
118  if (src.hasValue()) {
119  construct(std::move(src.value()));
120  src.clear();
121  }
122  }
void construct(Args &&...args)
Definition: Optional.h:379
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class Value>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( const None )
inlinenoexcept

Definition at line 124 of file Optional.h.

124 {}
template<class Value>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( Value &&  newValue)
inlinenoexcept

Definition at line 126 of file Optional.h.

127  {
128  construct(std::move(newValue));
129  }
void construct(Args &&...args)
Definition: Optional.h:379
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class Value>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( const Value &  newValue)
inlinenoexcept

Definition at line 131 of file Optional.h.

132  {
133  construct(newValue);
134  }
void construct(Args &&...args)
Definition: Optional.h:379
template<class Value>
template<typename Null = std::nullptr_t>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( typename std::enable_if< !std::is_pointer< Value >::value &&std::is_same< Null, std::nullptr_t >::value, Null >::type  )
inlinenoexcept

DEPRECATED: use folly::none

Definition at line 141 of file Optional.h.

144  {}
template<class Value>
template<typename... Args>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( in_place_t  ,
Args &&...  args 
)
inlineexplicitnoexcept

Definition at line 147 of file Optional.h.

149  : Optional{PrivateConstructor{}, std::forward<Args>(args)...} {}
FOLLY_CPP14_CONSTEXPR Optional() noexcept
Definition: Optional.h:107
template<class Value>
template<typename U , typename... Args>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( in_place_t  ,
std::initializer_list< U >  il,
Args &&...  args 
)
inlineexplicitnoexcept

Definition at line 152 of file Optional.h.

160  : Optional{PrivateConstructor{}, il, std::forward<Args>(args)...} {}
FOLLY_CPP14_CONSTEXPR Optional() noexcept
Definition: Optional.h:107
template<class Value>
folly::Optional< Value >::Optional ( const detail::OptionalPromiseReturn< Value > &  p)
inline

Definition at line 163 of file Optional.h.

164  : Optional{} {
165  p.promise_->value_ = this;
166  }
FOLLY_CPP14_CONSTEXPR Optional() noexcept
Definition: Optional.h:107
template<class Value>
template<typename... Args>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::Optional ( PrivateConstructor  ,
Args &&...  args 
)
inlineprivatenoexcept

Definition at line 367 of file Optional.h.

368  {
369  construct(std::forward<Args>(args)...);
370  }
void construct(Args &&...args)
Definition: Optional.h:379

Member Function Documentation

template<class Value>
void folly::Optional< Value >::assign ( const None )
inline

Definition at line 168 of file Optional.h.

168  {
169  clear();
170  }
void clear() noexcept
Definition: Optional.h:251
template<class Value>
void folly::Optional< Value >::assign ( Optional< Value > &&  src)
inline

Definition at line 172 of file Optional.h.

172  {
173  if (this != &src) {
174  if (src.hasValue()) {
175  assign(std::move(src.value()));
176  src.clear();
177  } else {
178  clear();
179  }
180  }
181  }
void assign(const None &)
Definition: Optional.h:168
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void clear() noexcept
Definition: Optional.h:251
template<class Value>
void folly::Optional< Value >::assign ( const Optional< Value > &  src)
inline

Definition at line 183 of file Optional.h.

183  {
184  if (src.hasValue()) {
185  assign(src.value());
186  } else {
187  clear();
188  }
189  }
void assign(const None &)
Definition: Optional.h:168
void clear() noexcept
Definition: Optional.h:251
template<class Value>
void folly::Optional< Value >::assign ( Value &&  newValue)
inline

Definition at line 191 of file Optional.h.

191  {
192  if (hasValue()) {
193  storage_.value = std::move(newValue);
194  } else {
195  construct(std::move(newValue));
196  }
197  }
void construct(Args &&...args)
Definition: Optional.h:379
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
Storage storage_
Definition: Optional.h:431
template<class Value>
void folly::Optional< Value >::assign ( const Value &  newValue)
inline

Definition at line 199 of file Optional.h.

199  {
200  if (hasValue()) {
201  storage_.value = newValue;
202  } else {
203  construct(newValue);
204  }
205  }
void construct(Args &&...args)
Definition: Optional.h:379
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
Storage storage_
Definition: Optional.h:431
template<class Value>
template<class... Args>
void folly::Optional< Value >::construct ( Args &&...  args)
inlineprivate

Definition at line 379 of file Optional.h.

379  {
380  const void* ptr = &storage_.value;
381  // For supporting const types.
382  new (const_cast<void*>(ptr)) Value(std::forward<Args>(args)...);
383  storage_.hasValue = true;
384  }
void * ptr
bool Value(const T &value, M matcher)
Storage storage_
Definition: Optional.h:431
template<class Value>
template<class... Args>
Value& folly::Optional< Value >::emplace ( Args &&...  args)
inline
template<class Value>
template<class U , class... Args>
std::enable_if< std::is_constructible<Value, std::initializer_list<U>&, Args&&...>::value, Value&>::type folly::Optional< Value >::emplace ( std::initializer_list< U >  ilist,
Args &&...  args 
)
inline

Definition at line 241 of file Optional.h.

241  {
242  clear();
243  construct(ilist, std::forward<Args>(args)...);
244  return value();
245  }
void construct(Args &&...args)
Definition: Optional.h:379
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
void clear() noexcept
Definition: Optional.h:251
template<class Value>
const Value* folly::Optional< Value >::get_pointer ( ) const
inline
template<class Value>
Value* folly::Optional< Value >::get_pointer ( )
inline

Definition at line 291 of file Optional.h.

291  {
292  return storage_.hasValue ? &storage_.value : nullptr;
293  }
Storage storage_
Definition: Optional.h:431
template<class Value>
Value* folly::Optional< Value >::get_pointer ( )
delete
template<class Value>
FOLLY_CPP14_CONSTEXPR bool folly::Optional< Value >::has_value ( ) const
inlinenoexcept
template<class Value>
FOLLY_CPP14_CONSTEXPR bool folly::Optional< Value >::hasValue ( ) const
inlinenoexcept
template<class Value>
FOLLY_CPP14_CONSTEXPR folly::Optional< Value >::operator bool ( ) const
inlineexplicitnoexcept

Definition at line 304 of file Optional.h.

304  {
305  return has_value();
306  }
FOLLY_CPP14_CONSTEXPR bool has_value() const noexcept
Definition: Optional.h:296
template<class Value>
FOLLY_CPP14_CONSTEXPR const Value& folly::Optional< Value >::operator* ( ) const
inline

Definition at line 308 of file Optional.h.

308  {
309  return value();
310  }
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
template<class Value>
FOLLY_CPP14_CONSTEXPR Value& folly::Optional< Value >::operator* ( )
inline

Definition at line 311 of file Optional.h.

311  {
312  return value();
313  }
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
template<class Value>
FOLLY_CPP14_CONSTEXPR const Value&& folly::Optional< Value >::operator* ( ) const
inline

Definition at line 314 of file Optional.h.

314  {
315  return std::move(value());
316  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
template<class Value>
FOLLY_CPP14_CONSTEXPR Value&& folly::Optional< Value >::operator* ( )
inline

Definition at line 317 of file Optional.h.

317  {
318  return std::move(value());
319  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
template<class Value>
FOLLY_CPP14_CONSTEXPR const Value* folly::Optional< Value >::operator-> ( ) const
inline

Definition at line 321 of file Optional.h.

321  {
322  return &value();
323  }
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
template<class Value>
FOLLY_CPP14_CONSTEXPR Value* folly::Optional< Value >::operator-> ( )
inline

Definition at line 324 of file Optional.h.

324  {
325  return &value();
326  }
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
template<class Value>
Optional& folly::Optional< Value >::operator= ( None  )
inlinenoexcept

Definition at line 207 of file Optional.h.

207  {
208  reset();
209  return *this;
210  }
void reset() noexcept
Definition: Optional.h:247
template<class Value>
template<class Arg >
Optional& folly::Optional< Value >::operator= ( Arg &&  arg)
inline

Definition at line 213 of file Optional.h.

213  {
214  assign(std::forward<Arg>(arg));
215  return *this;
216  }
void assign(const None &)
Definition: Optional.h:168
template<class Value>
Optional& folly::Optional< Value >::operator= ( Optional< Value > &&  other)
inlinenoexcept

Definition at line 218 of file Optional.h.

219  {
220  assign(std::move(other));
221  return *this;
222  }
void assign(const None &)
Definition: Optional.h:168
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class Value>
Optional& folly::Optional< Value >::operator= ( const Optional< Value > &  other)
inlinenoexcept

Definition at line 224 of file Optional.h.

225  {
226  assign(other);
227  return *this;
228  }
void assign(const None &)
Definition: Optional.h:168
template<class Value>
void folly::Optional< Value >::require_value ( ) const
inlineprivate

Definition at line 372 of file Optional.h.

372  {
373  if (!storage_.hasValue) {
374  throw_exception<OptionalEmptyException>();
375  }
376  }
Storage storage_
Definition: Optional.h:431
template<class Value>
void folly::Optional< Value >::reset ( )
inlinenoexcept

Definition at line 247 of file Optional.h.

Referenced by folly::TEST().

247  {
248  storage_.clear();
249  }
Storage storage_
Definition: Optional.h:431
template<class Value>
void folly::Optional< Value >::swap ( Optional< Value > &  that)
inlinenoexcept

Definition at line 255 of file Optional.h.

255  {
256  if (hasValue() && that.hasValue()) {
257  using std::swap;
258  swap(value(), that.value());
259  } else if (hasValue()) {
260  that.emplace(std::move(value()));
261  reset();
262  } else if (that.hasValue()) {
263  emplace(std::move(that.value()));
264  that.reset();
265  }
266  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void swap(Optional &that) noexcept(IsNothrowSwappable< Value >::value)
Definition: Optional.h:255
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
Value & emplace(Args &&...args)
Definition: Optional.h:231
void reset() noexcept
Definition: Optional.h:247
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
void swap(Optional< T > &a, Optional< T > &b) noexcept(noexcept(a.swap(b)))
Definition: Optional.h:445
template<class Value>
FOLLY_CPP14_CONSTEXPR Value& folly::Optional< Value >::value ( )
inline

Definition at line 273 of file Optional.h.

273  {
274  require_value();
275  return storage_.value;
276  }
void require_value() const
Definition: Optional.h:372
Storage storage_
Definition: Optional.h:431
template<class Value>
FOLLY_CPP14_CONSTEXPR Value&& folly::Optional< Value >::value ( )
inline

Definition at line 278 of file Optional.h.

278  {
279  require_value();
280  return std::move(storage_.value);
281  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void require_value() const
Definition: Optional.h:372
Storage storage_
Definition: Optional.h:431
template<class Value>
FOLLY_CPP14_CONSTEXPR const Value&& folly::Optional< Value >::value ( ) const
inline

Definition at line 283 of file Optional.h.

283  {
284  require_value();
285  return std::move(storage_.value);
286  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void require_value() const
Definition: Optional.h:372
Storage storage_
Definition: Optional.h:431
template<class Value>
template<class U >
FOLLY_CPP14_CONSTEXPR Value folly::Optional< Value >::value_or ( U &&  dflt) const
inline
template<class Value>
template<class U >
FOLLY_CPP14_CONSTEXPR Value folly::Optional< Value >::value_or ( U &&  dflt)
inline

Definition at line 339 of file Optional.h.

339  {
340  if (storage_.hasValue) {
341  return std::move(storage_.value);
342  }
343 
344  return std::forward<U>(dflt);
345  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Storage storage_
Definition: Optional.h:431

Friends And Related Function Documentation

template<class Value>
template<class T >
constexpr Optional<_t<std::decay<T> > > make_optional ( T &&  v)
friend

Definition at line 450 of file Optional.h.

450  {
451  using PrivateConstructor =
452  typename folly::Optional<_t<std::decay<T>>>::PrivateConstructor;
453  return {PrivateConstructor{}, std::forward<T>(v)};
454 }
auto v
template<class Value>
template<class T , class... Args>
constexpr Optional<T> make_optional ( Args &&...  args)
friend

Definition at line 457 of file Optional.h.

457  {
458  using PrivateConstructor = typename folly::Optional<T>::PrivateConstructor;
459  return {PrivateConstructor{}, std::forward<Args>(args)...};
460 }
template<class Value>
template<class T , class U , class... As>
constexpr Optional<T> make_optional ( std::initializer_list< U >  ,
As &&  ... 
)
friend

Member Data Documentation

template<class Value>
Storage folly::Optional< Value >::storage_
private

Definition at line 431 of file Optional.h.


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