Commit | Line | Data |
---|---|---|
d4b0bb18 TT |
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 | optional () | |
38 | : m_instantiated (false) | |
39 | { | |
40 | } | |
41 | ||
42 | ~optional () | |
43 | { | |
44 | if (m_instantiated) | |
45 | destroy (); | |
46 | } | |
47 | ||
48 | /* These aren't deleted in std::optional, but it was simpler to | |
49 | delete them here, because currently the users of this class don't | |
50 | need them, and making them depend on the definition of T is | |
51 | somewhat complicated. */ | |
52 | optional (const optional &other) = delete; | |
53 | optional<T> &operator= (const optional &other) = delete; | |
54 | ||
55 | template<typename... Args> | |
56 | void emplace (Args &&... args) | |
57 | { | |
58 | if (m_instantiated) | |
59 | destroy (); | |
60 | new (&m_item) T (std::forward<Args>(args)...); | |
61 | m_instantiated = true; | |
62 | } | |
63 | ||
64 | private: | |
65 | ||
66 | /* Destroy the object. */ | |
67 | void destroy () | |
68 | { | |
69 | gdb_assert (m_instantiated); | |
70 | m_instantiated = false; | |
71 | m_item.~T (); | |
72 | } | |
73 | ||
74 | /* The object. */ | |
75 | union | |
76 | { | |
77 | struct { } m_dummy; | |
78 | T m_item; | |
79 | }; | |
80 | ||
81 | /* True if the object was ever emplaced. */ | |
82 | bool m_instantiated; | |
83 | }; | |
84 | ||
85 | } | |
86 | ||
87 | #endif /* GDB_OPTIONAL_H */ |