mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-12 18:53:07 +02:00
Merge branch 'atomic-typeof' into 'master'
Define glib_typeof with C++11 decltype() Closes #2226 See merge request GNOME/glib!1715
This commit is contained in:
commit
e38982df4b
@ -735,6 +735,7 @@ G_ANALYZER_ANALYZING
|
|||||||
G_ANALYZER_NORETURN
|
G_ANALYZER_NORETURN
|
||||||
g_autoptr_cleanup_generic_gfree
|
g_autoptr_cleanup_generic_gfree
|
||||||
glib_typeof
|
glib_typeof
|
||||||
|
glib_typeof_2_68
|
||||||
g_macro__has_attribute
|
g_macro__has_attribute
|
||||||
g_macro__has_builtin
|
g_macro__has_builtin
|
||||||
g_macro__has_feature
|
g_macro__has_feature
|
||||||
|
@ -182,17 +182,6 @@ G_END_DECLS
|
|||||||
(void) (0 ? *(atomic) ^ (val) : 1); \
|
(void) (0 ? *(atomic) ^ (val) : 1); \
|
||||||
(guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \
|
(guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \
|
||||||
}))
|
}))
|
||||||
|
|
||||||
#if defined(glib_typeof)
|
|
||||||
#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
|
|
||||||
(G_GNUC_EXTENSION ({ \
|
|
||||||
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
|
|
||||||
glib_typeof ((oldval)) gapcae_oldval = (oldval); \
|
|
||||||
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
|
||||||
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
|
||||||
__atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
|
|
||||||
}))
|
|
||||||
#else /* if !defined(glib_typeof) */
|
|
||||||
#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
|
#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
|
||||||
(G_GNUC_EXTENSION ({ \
|
(G_GNUC_EXTENSION ({ \
|
||||||
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
|
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
|
||||||
@ -201,7 +190,6 @@ G_END_DECLS
|
|||||||
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
||||||
__atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
|
__atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
|
||||||
}))
|
}))
|
||||||
#endif /* defined(glib_typeof) */
|
|
||||||
#define g_atomic_pointer_add(atomic, val) \
|
#define g_atomic_pointer_add(atomic, val) \
|
||||||
(G_GNUC_EXTENSION ({ \
|
(G_GNUC_EXTENSION ({ \
|
||||||
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
||||||
|
@ -236,6 +236,11 @@
|
|||||||
((defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) || \
|
((defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) || \
|
||||||
defined(__clang__))
|
defined(__clang__))
|
||||||
#define glib_typeof(t) __typeof__ (t)
|
#define glib_typeof(t) __typeof__ (t)
|
||||||
|
#elif defined(__cplusplus) && __cplusplus >= 201103L
|
||||||
|
/* C++11 decltype() is close enough for our usage */
|
||||||
|
#include <type_traits>
|
||||||
|
#define glib_typeof(t) typename std::remove_reference<decltype (t)>::type
|
||||||
|
#define glib_typeof_2_68
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -110,7 +110,7 @@ gpointer g_try_realloc_n (gpointer mem,
|
|||||||
gsize n_blocks,
|
gsize n_blocks,
|
||||||
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
|
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
|
||||||
|
|
||||||
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
|
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58 && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
|
||||||
#define g_clear_pointer(pp, destroy) \
|
#define g_clear_pointer(pp, destroy) \
|
||||||
G_STMT_START \
|
G_STMT_START \
|
||||||
{ \
|
{ \
|
||||||
@ -213,7 +213,7 @@ g_steal_pointer (gpointer pp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* type safety */
|
/* type safety */
|
||||||
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
|
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58 && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
|
||||||
#define g_steal_pointer(pp) ((glib_typeof (*pp)) (g_steal_pointer) (pp))
|
#define g_steal_pointer(pp) ((glib_typeof (*pp)) (g_steal_pointer) (pp))
|
||||||
#else /* __GNUC__ */
|
#else /* __GNUC__ */
|
||||||
/* This version does not depend on gcc extensions, but gcc does not warn
|
/* This version does not depend on gcc extensions, but gcc does not warn
|
||||||
|
@ -71,7 +71,7 @@ gsize g_atomic_rc_box_get_size (gpointer mem_block);
|
|||||||
#define g_atomic_rc_box_new0(type) \
|
#define g_atomic_rc_box_new0(type) \
|
||||||
((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
|
((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
|
||||||
|
|
||||||
#ifdef glib_typeof
|
#if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
|
||||||
/* Type check to avoid assigning references to different types */
|
/* Type check to avoid assigning references to different types */
|
||||||
#define g_rc_box_acquire(mem_block) \
|
#define g_rc_box_acquire(mem_block) \
|
||||||
((glib_typeof (mem_block)) (g_rc_box_acquire) (mem_block))
|
((glib_typeof (mem_block)) (g_rc_box_acquire) (mem_block))
|
||||||
|
64
glib/tests/cxx.cpp
Normal file
64
glib/tests/cxx.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
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 ();
|
||||||
|
}
|
@ -14,6 +14,9 @@ glib_tests = {
|
|||||||
'collate' : {},
|
'collate' : {},
|
||||||
'cond' : {},
|
'cond' : {},
|
||||||
'convert' : {},
|
'convert' : {},
|
||||||
|
'cxx' : {
|
||||||
|
'source' : ['cxx.cpp'],
|
||||||
|
},
|
||||||
'dataset' : {},
|
'dataset' : {},
|
||||||
'date' : {},
|
'date' : {},
|
||||||
'dir' : {},
|
'dir' : {},
|
||||||
|
@ -513,7 +513,7 @@ GLIB_AVAILABLE_IN_ALL
|
|||||||
void g_object_remove_weak_pointer (GObject *object,
|
void g_object_remove_weak_pointer (GObject *object,
|
||||||
gpointer *weak_pointer_location);
|
gpointer *weak_pointer_location);
|
||||||
|
|
||||||
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
|
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56 && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
|
||||||
/* Make reference APIs type safe with macros */
|
/* Make reference APIs type safe with macros */
|
||||||
#define g_object_ref(Obj) ((glib_typeof (Obj)) (g_object_ref) (Obj))
|
#define g_object_ref(Obj) ((glib_typeof (Obj)) (g_object_ref) (Obj))
|
||||||
#define g_object_ref_sink(Obj) ((glib_typeof (Obj)) (g_object_ref_sink) (Obj))
|
#define g_object_ref_sink(Obj) ((glib_typeof (Obj)) (g_object_ref_sink) (Obj))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user