New global maintainer - Simon Marchi
[deliverable/binutils-gdb.git] / gdb / common / gdb_optional.h
1 /* An optional object.
2
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #ifndef GDB_OPTIONAL_H
21 #define GDB_OPTIONAL_H
22
23 namespace gdb
24 {
25
26 /* This class attempts to be a compatible subset of std::optional,
27 which is slated to be available in C++17. This class optionally
28 holds an object of some type -- by default it is constructed not
29 holding an object, but later the object can be "emplaced". This is
30 similar to using std::unique_ptr, but in-object allocation is
31 guaranteed. */
32 template<typename T>
33 class optional
34 {
35 public:
36
37 constexpr optional ()
38 : m_dummy (),
39 m_instantiated (false)
40 {
41 }
42
43 ~optional ()
44 {
45 if (m_instantiated)
46 destroy ();
47 }
48
49 /* These aren't deleted in std::optional, but it was simpler to
50 delete them here, because currently the users of this class don't
51 need them, and making them depend on the definition of T is
52 somewhat complicated. */
53 optional (const optional &other) = delete;
54 optional<T> &operator= (const optional &other) = delete;
55
56 template<typename... Args>
57 void emplace (Args &&... args)
58 {
59 if (m_instantiated)
60 destroy ();
61 new (&m_item) T (std::forward<Args>(args)...);
62 m_instantiated = true;
63 }
64
65 /* Observers. */
66 constexpr const T *operator-> () const
67 { return std::addressof (this->get ()); }
68
69 T *operator-> ()
70 { return std::addressof (this->get ()); }
71
72 constexpr const T &operator* () const &
73 { return this->get (); }
74
75 T &operator* () &
76 { return this->get (); }
77
78 T &&operator* () &&
79 { return std::move (this->get ()); }
80
81 constexpr const T &&operator* () const &&
82 { return std::move (this->get ()); }
83
84 constexpr explicit operator bool () const noexcept
85 { return m_instantiated; }
86
87 constexpr bool has_value () const noexcept
88 { return m_instantiated; }
89
90 private:
91
92 /* Destroy the object. */
93 void destroy ()
94 {
95 gdb_assert (m_instantiated);
96 m_instantiated = false;
97 m_item.~T ();
98 }
99
100 /* The get operations have m_instantiated as a precondition. */
101 T &get () noexcept { return m_item; }
102 constexpr const T &get () const noexcept { return m_item; }
103
104 /* The object. */
105 union
106 {
107 struct { } m_dummy;
108 T m_item;
109 };
110
111 /* True if the object was ever emplaced. */
112 bool m_instantiated;
113 };
114
115 }
116
117 #endif /* GDB_OPTIONAL_H */
This page took 0.03129 seconds and 4 git commands to generate.