shared: add NM_MIN()/NM_MAX() macros to replace glib's MIN()/MAX()

(cherry picked from commit b2016fd2a5)
This commit is contained in:
Thomas Haller 2016-09-05 14:12:41 +02:00
parent 99e30bdf70
commit 811aaead4c

View file

@ -542,6 +542,50 @@ nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
/*****************************************************************************/
/* Taken from systemd's UNIQ_T and UNIQ macros. */
#define NM_UNIQ_T(x, uniq) G_PASTE(__unique_prefix_, G_PASTE(x, uniq))
#define NM_UNIQ __COUNTER__
/*****************************************************************************/
/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
* the argument possibly twice.
*
* Taken from systemd's MIN()/MAX() macros. */
#define NM_MIN(a, b) __NM_MIN(NM_UNIQ, a, NM_UNIQ, b)
#define __NM_MIN(aq, a, bq, b) \
({ \
typeof (a) NM_UNIQ_T(A, aq) = (a); \
typeof (b) NM_UNIQ_T(B, bq) = (b); \
((NM_UNIQ_T(A, aq) < NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
})
#define NM_MAX(a, b) __NM_MAX(NM_UNIQ, a, NM_UNIQ, b)
#define __NM_MAX(aq, a, bq, b) \
({ \
typeof (a) NM_UNIQ_T(A, aq) = (a); \
typeof (b) NM_UNIQ_T(B, bq) = (b); \
((NM_UNIQ_T(A, aq) > NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
})
#define NM_CLAMP(x, low, high) __NM_CLAMP(NM_UNIQ, x, NM_UNIQ, low, NM_UNIQ, high)
#define __NM_CLAMP(xq, x, lowq, low, highq, high) \
({ \
typeof(x)NM_UNIQ_T(X,xq) = (x); \
typeof(low) NM_UNIQ_T(LOW,lowq) = (low); \
typeof(high) NM_UNIQ_T(HIGH,highq) = (high); \
\
( (NM_UNIQ_T(X,xq) > NM_UNIQ_T(HIGH,highq)) \
? NM_UNIQ_T(HIGH,highq) \
: (NM_UNIQ_T(X,xq) < NM_UNIQ_T(LOW,lowq)) \
? NM_UNIQ_T(LOW,lowq) \
: NM_UNIQ_T(X,xq)); \
})
/*****************************************************************************/
static inline guint
nm_encode_version (guint major, guint minor, guint micro) {
/* analog to the preprocessor macro NM_ENCODE_VERSION(). */