3 Copyright (C) 2017-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef COMMON_GDB_OPTIONAL_H
21 #define COMMON_GDB_OPTIONAL_H
23 #include "gdbsupport/traits.h"
30 explicit in_place_t () = default;
33 constexpr gdb::in_place_t in_place
{};
35 /* This class attempts to be a compatible subset of std::optional,
36 which is slated to be available in C++17. This class optionally
37 holds an object of some type -- by default it is constructed not
38 holding an object, but later the object can be "emplaced". This is
39 similar to using std::unique_ptr, but in-object allocation is
42 Unlike std::optional, we currently only support copy/move
43 construction/assignment of an optional<T> from either exactly
44 optional<T> or T. I.e., we don't support copy/move
45 construction/assignment from optional<U> or U, when U is a type
46 convertible to T. Making that work depending on the definitions of
47 T and U is somewhat complicated, and currently the users of this
48 class don't need it. */
59 template<typename
... Args
>
60 constexpr optional (in_place_t
, Args
&&... args
)
61 : m_item (std::forward
<Args
> (args
)...),
68 /* Copy and move constructors. */
70 optional (const optional
&other
)
72 if (other
.m_instantiated
)
73 this->emplace (other
.get ());
76 optional (optional
&&other
)
77 noexcept(std::is_nothrow_move_constructible
<T
> ())
79 if (other
.m_instantiated
)
80 this->emplace (std::move (other
.get ()));
83 constexpr optional (const T
&other
)
88 constexpr optional (T
&&other
)
89 noexcept (std::is_nothrow_move_constructible
<T
> ())
90 : m_item (std::move (other
)),
94 /* Assignment operators. */
97 operator= (const optional
&other
)
99 if (m_instantiated
&& other
.m_instantiated
)
100 this->get () = other
.get ();
103 if (other
.m_instantiated
)
104 this->emplace (other
.get ());
113 operator= (optional
&&other
)
114 noexcept (And
<std::is_nothrow_move_constructible
<T
>,
115 std::is_nothrow_move_assignable
<T
>> ())
117 if (m_instantiated
&& other
.m_instantiated
)
118 this->get () = std::move (other
.get ());
121 if (other
.m_instantiated
)
122 this->emplace (std::move (other
.get ()));
130 operator= (const T
&other
)
133 this->get () = other
;
135 this->emplace (other
);
140 operator= (T
&&other
)
141 noexcept (And
<std::is_nothrow_move_constructible
<T
>,
142 std::is_nothrow_move_assignable
<T
>> ())
145 this->get () = std::move (other
);
147 this->emplace (std::move (other
));
151 template<typename
... Args
>
152 T
&emplace (Args
&&... args
)
155 new (&m_item
) T (std::forward
<Args
>(args
)...);
156 m_instantiated
= true;
161 constexpr const T
*operator-> () const
162 { return std::addressof (this->get ()); }
165 { return std::addressof (this->get ()); }
167 constexpr const T
&operator* () const &
168 { return this->get (); }
171 { return this->get (); }
174 { return std::move (this->get ()); }
176 constexpr const T
&&operator* () const &&
177 { return std::move (this->get ()); }
179 constexpr explicit operator bool () const noexcept
180 { return m_instantiated
; }
182 constexpr bool has_value () const noexcept
183 { return m_instantiated
; }
185 /* 'reset' is a 'safe' operation with no precondition. */
186 void reset () noexcept
194 /* Destroy the object. */
197 gdb_assert (m_instantiated
);
198 m_instantiated
= false;
202 /* The get operations have m_instantiated as a precondition. */
205 #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L
206 gdb_assert (this->has_value ());
210 constexpr const T
&get () const noexcept
212 #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L
213 gdb_assert (this->has_value ());
223 volatile char dont_use
; /* Silences -Wmaybe-uninitialized warning, see
227 /* True if the object was ever emplaced. */
228 bool m_instantiated
= false;
233 #endif /* COMMON_GDB_OPTIONAL_H */