shared: add _NM_ENSURE_TYPE_CONST()

The sole purpose of this is more type-safe macros.

An alternative solution would be to define a function instead of a
macro. But if the function is unused (currently!) you get a compiler
warning (on some compilers even when marking the function as "static
inline", if it's in the source file). A workaround for that would be
to mark the function as _nm_unused, or to use a macro instead.

_NM_ENSURE_TYPE_CONST() is to aid the macro solution.
This commit is contained in:
Thomas Haller
2019-05-24 08:46:48 +02:00
parent 8f1b542bd7
commit 17dc6a9da6

View File

@@ -635,8 +635,10 @@ NM_G_ERROR_MSG (GError *error)
* 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)), type: ((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