From 51003d409bb4b6c9a8540f70b92f8045abc4f0c9 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 15 Jul 2020 07:51:20 -0400 Subject: [PATCH] Use C++11 decltype where possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are various places glib uses __typeof__ for type safety, but that's a GNUC extension. C++11 has standard decltype() that does a similar job, at least for cases we care about. This avoids C++ code to always have to cast return value of g_object_ref() which was causing type kind of error: error: invalid conversion from ‘gpointer’ {aka ‘void*’} to ‘GstElementFactory*’ {aka ‘_GstElementFactory*’} [-fpermissive] --- glib/gmacros.h | 4 +++ glib/tests/cxx.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ glib/tests/meson.build | 3 ++ 3 files changed, 71 insertions(+) create mode 100644 glib/tests/cxx.cpp diff --git a/glib/gmacros.h b/glib/gmacros.h index 65f3738a6..ced69a75f 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -236,6 +236,10 @@ ((defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) || \ defined(__clang__)) #define glib_typeof(t) __typeof__ (t) +#elif defined(__cplusplus) && __cplusplus >= 201103L +/* C++11 decltype() is close enough for our usage */ +#include +#define glib_typeof(t) typename std::remove_reference::type #endif /* diff --git a/glib/tests/cxx.cpp b/glib/tests/cxx.cpp new file mode 100644 index 000000000..c423b2fbc --- /dev/null +++ b/glib/tests/cxx.cpp @@ -0,0 +1,64 @@ +/* + * Copyright 2020 Xavier Claessens + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +#include + +typedef struct +{ + int dummy; +} MyObject; + +static void +test_typeof (void) +{ +#if __cplusplus >= 201103L + // Test that with C++11 we don't get those kind of errors: + // error: invalid conversion from ‘gpointer’ {aka ‘void*’} to ‘MyObject*’ [-fpermissive] + MyObject *obj = g_rc_box_new0 (MyObject); + MyObject *obj2 = g_rc_box_acquire (obj); + g_assert_true (obj2 == obj); + + MyObject *obj3 = g_atomic_pointer_get (&obj2); + g_assert_true (obj3 == obj); + + MyObject *obj4 = nullptr; + g_atomic_pointer_set (&obj4, obj3); + g_assert_true (obj4 == obj); + + MyObject *obj5 = nullptr; + g_atomic_pointer_compare_and_exchange (&obj5, nullptr, obj4); + g_assert_true (obj5 == obj); + + MyObject *obj6 = g_steal_pointer (&obj5); + g_assert_true (obj6 == obj); + + g_clear_pointer (&obj6, g_rc_box_release); + g_rc_box_release (obj); +#else + g_test_skip ("This test requires C++11 compiler"); +#endif +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/C++/typeof", test_typeof); + + return g_test_run (); +} diff --git a/glib/tests/meson.build b/glib/tests/meson.build index 567f5eda4..1c78dbf08 100644 --- a/glib/tests/meson.build +++ b/glib/tests/meson.build @@ -14,6 +14,9 @@ glib_tests = { 'collate' : {}, 'cond' : {}, 'convert' : {}, + 'cxx' : { + 'source' : ['cxx.cpp'], + }, 'dataset' : {}, 'date' : {}, 'dir' : {},