swift
Loading...
Searching...
No Matches
optional.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_OPTIONAL_H
7#define SWIFT_MISC_OPTIONAL_H
8
9#include <utility>
10
11#include <QtGlobal>
12
13namespace swift::misc
14{
15
21 template <typename T>
23 {
24 public:
26 Optional() noexcept = default;
27
29 Optional(T value) noexcept(std::is_nothrow_move_constructible_v<T>)
30 {
31 new (m_data.bytes) T(std::move(value));
32 m_isValid = true;
33 }
34
36 Optional(std::nullptr_t) noexcept {}
37
39 Optional(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v<T>)
40 {
41 if (other.m_isValid)
42 {
43 new (m_data.bytes) T(*other);
44 }
45 m_isValid = other.m_isValid;
46 }
47
49 Optional(Optional &&other) noexcept(std::is_nothrow_move_constructible_v<T>)
50 {
51 if (other.m_isValid)
52 {
53 new (m_data.bytes) T(std::move(*other));
54 }
55 m_isValid = other.m_isValid;
56 }
57
59 Optional &operator=(std::nullptr_t) noexcept
60 {
61 reset();
62 return *this;
63 }
64
66 Optional &operator=(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v<T>)
67 {
68 reset();
69 if (other.m_isValid)
70 {
71 new (m_data.bytes) T(*other);
72 }
73 m_isValid = other.m_isValid;
74 return *this;
75 }
76
78 Optional &operator=(Optional &&other) noexcept(std::is_nothrow_move_constructible_v<T>)
79 {
80 reset();
81 if (other.m_isValid)
82 {
83 new (m_data.bytes) T(std::move(*other));
84 }
85 m_isValid = other.m_isValid;
86 return *this;
87 }
88
91 {
92 if (m_isValid)
93 {
94 (*this)->~T();
95 }
96 }
97
99 explicit operator bool() const noexcept { return m_isValid; }
100
102 void reset() noexcept
103 {
104 if (m_isValid)
105 {
106 (*this)->~T();
107 }
108 m_isValid = false;
109 }
110
112 T &operator*() { return dereference(); }
113
115 const T &operator*() const { return dereference(); }
116
118 T *operator->() { return &dereference(); }
119
121 const T *operator->() const { return &dereference(); }
122
123 private:
124 bool m_isValid = false;
125
126 T &dereference()
127 {
128 Q_ASSERT(m_isValid);
129 return m_data.object;
130 }
131 const T &dereference() const
132 {
133 Q_ASSERT(m_isValid);
134 return m_data.object;
135 }
136 union Data
137 {
138 Data() {} // NOLINT(modernize-use-equals-default)
139 ~Data() {} // NOLINT(modernize-use-equals-default)
140 char bytes[sizeof(T)];
141 T object;
142 };
143 Data m_data;
144 };
145
149 template <typename T>
150 void swap(Optional<T> &a, Optional<T> &b) noexcept(std::is_nothrow_swappable_v<T>)
151 {
152 if (a)
153 {
154 if (b)
155 {
156 using std::swap;
157 swap(*a, *b);
158 }
159 else
160 {
161 b = std::move(a);
162 a = nullptr;
163 }
164 }
165 else if (b)
166 {
167 a = std::move(b);
168 b = nullptr;
169 }
170 }
171
172} // namespace swift::misc
173
174#endif // SWIFT_MISC_OPTIONAL_H
Own implementation of std::optional.
Definition optional.h:23
void reset() noexcept
If object is valid, destroy to make it invalid.
Definition optional.h:102
Optional & operator=(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Copy assignment.
Definition optional.h:66
~Optional()
Destructor.
Definition optional.h:90
Optional & operator=(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Move assignment.
Definition optional.h:78
Optional & operator=(std::nullptr_t) noexcept
Assign a nullptr.
Definition optional.h:59
const T & operator*() const
Dereference operator, returns reference to contained value, undefined if there is no value contained.
Definition optional.h:115
Optional(std::nullptr_t) noexcept
Construct from a nullptr, equivalent to default constructor.
Definition optional.h:36
Optional() noexcept=default
Default constructor.
T & operator*()
Dereference operator, returns reference to contained value, undefined if there is no value contained.
Definition optional.h:112
T * operator->()
Indirection operator, returns pointer to contained value, undefined if there is no value contained.
Definition optional.h:118
Optional(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Move constructor.
Definition optional.h:49
Optional(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Copy constructor.
Definition optional.h:39
const T * operator->() const
Indirection operator, returns pointer to contained value, undefined if there is no value contained.
Definition optional.h:121
Free functions in swift::misc.
void swap(Optional< T > &a, Optional< T > &b) noexcept(std::is_nothrow_swappable_v< T >)
Efficient swap for two Optional objects.
Definition optional.h:150