1 //===-- Standalone implementation of std::optional --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_OPTIONAL_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_OPTIONAL_H
12 #include "src/__support/CPP/type_traits.h"
13 #include "src/__support/CPP/utility.h"
14 #include "src/__support/macros/attributes.h"
16 namespace LIBC_NAMESPACE
{
19 // Trivial nullopt_t struct.
21 LIBC_INLINE
constexpr explicit nullopt_t() = default;
24 // nullopt that can be used and returned.
25 LIBC_INLINE_VAR
constexpr nullopt_t nullopt
{};
27 // This is very simple implementation of the std::optional class. It makes
28 // several assumptions that the underlying type is trivially constructable,
29 // copyable, or movable.
30 template <typename T
> class optional
{
31 template <typename U
, bool = !is_trivially_destructible
<U
>::value
>
32 struct OptionalStorage
{
38 LIBC_INLINE
~OptionalStorage() { stored_value
.~U(); }
39 LIBC_INLINE
constexpr OptionalStorage() : empty() {}
41 template <typename
... Args
>
42 LIBC_INLINE
constexpr explicit OptionalStorage(in_place_t
, Args
&&...args
)
43 : stored_value(forward
<Args
>(args
)...) {}
46 template <typename U
> struct OptionalStorage
<U
, false> {
52 // The only difference is that this class doesn't have a destructor.
53 LIBC_INLINE
constexpr OptionalStorage() : empty() {}
55 template <typename
... Args
>
56 LIBC_INLINE
constexpr explicit OptionalStorage(in_place_t
, Args
&&...args
)
57 : stored_value(forward
<Args
>(args
)...) {}
60 OptionalStorage
<T
> storage
;
64 LIBC_INLINE
constexpr optional() = default;
65 LIBC_INLINE
constexpr optional(nullopt_t
) {}
67 LIBC_INLINE
constexpr optional(const T
&t
)
68 : storage(in_place
, t
), in_use(true) {}
69 LIBC_INLINE
constexpr optional(const optional
&) = default;
71 LIBC_INLINE
constexpr optional(T
&&t
)
72 : storage(in_place
, move(t
)), in_use(true) {}
73 LIBC_INLINE
constexpr optional(optional
&&O
) = default;
75 template <typename
... ArgTypes
>
76 LIBC_INLINE
constexpr optional(in_place_t
, ArgTypes
&&...Args
)
77 : storage(in_place
, forward
<ArgTypes
>(Args
)...), in_use(true) {}
79 LIBC_INLINE
constexpr optional
&operator=(T
&&t
) {
83 LIBC_INLINE
constexpr optional
&operator=(optional
&&) = default;
85 LIBC_INLINE
constexpr optional
&operator=(const T
&t
) {
89 LIBC_INLINE
constexpr optional
&operator=(const optional
&) = default;
91 LIBC_INLINE
constexpr void reset() {
93 storage
.~OptionalStorage();
97 LIBC_INLINE
constexpr const T
&value() const & {
98 return storage
.stored_value
;
101 LIBC_INLINE
constexpr T
&value() & { return storage
.stored_value
; }
103 LIBC_INLINE
constexpr explicit operator bool() const { return in_use
; }
104 LIBC_INLINE
constexpr bool has_value() const { return in_use
; }
105 LIBC_INLINE
constexpr const T
*operator->() const {
106 return &storage
.stored_value
;
108 LIBC_INLINE
constexpr T
*operator->() { return &storage
.stored_value
; }
109 LIBC_INLINE
constexpr const T
&operator*() const & {
110 return storage
.stored_value
;
112 LIBC_INLINE
constexpr T
&operator*() & { return storage
.stored_value
; }
114 LIBC_INLINE
constexpr T
&&value() && { return move(storage
.stored_value
); }
115 LIBC_INLINE
constexpr T
&&operator*() && {
116 return move(storage
.stored_value
);
121 } // namespace LIBC_NAMESPACE
123 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_OPTIONAL_H