[glib: 1/2] glib_typeof: Move definition to its own header




commit 090d65712d56c3cdaa8ff1f601f19d8c5296d563
Author: Xavier Claessens <xavier claessens collabora com>
Date:   Fri Feb 26 15:15:57 2021 -0500

    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.

 docs/reference/glib/glib-sections.txt |  1 -
 glib/gatomic.h                        | 22 ++++++++----------
 glib/glib-typeof.h                    | 43 +++++++++++++++++++++++++++++++++++
 glib/glib.h                           |  1 +
 glib/gmacros.h                        | 22 ------------------
 glib/gmem.h                           | 10 +++-----
 glib/grcbox.h                         |  8 ++-----
 glib/meson.build                      |  1 +
 gobject/gobject.h                     |  7 +-----
 9 files changed, 60 insertions(+), 55 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index d8a8d7d95..c956a525f 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -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
diff --git a/glib/gatomic.h b/glib/gatomic.h
index 2ad648aad..5583fb0c9 100644
--- a/glib/gatomic.h
+++ b/glib/gatomic.h
@@ -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
diff --git a/glib/glib-typeof.h b/glib/glib-typeof.h
new file mode 100644
index 000000000..198901c29
--- /dev/null
+++ b/glib/glib-typeof.h
@@ -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__ */
diff --git a/glib/glib.h b/glib/glib.h
index a4d43a9bf..e72c09da5 100644
--- a/glib/glib.h
+++ b/glib/glib.h
@@ -112,6 +112,7 @@
 #include <glib/deprecated/gthread.h>
 
 #include <glib/glib-autocleanups.h>
+#include <glib/glib-typeof.h>
 
 #undef __GLIB_H_INSIDE__
 
diff --git a/glib/gmacros.h b/glib/gmacros.h
index 031ec2d44..27c2c48ac 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -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
diff --git a/glib/gmem.h b/glib/gmem.h
index ccf477843..47c4735ac 100644
--- a/glib/gmem.h
+++ b/glib/gmem.h
@@ -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
diff --git a/glib/grcbox.h b/glib/grcbox.h
index 47dfd488b..0f8b99a80 100644
--- a/glib/grcbox.h
+++ b/glib/grcbox.h
@@ -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))
diff --git a/glib/meson.build b/glib/meson.build
index 8c18e6de4..28bfae200 100644
--- a/glib/meson.build
+++ b/glib/meson.build
@@ -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',
diff --git a/gobject/gobject.h b/gobject/gobject.h
index 125aed876..aec8975e4 100644
--- a/gobject/gobject.h
+++ b/gobject/gobject.h
@@ -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))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]