Petr Písař 548d15
From 21c794102c27d8474809cb54578941f8bbe63218 Mon Sep 17 00:00:00 2001
Petr Písař 548d15
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Petr Písař 548d15
Date: Tue, 28 Feb 2012 11:22:11 +0100
Petr Písař 548d15
Subject: [PATCH] Fix compiling C++ applications
Petr Písař 548d15
MIME-Version: 1.0
Petr Písař 548d15
Content-Type: text/plain; charset=UTF-8
Petr Písař 548d15
Content-Transfer-Encoding: 8bit
Petr Písař 548d15
Petr Písař 548d15
The compiler error is:
Petr Písař 548d15
 /usr/include/bluetooth/bluetooth.h::131:9: error: invalid conversion from 'void*' to 'bt_get_le64(void*)::<anonymous struct>*'
Petr Písař 548d15
 ...
Petr Písař 548d15
Petr Písař 548d15
The reason is that C++, in contrast to C, does not allow conversion of
Petr Písař 548d15
void * to anything, and this code gets compiled as C++ when the app is
Petr Písař 548d15
written in C++. The macro with the assignment itself is older, but only
Petr Písař 548d15
recent Bluez starts to use it in inline functions, thus triggering the
Petr Písař 548d15
problem.
Petr Písař 548d15
Petr Písař 548d15
This patch keeps the "struct __attribute__((packed))" magic and merely
Petr Písař 548d15
changes the typecast so that it works in C and C++. Like the existing
Petr Písař 548d15
macro this patch relies on support for typeof.
Petr Písař 548d15
Petr Písař 548d15
The new variant of the code is in an ifdef and only used for C++
Petr Písař 548d15
to avoid unexpected regressions in C applications.
Petr Písař 548d15
Petr Písař 548d15
Signed-off-by: Patrick Ohly <patrick.ohly@intel.com>
Petr Písař 548d15
---
Petr Písař 548d15
 lib/bluetooth.h |   31 +++++++++++++++++++++++++++++++
Petr Písař 548d15
 1 files changed, 31 insertions(+), 0 deletions(-)
Petr Písař 548d15
Petr Písař 548d15
diff --git a/lib/bluetooth.h b/lib/bluetooth.h
Petr Písař 548d15
index 5bd4f03..3f1a177 100644
Petr Písař 548d15
--- a/lib/bluetooth.h
Petr Písař 548d15
+++ b/lib/bluetooth.h
Petr Písař 548d15
@@ -109,6 +109,12 @@ enum {
Petr Písař 548d15
 #endif
Petr Písař 548d15
 
Petr Písař 548d15
 /* Bluetooth unaligned access */
Petr Písař 548d15
+#ifndef __cplusplus
Petr Písař 548d15
+/*
Petr Písař 548d15
+ * traditional code, doesn't work in C++ because
Petr Písař 548d15
+ * of the void * to struct pointer assignment
Petr Písař 548d15
+ */
Petr Písař 548d15
+
Petr Písař 548d15
 #define bt_get_unaligned(ptr)			\
Petr Písař 548d15
 ({						\
Petr Písař 548d15
 	struct __attribute__((packed)) {	\
Petr Písař 548d15
@@ -125,6 +131,31 @@ do {						\
Petr Písař 548d15
 	__p->__v = (val);			\
Petr Písař 548d15
 } while(0)
Petr Písař 548d15
 
Petr Písař 548d15
+#else /* __cplusplus */
Petr Písař 548d15
+
Petr Písař 548d15
+/*
Petr Písař 548d15
+ * modified code with typeof typecast, for C++;
Petr Písař 548d15
+ * the traditional code continues to be used for
Petr Písař 548d15
+ * C to avoid unexpected regressions with this
Petr Písař 548d15
+ * code here (it should work in C and C++, though)
Petr Písař 548d15
+ */
Petr Písař 548d15
+#define bt_get_unaligned(ptr)                  \
Petr Písař 548d15
+({                                             \
Petr Písař 548d15
+       struct __attribute__((packed)) {        \
Petr Písař 548d15
+               typeof(*(ptr)) __v;             \
Petr Písař 548d15
+       } *__p = (typeof(__p)) (ptr);           \
Petr Písař 548d15
+       __p->__v;                               \
Petr Písař 548d15
+})
Petr Písař 548d15
+
Petr Písař 548d15
+#define bt_put_unaligned(val, ptr)             \
Petr Písař 548d15
+do {                                           \
Petr Písař 548d15
+       struct __attribute__((packed)) {        \
Petr Písař 548d15
+               typeof(*(ptr)) __v;             \
Petr Písař 548d15
+       } *__p = (typeof(__p)) (ptr);           \
Petr Písař 548d15
+       __p->__v = (val);                       \
Petr Písař 548d15
+} while(0)
Petr Písař 548d15
+#endif /* __cplusplus */
Petr Písař 548d15
+
Petr Písař 548d15
 #if __BYTE_ORDER == __LITTLE_ENDIAN
Petr Písař 548d15
 static inline uint64_t bt_get_le64(void *ptr)
Petr Písař 548d15
 {
Petr Písař 548d15
-- 
Petr Písař 548d15
1.7.7.6
Petr Písař 548d15