std-aux: move _NM_ENSURE_TYPE(), _NM_ENSURE_TYPE_CONST(), NM_STRUCT_OFFSET_ENSURE_TYPE()

This commit is contained in:
Thomas Haller 2021-07-30 10:36:01 +02:00
parent 194e29d0dc
commit 387d5ded93
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 28 additions and 26 deletions

View file

@ -343,32 +343,6 @@ NM_G_ERROR_MSG(GError *error)
_ptr; \
})
#if _NM_CC_SUPPORT_GENERIC
/* returns @value, if the type of @value matches @type.
* This requires support for C11 _Generic(). If no support is
* present, this returns @value directly.
*
* It's useful to check the let the compiler ensure that @value is
* of a certain type. */
#define _NM_ENSURE_TYPE(type, value) (_Generic((value), type : (value)))
#define _NM_ENSURE_TYPE_CONST(type, value) \
(_Generic((value), const type \
: ((const type) (value)), const type const \
: ((const type) (value)), type \
: ((const type) (value)), type const \
: ((const type) (value))))
#else
#define _NM_ENSURE_TYPE(type, value) (value)
#define _NM_ENSURE_TYPE_CONST(type, value) ((const type) (value))
#endif
#if _NM_CC_SUPPORT_GENERIC && (!defined(__clang__) || __clang_major__ > 3)
#define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) \
(_Generic((&(((container *) NULL)->field))[0], type : G_STRUCT_OFFSET(container, field)))
#else
#define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) G_STRUCT_OFFSET(container, field)
#endif
/* Casts (arg) to (type**), but also having a compile time check that
* the arg is some sort of pointer to a pointer.
*

View file

@ -239,6 +239,34 @@ typedef uint64_t _nm_bitwise nm_be64_t;
/*****************************************************************************/
#if _NM_CC_SUPPORT_GENERIC
/* returns @value, if the type of @value matches @type.
* This requires support for C11 _Generic(). If no support is
* present, this returns @value directly.
*
* It's useful to check the let the compiler ensure that @value is
* of a certain type. */
#define _NM_ENSURE_TYPE(type, value) (_Generic((value), type : (value)))
#define _NM_ENSURE_TYPE_CONST(type, value) \
(_Generic((value), const type \
: ((const type) (value)), const type const \
: ((const type) (value)), type \
: ((const type) (value)), type const \
: ((const type) (value))))
#else
#define _NM_ENSURE_TYPE(type, value) (value)
#define _NM_ENSURE_TYPE_CONST(type, value) ((const type) (value))
#endif
#if _NM_CC_SUPPORT_GENERIC && (!defined(__clang__) || __clang_major__ > 3)
#define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) \
(_Generic((&(((container *) NULL)->field))[0], type : nm_offsetof(container, field)))
#else
#define NM_STRUCT_OFFSET_ENSURE_TYPE(type, container, field) nm_offsetof(container, field)
#endif
/*****************************************************************************/
static inline uint32_t
nm_add_clamped_u32(uint32_t a, uint32_t b)
{