V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
optional.h
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This file is a clone of "base/optional.h" in chromium.
6 // Keep in sync, especially when fixing bugs.
7 // Copyright 2017 the V8 project authors. All rights reserved.
8 
9 #ifndef V8_BASE_OPTIONAL_H_
10 #define V8_BASE_OPTIONAL_H_
11 
12 #include <type_traits>
13 #include <utility>
14 
15 #include "src/base/logging.h"
16 
17 namespace v8 {
18 namespace base {
19 
20 // Specification:
21 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t
22 struct in_place_t {};
23 
24 // Specification:
25 // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t
26 struct nullopt_t {
27  constexpr explicit nullopt_t(int) {}
28 };
29 
30 // Specification:
31 // http://en.cppreference.com/w/cpp/utility/optional/in_place
32 constexpr in_place_t in_place = {};
33 
34 // Specification:
35 // http://en.cppreference.com/w/cpp/utility/optional/nullopt
36 constexpr nullopt_t nullopt(0);
37 
38 // Forward declaration, which is refered by following helpers.
39 template <typename T>
40 class Optional;
41 
42 namespace internal {
43 
44 template <typename T, bool = std::is_trivially_destructible<T>::value>
46  // Initializing |empty_| here instead of using default member initializing
47  // to avoid errors in g++ 4.8.
48  constexpr OptionalStorageBase() : empty_('\0') {}
49 
50  template <class... Args>
51  constexpr explicit OptionalStorageBase(in_place_t, Args&&... args)
52  : is_populated_(true), value_(std::forward<Args>(args)...) {}
53 
54  // When T is not trivially destructible we must call its
55  // destructor before deallocating its memory.
56  // Note that this hides the (implicitly declared) move constructor, which
57  // would be used for constexpr move constructor in OptionalStorage<T>.
58  // It is needed iff T is trivially move constructible. However, the current
59  // is_trivially_{copy,move}_constructible implementation requires
60  // is_trivially_destructible (which looks a bug, cf:
61  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 and
62  // http://cplusplus.github.io/LWG/lwg-active.html#2116), so it is not
63  // necessary for this case at the moment. Please see also the destructor
64  // comment in "is_trivially_destructible = true" specialization below.
66  if (is_populated_) value_.~T();
67  }
68 
69  template <class... Args>
70  void Init(Args&&... args) {
71  DCHECK(!is_populated_);
72  ::new (&value_) T(std::forward<Args>(args)...);
73  is_populated_ = true;
74  }
75 
76  bool is_populated_ = false;
77  union {
78  // |empty_| exists so that the union will always be initialized, even when
79  // it doesn't contain a value. Union members must be initialized for the
80  // constructor to be 'constexpr'.
81  char empty_;
82  T value_;
83  };
84 };
85 
86 template <typename T>
87 struct OptionalStorageBase<T, true /* trivially destructible */> {
88  // Initializing |empty_| here instead of using default member initializing
89  // to avoid errors in g++ 4.8.
90  constexpr OptionalStorageBase() : empty_('\0') {}
91 
92  template <class... Args>
93  constexpr explicit OptionalStorageBase(in_place_t, Args&&... args)
94  : is_populated_(true), value_(std::forward<Args>(args)...) {}
95 
96  // When T is trivially destructible (i.e. its destructor does nothing) there
97  // is no need to call it. Implicitly defined destructor is trivial, because
98  // both members (bool and union containing only variants which are trivially
99  // destructible) are trivially destructible.
100  // Explicitly-defaulted destructor is also trivial, but do not use it here,
101  // because it hides the implicit move constructor. It is needed to implement
102  // constexpr move constructor in OptionalStorage iff T is trivially move
103  // constructible. Note that, if T is trivially move constructible, the move
104  // constructor of OptionalStorageBase<T> is also implicitly defined and it is
105  // trivially move constructor. If T is not trivially move constructible,
106  // "not declaring move constructor without destructor declaration" here means
107  // "delete move constructor", which works because any move constructor of
108  // OptionalStorage will not refer to it in that case.
109 
110  template <class... Args>
111  void Init(Args&&... args) {
112  DCHECK(!is_populated_);
113  ::new (&value_) T(std::forward<Args>(args)...);
114  is_populated_ = true;
115  }
116 
117  bool is_populated_ = false;
118  union {
119  // |empty_| exists so that the union will always be initialized, even when
120  // it doesn't contain a value. Union members must be initialized for the
121  // constructor to be 'constexpr'.
122  char empty_;
123  T value_;
124  };
125 };
126 
127 // Implement conditional constexpr copy and move constructors. These are
128 // constexpr if is_trivially_{copy,move}_constructible<T>::value is true
129 // respectively. If each is true, the corresponding constructor is defined as
130 // "= default;", which generates a constexpr constructor (In this case,
131 // the condition of constexpr-ness is satisfied because the base class also has
132 // compiler generated constexpr {copy,move} constructors). Note that
133 // placement-new is prohibited in constexpr.
134 #if defined(__GNUC__) && __GNUC__ < 5
135 // gcc <5 does not implement std::is_trivially_copy_constructible.
136 // Conservatively assume false for this configuration.
137 // TODO(clemensh): Remove this once we drop support for gcc <5.
138 #define TRIVIALLY_COPY_CONSTRUCTIBLE(T) false
139 #define TRIVIALLY_MOVE_CONSTRUCTIBLE(T) false
140 #else
141 #define TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
142  std::is_trivially_copy_constructible<T>::value
143 #define TRIVIALLY_MOVE_CONSTRUCTIBLE(T) \
144  std::is_trivially_move_constructible<T>::value
145 #endif
146 template <typename T, bool = TRIVIALLY_COPY_CONSTRUCTIBLE(T),
147  bool = TRIVIALLY_MOVE_CONSTRUCTIBLE(T)>
148 #undef TRIVIALLY_COPY_CONSTRUCTIBLE
150  // This is no trivially {copy,move} constructible case. Other cases are
151  // defined below as specializations.
152 
153  // Accessing the members of template base class requires explicit
154  // declaration.
158 
159  // Inherit constructors (specifically, the in_place constructor).
161 
162  // User defined constructor deletes the default constructor.
163  // Define it explicitly.
164  OptionalStorage() = default;
165 
166  OptionalStorage(const OptionalStorage& other) {
167  if (other.is_populated_) Init(other.value_);
168  }
169 
170  OptionalStorage(OptionalStorage&& other) noexcept(
171  std::is_nothrow_move_constructible<T>::value) {
172  if (other.is_populated_) Init(std::move(other.value_));
173  }
174 };
175 
176 template <typename T>
177 struct OptionalStorage<T, true /* trivially copy constructible */,
178  false /* trivially move constructible */>
179  : OptionalStorageBase<T> {
184 
185  OptionalStorage() = default;
186  OptionalStorage(const OptionalStorage& other) = default;
187 
188  OptionalStorage(OptionalStorage&& other) noexcept(
189  std::is_nothrow_move_constructible<T>::value) {
190  if (other.is_populated_) Init(std::move(other.value_));
191  }
192 };
193 
194 template <typename T>
195 struct OptionalStorage<T, false /* trivially copy constructible */,
196  true /* trivially move constructible */>
197  : OptionalStorageBase<T> {
202 
203  OptionalStorage() = default;
204  OptionalStorage(OptionalStorage&& other) = default;
205 
206  OptionalStorage(const OptionalStorage& other) {
207  if (other.is_populated_) Init(other.value_);
208  }
209 };
210 
211 template <typename T>
212 struct OptionalStorage<T, true /* trivially copy constructible */,
213  true /* trivially move constructible */>
214  : OptionalStorageBase<T> {
215  // If both trivially {copy,move} constructible are true, it is not necessary
216  // to use user-defined constructors. So, just inheriting constructors
217  // from the base class works.
219 };
220 
221 // Base class to support conditionally usable copy-/move- constructors
222 // and assign operators.
223 template <typename T>
225  // This class provides implementation rather than public API, so everything
226  // should be hidden. Often we use composition, but we cannot in this case
227  // because of C++ language restriction.
228  protected:
229  constexpr OptionalBase() = default;
230  constexpr OptionalBase(const OptionalBase& other) = default;
231  constexpr OptionalBase(OptionalBase&& other) = default;
232 
233  template <class... Args>
234  constexpr explicit OptionalBase(in_place_t, Args&&... args)
235  : storage_(in_place, std::forward<Args>(args)...) {}
236 
237  // Implementation of converting constructors.
238  template <typename U>
239  explicit OptionalBase(const OptionalBase<U>& other) {
240  if (other.storage_.is_populated_) storage_.Init(other.storage_.value_);
241  }
242 
243  template <typename U>
244  explicit OptionalBase(OptionalBase<U>&& other) {
245  if (other.storage_.is_populated_)
246  storage_.Init(std::move(other.storage_.value_));
247  }
248 
249  ~OptionalBase() = default;
250 
251  OptionalBase& operator=(const OptionalBase& other) {
252  CopyAssign(other);
253  return *this;
254  }
255 
256  OptionalBase& operator=(OptionalBase&& other) noexcept(
257  std::is_nothrow_move_assignable<T>::value&&
258  std::is_nothrow_move_constructible<T>::value) {
259  MoveAssign(std::move(other));
260  return *this;
261  }
262 
263  template <typename U>
264  void CopyAssign(const OptionalBase<U>& other) {
265  if (other.storage_.is_populated_)
266  InitOrAssign(other.storage_.value_);
267  else
268  FreeIfNeeded();
269  }
270 
271  template <typename U>
272  void MoveAssign(OptionalBase<U>&& other) {
273  if (other.storage_.is_populated_)
274  InitOrAssign(std::move(other.storage_.value_));
275  else
276  FreeIfNeeded();
277  }
278 
279  template <typename U>
280  void InitOrAssign(U&& value) {
281  if (storage_.is_populated_)
282  storage_.value_ = std::forward<U>(value);
283  else
284  storage_.Init(std::forward<U>(value));
285  }
286 
287  void FreeIfNeeded() {
288  if (!storage_.is_populated_) return;
289  storage_.value_.~T();
290  storage_.is_populated_ = false;
291  }
292 
293  // For implementing conversion, allow access to other typed OptionalBase
294  // class.
295  template <typename U>
296  friend class OptionalBase;
297 
298  OptionalStorage<T> storage_;
299 };
300 
301 // The following {Copy,Move}{Constructible,Assignable} structs are helpers to
302 // implement constructor/assign-operator overloading. Specifically, if T is
303 // is not movable but copyable, Optional<T>'s move constructor should not
304 // participate in overload resolution. This inheritance trick implements that.
305 template <bool is_copy_constructible>
307 
308 template <>
309 struct CopyConstructible<false> {
310  constexpr CopyConstructible() = default;
311  constexpr CopyConstructible(const CopyConstructible&) = delete;
312  constexpr CopyConstructible(CopyConstructible&&) = default;
313  CopyConstructible& operator=(const CopyConstructible&) = default;
314  CopyConstructible& operator=(CopyConstructible&&) = default;
315 };
316 
317 template <bool is_move_constructible>
319 
320 template <>
321 struct MoveConstructible<false> {
322  constexpr MoveConstructible() = default;
323  constexpr MoveConstructible(const MoveConstructible&) = default;
324  constexpr MoveConstructible(MoveConstructible&&) = delete;
325  MoveConstructible& operator=(const MoveConstructible&) = default;
326  MoveConstructible& operator=(MoveConstructible&&) = default;
327 };
328 
329 template <bool is_copy_assignable>
330 struct CopyAssignable {};
331 
332 template <>
333 struct CopyAssignable<false> {
334  constexpr CopyAssignable() = default;
335  constexpr CopyAssignable(const CopyAssignable&) = default;
336  constexpr CopyAssignable(CopyAssignable&&) = default;
337  CopyAssignable& operator=(const CopyAssignable&) = delete;
338  CopyAssignable& operator=(CopyAssignable&&) = default;
339 };
340 
341 template <bool is_move_assignable>
342 struct MoveAssignable {};
343 
344 template <>
345 struct MoveAssignable<false> {
346  constexpr MoveAssignable() = default;
347  constexpr MoveAssignable(const MoveAssignable&) = default;
348  constexpr MoveAssignable(MoveAssignable&&) = default;
349  MoveAssignable& operator=(const MoveAssignable&) = default;
350  MoveAssignable& operator=(MoveAssignable&&) = delete;
351 };
352 
353 // Helper to conditionally enable converting constructors and assign operators.
354 template <typename T, typename U>
356  : std::integral_constant<
357  bool, std::is_constructible<T, Optional<U>&>::value ||
358  std::is_constructible<T, const Optional<U>&>::value ||
359  std::is_constructible<T, Optional<U>&&>::value ||
360  std::is_constructible<T, const Optional<U>&&>::value ||
361  std::is_convertible<Optional<U>&, T>::value ||
362  std::is_convertible<const Optional<U>&, T>::value ||
363  std::is_convertible<Optional<U>&&, T>::value ||
364  std::is_convertible<const Optional<U>&&, T>::value> {};
365 
366 template <typename T, typename U>
368  : std::integral_constant<
369  bool, IsConvertibleFromOptional<T, U>::value ||
370  std::is_assignable<T&, Optional<U>&>::value ||
371  std::is_assignable<T&, const Optional<U>&>::value ||
372  std::is_assignable<T&, Optional<U>&&>::value ||
373  std::is_assignable<T&, const Optional<U>&&>::value> {};
374 
375 // Forward compatibility for C++17.
376 // Introduce one more deeper nested namespace to avoid leaking using std::swap.
377 namespace swappable_impl {
378 using std::swap;
379 
381  // Tests if swap can be called. Check<T&>(0) returns true_type iff swap
382  // is available for T. Otherwise, Check's overload resolution falls back
383  // to Check(...) declared below thanks to SFINAE, so returns false_type.
384  template <typename T>
385  static auto Check(int i)
386  -> decltype(swap(std::declval<T>(), std::declval<T>()), std::true_type());
387 
388  template <typename T>
389  static std::false_type Check(...);
390 };
391 } // namespace swappable_impl
392 
393 template <typename T>
394 struct IsSwappable : decltype(swappable_impl::IsSwappableImpl::Check<T&>(0)) {};
395 
396 // Forward compatibility for C++20.
397 template <typename T>
398 using RemoveCvRefT =
399  typename std::remove_cv<typename std::remove_reference<T>::type>::type;
400 
401 } // namespace internal
402 
403 // On Windows, by default, empty-base class optimization does not work,
404 // which means even if the base class is empty struct, it still consumes one
405 // byte for its body. __declspec(empty_bases) enables the optimization.
406 // cf)
407 // https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/
408 #ifdef OS_WIN
409 #define OPTIONAL_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
410 #else
411 #define OPTIONAL_DECLSPEC_EMPTY_BASES
412 #endif
413 
414 // base::Optional is a Chromium version of the C++17 optional class:
415 // std::optional documentation:
416 // http://en.cppreference.com/w/cpp/utility/optional
417 // Chromium documentation:
418 // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md
419 //
420 // These are the differences between the specification and the implementation:
421 // - Constructors do not use 'constexpr' as it is a C++14 extension.
422 // - 'constexpr' might be missing in some places for reasons specified locally.
423 // - No exceptions are thrown, because they are banned from Chromium.
424 // Marked noexcept for only move constructor and move assign operators.
425 // - All the non-members are in the 'base' namespace instead of 'std'.
426 //
427 // Note that T cannot have a constructor T(Optional<T>) etc. Optional<T> checks
428 // T's constructor (specifically via IsConvertibleFromOptional), and in the
429 // check whether T can be constructible from Optional<T>, which is recursive
430 // so it does not work. As of Feb 2018, std::optional C++17 implementation in
431 // both clang and gcc has same limitation. MSVC SFINAE looks to have different
432 // behavior, but anyway it reports an error, too.
433 template <typename T>
434 class OPTIONAL_DECLSPEC_EMPTY_BASES Optional
435  : public internal::OptionalBase<T>,
436  public internal::CopyConstructible<std::is_copy_constructible<T>::value>,
437  public internal::MoveConstructible<std::is_move_constructible<T>::value>,
438  public internal::CopyAssignable<std::is_copy_constructible<T>::value &&
439  std::is_copy_assignable<T>::value>,
440  public internal::MoveAssignable<std::is_move_constructible<T>::value &&
441  std::is_move_assignable<T>::value> {
442  public:
443 #undef OPTIONAL_DECLSPEC_EMPTY_BASES
444  using value_type = T;
445 
446  // Defer default/copy/move constructor implementation to OptionalBase.
447  constexpr Optional() = default;
448  constexpr Optional(const Optional& other) = default;
449  constexpr Optional(Optional&& other) = default;
450 
451  constexpr Optional(nullopt_t) {} // NOLINT(runtime/explicit)
452 
453  // Converting copy constructor. "explicit" only if
454  // std::is_convertible<const U&, T>::value is false. It is implemented by
455  // declaring two almost same constructors, but that condition in enable_if
456  // is different, so that either one is chosen, thanks to SFINAE.
457  template <typename U,
458  typename std::enable_if<
459  std::is_constructible<T, const U&>::value &&
460  !internal::IsConvertibleFromOptional<T, U>::value &&
461  std::is_convertible<const U&, T>::value,
462  bool>::type = false>
463  Optional(const Optional<U>& other) : internal::OptionalBase<T>(other) {}
464 
465  template <typename U,
466  typename std::enable_if<
467  std::is_constructible<T, const U&>::value &&
468  !internal::IsConvertibleFromOptional<T, U>::value &&
469  !std::is_convertible<const U&, T>::value,
470  bool>::type = false>
471  explicit Optional(const Optional<U>& other)
472  : internal::OptionalBase<T>(other) {}
473 
474  // Converting move constructor. Similar to converting copy constructor,
475  // declaring two (explicit and non-explicit) constructors.
476  template <typename U,
477  typename std::enable_if<
478  std::is_constructible<T, U&&>::value &&
479  !internal::IsConvertibleFromOptional<T, U>::value &&
480  std::is_convertible<U&&, T>::value,
481  bool>::type = false>
482  Optional(Optional<U>&& other) : internal::OptionalBase<T>(std::move(other)) {}
483 
484  template <typename U,
485  typename std::enable_if<
486  std::is_constructible<T, U&&>::value &&
487  !internal::IsConvertibleFromOptional<T, U>::value &&
488  !std::is_convertible<U&&, T>::value,
489  bool>::type = false>
490  explicit Optional(Optional<U>&& other)
491  : internal::OptionalBase<T>(std::move(other)) {}
492 
493  template <class... Args>
494  constexpr explicit Optional(in_place_t, Args&&... args)
495  : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {}
496 
497  template <class U, class... Args,
498  class = typename std::enable_if<std::is_constructible<
499  value_type, std::initializer_list<U>&, Args...>::value>::type>
500  constexpr explicit Optional(in_place_t, std::initializer_list<U> il,
501  Args&&... args)
502  : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {}
503 
504  // Forward value constructor. Similar to converting constructors,
505  // conditionally explicit.
506  template <
507  typename U = value_type,
508  typename std::enable_if<
509  std::is_constructible<T, U&&>::value &&
510  !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value &&
511  !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
512  std::is_convertible<U&&, T>::value,
513  bool>::type = false>
514  constexpr Optional(U&& value) // NOLINT(runtime/explicit)
515  : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
516 
517  template <
518  typename U = value_type,
519  typename std::enable_if<
520  std::is_constructible<T, U&&>::value &&
521  !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value &&
522  !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
523  !std::is_convertible<U&&, T>::value,
524  bool>::type = false>
525  constexpr explicit Optional(U&& value)
526  : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
527 
528  ~Optional() = default;
529 
530  // Defer copy-/move- assign operator implementation to OptionalBase.
531  Optional& operator=(const Optional& other) = default;
532  Optional& operator=(Optional&& other) = default;
533 
534  Optional& operator=(nullopt_t) {
535  FreeIfNeeded();
536  return *this;
537  }
538 
539  // Perfect-forwarded assignment.
540  template <typename U>
541  typename std::enable_if<
542  !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
543  std::is_constructible<T, U>::value &&
544  std::is_assignable<T&, U>::value &&
545  (!std::is_scalar<T>::value ||
546  !std::is_same<typename std::decay<U>::type, T>::value),
547  Optional&>::type
548  operator=(U&& value) {
549  InitOrAssign(std::forward<U>(value));
550  return *this;
551  }
552 
553  // Copy assign the state of other.
554  template <typename U>
555  typename std::enable_if<!internal::IsAssignableFromOptional<T, U>::value &&
556  std::is_constructible<T, const U&>::value &&
557  std::is_assignable<T&, const U&>::value,
558  Optional&>::type
559  operator=(const Optional<U>& other) {
560  CopyAssign(other);
561  return *this;
562  }
563 
564  // Move assign the state of other.
565  template <typename U>
566  typename std::enable_if<!internal::IsAssignableFromOptional<T, U>::value &&
567  std::is_constructible<T, U>::value &&
568  std::is_assignable<T&, U>::value,
569  Optional&>::type
570  operator=(Optional<U>&& other) {
571  MoveAssign(std::move(other));
572  return *this;
573  }
574 
575  const T* operator->() const {
576  DCHECK(storage_.is_populated_);
577  return &storage_.value_;
578  }
579 
580  T* operator->() {
581  DCHECK(storage_.is_populated_);
582  return &storage_.value_;
583  }
584 
585  const T& operator*() const & {
586  DCHECK(storage_.is_populated_);
587  return storage_.value_;
588  }
589 
590  T& operator*() & {
591  DCHECK(storage_.is_populated_);
592  return storage_.value_;
593  }
594 
595  const T&& operator*() const && {
596  DCHECK(storage_.is_populated_);
597  return std::move(storage_.value_);
598  }
599 
600  T&& operator*() && {
601  DCHECK(storage_.is_populated_);
602  return std::move(storage_.value_);
603  }
604 
605  constexpr explicit operator bool() const { return storage_.is_populated_; }
606 
607  constexpr bool has_value() const { return storage_.is_populated_; }
608 
609  T& value() & {
610  CHECK(storage_.is_populated_);
611  return storage_.value_;
612  }
613 
614  const T& value() const & {
615  CHECK(storage_.is_populated_);
616  return storage_.value_;
617  }
618 
619  T&& value() && {
620  CHECK(storage_.is_populated_);
621  return std::move(storage_.value_);
622  }
623 
624  const T&& value() const && {
625  CHECK(storage_.is_populated_);
626  return std::move(storage_.value_);
627  }
628 
629  template <class U>
630  constexpr T value_or(U&& default_value) const & {
631  // TODO(mlamouri): add the following assert when possible:
632  // static_assert(std::is_copy_constructible<T>::value,
633  // "T must be copy constructible");
634  static_assert(std::is_convertible<U, T>::value,
635  "U must be convertible to T");
636  return storage_.is_populated_
637  ? storage_.value_
638  : static_cast<T>(std::forward<U>(default_value));
639  }
640 
641  template <class U>
642  T value_or(U&& default_value) && {
643  // TODO(mlamouri): add the following assert when possible:
644  // static_assert(std::is_move_constructible<T>::value,
645  // "T must be move constructible");
646  static_assert(std::is_convertible<U, T>::value,
647  "U must be convertible to T");
648  return storage_.is_populated_
649  ? std::move(storage_.value_)
650  : static_cast<T>(std::forward<U>(default_value));
651  }
652 
653  void swap(Optional& other) {
654  if (!storage_.is_populated_ && !other.storage_.is_populated_) return;
655 
656  if (storage_.is_populated_ != other.storage_.is_populated_) {
657  if (storage_.is_populated_) {
658  other.storage_.Init(std::move(storage_.value_));
659  FreeIfNeeded();
660  } else {
661  storage_.Init(std::move(other.storage_.value_));
662  other.FreeIfNeeded();
663  }
664  return;
665  }
666 
667  DCHECK(storage_.is_populated_ && other.storage_.is_populated_);
668  using std::swap;
669  swap(**this, *other);
670  }
671 
672  void reset() { FreeIfNeeded(); }
673 
674  template <class... Args>
675  T& emplace(Args&&... args) {
676  FreeIfNeeded();
677  storage_.Init(std::forward<Args>(args)...);
678  return storage_.value_;
679  }
680 
681  template <class U, class... Args>
682  typename std::enable_if<
683  std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
684  T&>::type
685  emplace(std::initializer_list<U> il, Args&&... args) {
686  FreeIfNeeded();
687  storage_.Init(il, std::forward<Args>(args)...);
688  return storage_.value_;
689  }
690 
691  private:
692  // Accessing template base class's protected member needs explicit
693  // declaration to do so.
694  using internal::OptionalBase<T>::CopyAssign;
695  using internal::OptionalBase<T>::FreeIfNeeded;
696  using internal::OptionalBase<T>::InitOrAssign;
697  using internal::OptionalBase<T>::MoveAssign;
698  using internal::OptionalBase<T>::storage_;
699 };
700 
701 // Here after defines comparation operators. The definition follows
702 // http://en.cppreference.com/w/cpp/utility/optional/operator_cmp
703 // while bool() casting is replaced by has_value() to meet the chromium
704 // style guide.
705 template <class T, class U>
706 bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) {
707  if (lhs.has_value() != rhs.has_value()) return false;
708  if (!lhs.has_value()) return true;
709  return *lhs == *rhs;
710 }
711 
712 template <class T, class U>
713 bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) {
714  if (lhs.has_value() != rhs.has_value()) return true;
715  if (!lhs.has_value()) return false;
716  return *lhs != *rhs;
717 }
718 
719 template <class T, class U>
720 bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) {
721  if (!rhs.has_value()) return false;
722  if (!lhs.has_value()) return true;
723  return *lhs < *rhs;
724 }
725 
726 template <class T, class U>
727 bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) {
728  if (!lhs.has_value()) return true;
729  if (!rhs.has_value()) return false;
730  return *lhs <= *rhs;
731 }
732 
733 template <class T, class U>
734 bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) {
735  if (!lhs.has_value()) return false;
736  if (!rhs.has_value()) return true;
737  return *lhs > *rhs;
738 }
739 
740 template <class T, class U>
741 bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) {
742  if (!rhs.has_value()) return true;
743  if (!lhs.has_value()) return false;
744  return *lhs >= *rhs;
745 }
746 
747 template <class T>
748 constexpr bool operator==(const Optional<T>& opt, nullopt_t) {
749  return !opt;
750 }
751 
752 template <class T>
753 constexpr bool operator==(nullopt_t, const Optional<T>& opt) {
754  return !opt;
755 }
756 
757 template <class T>
758 constexpr bool operator!=(const Optional<T>& opt, nullopt_t) {
759  return opt.has_value();
760 }
761 
762 template <class T>
763 constexpr bool operator!=(nullopt_t, const Optional<T>& opt) {
764  return opt.has_value();
765 }
766 
767 template <class T>
768 constexpr bool operator<(const Optional<T>& opt, nullopt_t) {
769  return false;
770 }
771 
772 template <class T>
773 constexpr bool operator<(nullopt_t, const Optional<T>& opt) {
774  return opt.has_value();
775 }
776 
777 template <class T>
778 constexpr bool operator<=(const Optional<T>& opt, nullopt_t) {
779  return !opt;
780 }
781 
782 template <class T>
783 constexpr bool operator<=(nullopt_t, const Optional<T>& opt) {
784  return true;
785 }
786 
787 template <class T>
788 constexpr bool operator>(const Optional<T>& opt, nullopt_t) {
789  return opt.has_value();
790 }
791 
792 template <class T>
793 constexpr bool operator>(nullopt_t, const Optional<T>& opt) {
794  return false;
795 }
796 
797 template <class T>
798 constexpr bool operator>=(const Optional<T>& opt, nullopt_t) {
799  return true;
800 }
801 
802 template <class T>
803 constexpr bool operator>=(nullopt_t, const Optional<T>& opt) {
804  return !opt;
805 }
806 
807 template <class T, class U>
808 constexpr bool operator==(const Optional<T>& opt, const U& value) {
809  return opt.has_value() ? *opt == value : false;
810 }
811 
812 template <class T, class U>
813 constexpr bool operator==(const U& value, const Optional<T>& opt) {
814  return opt.has_value() ? value == *opt : false;
815 }
816 
817 template <class T, class U>
818 constexpr bool operator!=(const Optional<T>& opt, const U& value) {
819  return opt.has_value() ? *opt != value : true;
820 }
821 
822 template <class T, class U>
823 constexpr bool operator!=(const U& value, const Optional<T>& opt) {
824  return opt.has_value() ? value != *opt : true;
825 }
826 
827 template <class T, class U>
828 constexpr bool operator<(const Optional<T>& opt, const U& value) {
829  return opt.has_value() ? *opt < value : true;
830 }
831 
832 template <class T, class U>
833 constexpr bool operator<(const U& value, const Optional<T>& opt) {
834  return opt.has_value() ? value < *opt : false;
835 }
836 
837 template <class T, class U>
838 constexpr bool operator<=(const Optional<T>& opt, const U& value) {
839  return opt.has_value() ? *opt <= value : true;
840 }
841 
842 template <class T, class U>
843 constexpr bool operator<=(const U& value, const Optional<T>& opt) {
844  return opt.has_value() ? value <= *opt : false;
845 }
846 
847 template <class T, class U>
848 constexpr bool operator>(const Optional<T>& opt, const U& value) {
849  return opt.has_value() ? *opt > value : false;
850 }
851 
852 template <class T, class U>
853 constexpr bool operator>(const U& value, const Optional<T>& opt) {
854  return opt.has_value() ? value > *opt : true;
855 }
856 
857 template <class T, class U>
858 constexpr bool operator>=(const Optional<T>& opt, const U& value) {
859  return opt.has_value() ? *opt >= value : false;
860 }
861 
862 template <class T, class U>
863 constexpr bool operator>=(const U& value, const Optional<T>& opt) {
864  return opt.has_value() ? value >= *opt : true;
865 }
866 
867 template <class T>
868 constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) {
869  return Optional<typename std::decay<T>::type>(std::forward<T>(value));
870 }
871 
872 template <class T, class... Args>
873 constexpr Optional<T> make_optional(Args&&... args) {
874  return Optional<T>(in_place, std::forward<Args>(args)...);
875 }
876 
877 template <class T, class U, class... Args>
878 constexpr Optional<T> make_optional(std::initializer_list<U> il,
879  Args&&... args) {
880  return Optional<T>(in_place, il, std::forward<Args>(args)...);
881 }
882 
883 // Partial specialization for a function template is not allowed. Also, it is
884 // not allowed to add overload function to std namespace, while it is allowed
885 // to specialize the template in std. Thus, swap() (kind of) overloading is
886 // defined in base namespace, instead.
887 template <class T>
888 typename std::enable_if<std::is_move_constructible<T>::value &&
889  internal::IsSwappable<T>::value>::type
890 swap(Optional<T>& lhs, Optional<T>& rhs) {
891  lhs.swap(rhs);
892 }
893 
894 } // namespace base
895 } // namespace v8
896 
897 #endif // V8_BASE_OPTIONAL_H_
STL namespace.
Definition: libplatform.h:13