summaryrefslogtreecommitdiff
path: root/gdb/common/gdb_optional.h
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2017-01-10 23:34:22 -0700
committerTom Tromey <tom@tromey.com>2017-02-10 12:24:31 -0700
commitd4b0bb186e204f77ed70bc719d16c6ca302094fd (patch)
treec919b755b87992476189da60f52abaab067d8907 /gdb/common/gdb_optional.h
parentf67f945cf2f6361d4c4997c487b174e396d23cd9 (diff)
downloadbinutils-gdb-d4b0bb186e204f77ed70bc719d16c6ca302094fd.tar.gz
Remove some ui_out-related cleanups from Python
This patch introduces a bit of infrastructure -- namely, a minimal std::optional analogue called gdb::optional, and an RAII template class that works like make_cleanup_ui_out_tuple_begin_end or make_cleanup_ui_out_list_begin_end -- and then uses these in the Python code. This removes a number of cleanups and generally simplifies this code. std::optional is only available in C++17. Normally I would have had this code check __cplusplus, but my gcc apparently isn't new enough to find <optional>, even with -std=c++1z; so, because I could not test it, the patch does not do this. gdb/ChangeLog 2017-02-10 Tom Tromey <tom@tromey.com> * ui-out.h (ui_out_emit_type): New class. (ui_out_emit_tuple, ui_out_emit_list): New typedefs. * python/py-framefilter.c (py_print_single_arg): Use gdb::optional and ui_out_emit_tuple. (enumerate_locals): Likewise. (py_mi_print_variables, py_print_locals, py_print_args): Use ui_out_emit_list. (py_print_frame): Use gdb::optional, ui_out_emit_tuple, ui_out_emit_list. * common/gdb_optional.h: New file.
Diffstat (limited to 'gdb/common/gdb_optional.h')
-rw-r--r--gdb/common/gdb_optional.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/gdb/common/gdb_optional.h b/gdb/common/gdb_optional.h
new file mode 100644
index 00000000000..d991da123cf
--- /dev/null
+++ b/gdb/common/gdb_optional.h
@@ -0,0 +1,87 @@
+/* An optional object.
+
+ Copyright (C) 2017 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDB_OPTIONAL_H
+#define GDB_OPTIONAL_H
+
+namespace gdb
+{
+
+/* This class attempts to be a compatible subset of std::optional,
+ which is slated to be available in C++17. This class optionally
+ holds an object of some type -- by default it is constructed not
+ holding an object, but later the object can be "emplaced". This is
+ similar to using std::unique_ptr, but in-object allocation is
+ guaranteed. */
+template<typename T>
+class optional
+{
+public:
+
+ optional ()
+ : m_instantiated (false)
+ {
+ }
+
+ ~optional ()
+ {
+ if (m_instantiated)
+ destroy ();
+ }
+
+ /* These aren't deleted in std::optional, but it was simpler to
+ delete them here, because currently the users of this class don't
+ need them, and making them depend on the definition of T is
+ somewhat complicated. */
+ optional (const optional &other) = delete;
+ optional<T> &operator= (const optional &other) = delete;
+
+ template<typename... Args>
+ void emplace (Args &&... args)
+ {
+ if (m_instantiated)
+ destroy ();
+ new (&m_item) T (std::forward<Args>(args)...);
+ m_instantiated = true;
+ }
+
+private:
+
+ /* Destroy the object. */
+ void destroy ()
+ {
+ gdb_assert (m_instantiated);
+ m_instantiated = false;
+ m_item.~T ();
+ }
+
+ /* The object. */
+ union
+ {
+ struct { } m_dummy;
+ T m_item;
+ };
+
+ /* True if the object was ever emplaced. */
+ bool m_instantiated;
+};
+
+}
+
+#endif /* GDB_OPTIONAL_H */