qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] include/qemu: Provide a C++ compatible version of typeof_strip_qual
@ 2024-06-24 20:56 Felix Wu
  2024-06-25  2:26 ` Philippe Mathieu-Daudé
  2024-06-25  7:52 ` Daniel P. Berrangé
  0 siblings, 2 replies; 10+ messages in thread
From: Felix Wu @ 2024-06-24 20:56 UTC (permalink / raw)
  To: peter.maydell; +Cc: qemu-devel, Roman Kiryanov, Felix Wu

From: Roman Kiryanov <rkir@google.com>

to use the QEMU headers with a C++ compiler.

Signed-off-by: Felix Wu <flwu@google.com>
Signed-off-by: Roman Kiryanov <rkir@google.com>
---
 include/qemu/atomic.h   |  8 ++++++++
 include/qemu/atomic.hpp | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 include/qemu/atomic.hpp

diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
index 99110abefb..aeaecc440a 100644
--- a/include/qemu/atomic.h
+++ b/include/qemu/atomic.h
@@ -20,6 +20,13 @@
 /* Compiler barrier */
 #define barrier()   ({ asm volatile("" ::: "memory"); (void)0; })
 
+#ifdef __cplusplus
+
+#ifndef typeof_strip_qual
+#error Use the typeof_strip_qual(expr) definition from atomic.hpp on C++ builds.
+#endif
+
+#else  /* __cpluplus */
 /* The variable that receives the old value of an atomically-accessed
  * variable must be non-qualified, because atomic builtins return values
  * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
@@ -61,6 +68,7 @@
         __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
         (unsigned short)1,                                                         \
       (expr)+0))))))
+#endif  /* __cpluplus */
 
 #ifndef __ATOMIC_RELAXED
 #error "Expecting C11 atomic ops"
diff --git a/include/qemu/atomic.hpp b/include/qemu/atomic.hpp
new file mode 100644
index 0000000000..5844e3d427
--- /dev/null
+++ b/include/qemu/atomic.hpp
@@ -0,0 +1,38 @@
+/*
+ * The C++ definition for typeof_strip_qual used in atomic.h.
+ *
+ * Copyright (C) 2024 Google, Inc.
+ *
+ * Author: Roman Kiryanov <rkir@google.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ * See docs/devel/atomics.rst for discussion about the guarantees each
+ * atomic primitive is meant to provide.
+ */
+
+#ifndef QEMU_ATOMIC_HPP
+#define QEMU_ATOMIC_HPP
+
+#include <type_traits>
+
+/* Match the integer promotion behavior of typeof_strip_qual, see atomic.h */
+template <class T> struct typeof_strip_qual_cpp { using result = decltype(+T(0)); };
+
+template <> struct typeof_strip_qual_cpp<bool> { using result = bool; };
+template <> struct typeof_strip_qual_cpp<signed char> { using result = signed char; };
+template <> struct typeof_strip_qual_cpp<unsigned char> { using result = unsigned char; };
+template <> struct typeof_strip_qual_cpp<signed short> { using result = signed short; };
+template <> struct typeof_strip_qual_cpp<unsigned short> { using result = unsigned short; };
+
+#define typeof_strip_qual(expr) \
+    typeof_strip_qual_cpp< \
+        std::remove_cv< \
+            std::remove_reference< \
+                decltype(expr) \
+            >::type \
+        >::type \
+    >::result
+
+#endif /* QEMU_ATOMIC_HPP */
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-06-25 10:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-24 20:56 [PATCH 1/1] include/qemu: Provide a C++ compatible version of typeof_strip_qual Felix Wu
2024-06-25  2:26 ` Philippe Mathieu-Daudé
2024-06-25  2:32   ` Roman Kiryanov
2024-06-25  6:05     ` Paolo Bonzini
2024-06-25  6:19       ` Philippe Mathieu-Daudé
2024-06-25  9:27         ` Peter Maydell
2024-06-25 10:16           ` Peter Maydell
2024-06-25 10:31             ` Daniel P. Berrangé
2024-06-25  6:07     ` Thomas Huth
2024-06-25  7:52 ` Daniel P. Berrangé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).