This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 01/14] Add a type-safe C++ interface to a registry


This changes DECLARE_REGISTRY to add a type-safe interface.  This
interface is a C++ class that handles the details of registering a
key, and provides various useful methods, including policy-based
cleanup.

gdb/ChangeLog
2019-04-22  Tom Tromey  <tom@tromey.com>

	* registry.h (DECLARE_REGISTRY): Define the _key class.
---
 gdb/ChangeLog  |  4 ++++
 gdb/registry.h | 53 ++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/gdb/registry.h b/gdb/registry.h
index 3881e29b54f..5890147681d 100644
--- a/gdb/registry.h
+++ b/gdb/registry.h
@@ -243,11 +243,52 @@ typedef void (*registry_ ## TAG ## _callback) (struct TAG *, void *);	\
 extern const struct TAG ## _data *register_ ## TAG ## _data (void);	\
 extern const struct TAG ## _data *register_ ## TAG ## _data_with_cleanup \
  (registry_ ## TAG ## _callback save, registry_ ## TAG ## _callback free); \
-extern void clear_ ## TAG ## _data (struct TAG *);		\
-extern void set_ ## TAG ## _data (struct TAG *,			\
-				  const struct TAG ## _data *data, \
-				  void *value);			\
-extern void *TAG ## _data (struct TAG *,			\
-			   const struct TAG ## _data *data);
+extern void clear_ ## TAG ## _data (struct TAG *);			\
+extern void set_ ## TAG ## _data (struct TAG *,				\
+				  const struct TAG ## _data *data,	\
+				  void *value);				\
+extern void *TAG ## _data (struct TAG *,				\
+			   const struct TAG ## _data *data);		\
+									\
+template<typename DATA, typename Deleter = std::default_delete<DATA>>	\
+class TAG ## _key							\
+{									\
+public:									\
+									\
+  TAG ## _key ()							\
+    : m_key (register_ ## TAG ## _data_with_cleanup (nullptr,		\
+						     cleanup))		\
+  {									\
+  }									\
+									\
+  DATA *get (struct TAG *obj) const					\
+  {									\
+    return (DATA *) TAG ## _data (obj, m_key);				\
+  }									\
+									\
+  void set (struct TAG *obj, DATA *data) const				\
+  {									\
+    set_ ## TAG ## _data (obj, m_key, data);				\
+  }									\
+									\
+  template<typename... Args>						\
+  DATA *emplace (struct TAG *obj, Args &&...args) const			\
+  {									\
+    DATA *result = new DATA (std::forward<Args> (args)...);		\
+    set (obj, result);							\
+    return result;							\
+  }									\
+									\
+private:								\
+									\
+  static void cleanup (struct TAG *obj, void *arg)			\
+  {									\
+    DATA *datum = (DATA *) arg;						\
+    Deleter d;								\
+    d (datum);								\
+  }									\
+									\
+  const struct TAG ## _data *m_key;					\
+};
 
 #endif /* REGISTRY_H */
-- 
2.17.2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]