glib_typeof: Move definition to its own header

It is cleaner to define glib_typeof() in a header included after
gversionmacros.h so we can use GLIB_VERSION_MIN_REQUIRED directly
instead of doing it everywhere glib_typeof() is used.
This commit is contained in:
Xavier Claessens 2021-02-26 15:15:57 -05:00
parent d64d46dc42
commit 090d65712d
9 changed files with 60 additions and 55 deletions

View File

@ -749,7 +749,6 @@ G_ANALYZER_ANALYZING
G_ANALYZER_NORETURN
g_autoptr_cleanup_generic_gfree
glib_typeof
glib_typeof_2_68
g_macro__has_attribute
g_macro__has_builtin
g_macro__has_feature

View File

@ -25,11 +25,7 @@
#endif
#include <glib/gtypes.h>
#if defined(glib_typeof_2_68) && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
/* for glib_typeof */
#include <type_traits>
#endif
#include <glib/glib-typeof.h>
G_BEGIN_DECLS
@ -108,7 +104,7 @@ G_END_DECLS
__atomic_store ((gint *)(atomic), &gais_temp, __ATOMIC_SEQ_CST); \
}))
#if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
#if defined(glib_typeof)
#define g_atomic_pointer_get(atomic) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
@ -125,7 +121,7 @@ G_END_DECLS
(void) (0 ? (gpointer) * (atomic) : NULL); \
__atomic_store (gaps_temp_atomic, &gaps_temp_newval, __ATOMIC_SEQ_CST); \
}))
#else /* if !(defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)) */
#else /* if !(defined(glib_typeof) */
#define g_atomic_pointer_get(atomic) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
@ -142,7 +138,7 @@ G_END_DECLS
(void) (0 ? (gpointer) *(atomic) : NULL); \
__atomic_store (gaps_temp_atomic, &gaps_temp_newval, __ATOMIC_SEQ_CST); \
}))
#endif /* if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68) */
#endif /* if defined(glib_typeof) */
#define g_atomic_int_inc(atomic) \
(G_GNUC_EXTENSION ({ \
@ -307,7 +303,7 @@ G_END_DECLS
__asm__ __volatile__ ("" : : : "memory"); \
gapg_result; \
}))
#if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
#if defined(glib_typeof)
#define g_atomic_pointer_set(atomic, newval) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
@ -316,7 +312,7 @@ G_END_DECLS
__asm__ __volatile__ ("" : : : "memory"); \
*(atomic) = (glib_typeof (*(atomic))) (gsize) (newval); \
}))
#else /* if !(defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)) */
#else /* if !(defined(glib_typeof) */
#define g_atomic_pointer_set(atomic, newval) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
@ -325,7 +321,7 @@ G_END_DECLS
__asm__ __volatile__ ("" : : : "memory"); \
*(atomic) = (gpointer) (gsize) (newval); \
}))
#endif /* if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68) */
#endif /* if defined(glib_typeof) */
#define g_atomic_int_inc(atomic) \
(G_GNUC_EXTENSION ({ \
@ -428,7 +424,7 @@ G_END_DECLS
#define g_atomic_int_dec_and_test(atomic) \
(g_atomic_int_dec_and_test ((gint *) (atomic)))
#if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
#if defined(glib_typeof)
/* The (void *) cast in the middle *looks* redundant, because
* g_atomic_pointer_get returns void * already, but it's to silence
* -Werror=bad-function-cast when we're doing something like:
@ -438,7 +434,7 @@ G_END_DECLS
* non-pointer-typed result. */
#define g_atomic_pointer_get(atomic) \
(glib_typeof (*(atomic))) (void *) ((g_atomic_pointer_get) ((void *) atomic))
#else /* !(defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)) */
#else /* !(defined(glib_typeof) */
#define g_atomic_pointer_get(atomic) \
(g_atomic_pointer_get (atomic))
#endif

43
glib/glib-typeof.h Normal file
View File

@ -0,0 +1,43 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 2021 Iain Lane, 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/>.
*/
#ifndef __GLIB_TYPEOF_H__
#define __GLIB_TYPEOF_H__
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
#error "Only <glib.h> can be included directly."
#endif
#include <glib/gversionmacros.h>
/*
* We can only use __typeof__ on GCC >= 4.8, and not when compiling C++. Since
* __typeof__ is used in a few places in GLib, provide a pre-processor symbol
* to factor the check out from callers.
*
* This symbol is private.
*/
#undef glib_typeof
#if !defined(__cplusplus) && (G_GNUC_CHECK_VERSION(4, 8) || defined(__clang__))
#define glib_typeof(t) __typeof__ (t)
#elif defined(__cplusplus) && __cplusplus >= 201103L && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
/* C++11 decltype() is close enough for our usage */
#include <type_traits>
#define glib_typeof(t) typename std::remove_reference<decltype (t)>::type
#endif
#endif /* __GLIB_TYPEOF_H__ */

View File

@ -112,6 +112,7 @@
#include <glib/deprecated/gthread.h>
#include <glib/glib-autocleanups.h>
#include <glib/glib-typeof.h>
#undef __GLIB_H_INSIDE__

View File

@ -276,28 +276,6 @@
#define G_GNUC_NULL_TERMINATED
#endif
/*
* We can only use __typeof__ on GCC >= 4.8, and not when compiling C++. Since
* __typeof__ is used in a few places in GLib, provide a pre-processor symbol
* to factor the check out from callers.
*
* This symbol is private.
*/
#undef glib_typeof
#if !defined(__cplusplus) && (G_GNUC_CHECK_VERSION(4, 8) || defined(__clang__))
#define glib_typeof(t) __typeof__ (t)
#elif defined(__cplusplus) && __cplusplus >= 201103L
/* C++11 decltype() is close enough for our usage */
/* This needs `#include <type_traits>`, but we have guarded this feature with a
* `GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68` check, and such a check
* cannot be enforced in this header due to include ordering requirements.
* Within GLib itself, which use `glib_typeof` need to add the include
* themselves. See other examples in GLib for how to do this.
*/
#define glib_typeof(t) typename std::remove_reference<decltype (t)>::type
#define glib_typeof_2_68
#endif
/*
* Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html
* These are not available on GCC, but since the pre-processor doesn't do

View File

@ -30,11 +30,7 @@
#endif
#include <glib/gutils.h>
#if defined(glib_typeof_2_68) && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
/* for glib_typeof */
#include <type_traits>
#endif
#include <glib/glib-typeof.h>
G_BEGIN_DECLS
@ -115,7 +111,7 @@ gpointer g_try_realloc_n (gpointer mem,
gsize n_blocks,
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
#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)
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
#define g_clear_pointer(pp, destroy) \
G_STMT_START \
{ \
@ -218,7 +214,7 @@ g_steal_pointer (gpointer pp)
}
/* type safety */
#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)
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
#define g_steal_pointer(pp) ((glib_typeof (*pp)) (g_steal_pointer) (pp))
#else /* __GNUC__ */
/* This version does not depend on gcc extensions, but gcc does not warn

View File

@ -23,11 +23,7 @@
#endif
#include <glib/gmem.h>
#if defined(glib_typeof_2_68) && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
/* for glib_typeof */
#include <type_traits>
#endif
#include <glib/glib-typeof.h>
G_BEGIN_DECLS
@ -76,7 +72,7 @@ gsize g_atomic_rc_box_get_size (gpointer mem_block);
#define g_atomic_rc_box_new0(type) \
((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
#if defined(glib_typeof) && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68)
#if defined(glib_typeof)
/* Type check to avoid assigning references to different types */
#define g_rc_box_acquire(mem_block) \
((glib_typeof (mem_block)) (g_rc_box_acquire) (mem_block))

View File

@ -142,6 +142,7 @@ install_headers(glib_deprecated_headers, subdir : 'glib-2.0/glib/deprecated')
glib_sub_headers = files(
'glib-autocleanups.h',
'glib-typeof.h',
'galloca.h',
'garray.h',
'gasyncqueue.h',

View File

@ -28,11 +28,6 @@
#include <gobject/gsignal.h>
#include <gobject/gboxed.h>
#if defined(glib_typeof_2_68) && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
/* for glib_typeof */
#include <type_traits>
#endif
G_BEGIN_DECLS
/* --- type macros --- */
@ -518,7 +513,7 @@ GLIB_AVAILABLE_IN_ALL
void g_object_remove_weak_pointer (GObject *object,
gpointer *weak_pointer_location);
#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)
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
/* Make reference APIs type safe with macros */
#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))