From 0dc5ea241253eb13bd7402fa37e338552f02380b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 10 Jan 2021 11:44:25 +0100 Subject: [PATCH] shared: add NMOptionBool as alternative to NMTernary NMTernary is part of libnm's public API. It thus cannot be used by code without libnm/libnm-core dependency. Add another enum with the same purpose. The name "NMTernary" is already taken, and we should not use some macro trickery to use (effectively) different types under the same name. Another possible name would be "NMTern", but for no strong reasons we choose NMOptionBool. The naming reminds of rust's std::option::Option. --- libnm-core/nm-core-internal.h | 18 ++++++++++++++++++ shared/nm-glib-aux/nm-shared-utils.h | 12 ++++++++++++ 2 files changed, 30 insertions(+) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 01b3a55db6..afdd2f2fc1 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -230,6 +230,24 @@ _NM_SETTING_WIRED_WAKE_ON_LAN_CAST(NMSettingWiredWakeOnLan v) /*****************************************************************************/ +static inline NMTernary +NM_TERNARY_FROM_OPTION_BOOL(NMOptionBool v) +{ + nm_assert(NM_IN_SET(v, NM_OPTION_BOOL_DEFAULT, NM_OPTION_BOOL_TRUE, NM_OPTION_BOOL_FALSE)); + + return (NMTernary) v; +} + +static inline NMOptionBool +NM_TERNARY_TO_OPTION_BOOL(NMTernary v) +{ + nm_assert(NM_IN_SET(v, NM_TERNARY_DEFAULT, NM_TERNARY_TRUE, NM_TERNARY_FALSE)); + + return (NMOptionBool) v; +} + +/*****************************************************************************/ + typedef enum { /*< skip >*/ NM_SETTING_PARSE_FLAGS_NONE = 0, NM_SETTING_PARSE_FLAGS_STRICT = 1LL << 0, diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 669c9fbcb4..8f0d50c9f5 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -8,6 +8,18 @@ #include +/*****************************************************************************/ + +/* An optional boolean (like NMTernary, with identical numerical + * enum values). Note that this enum type is _nm_packed! */ +typedef enum _nm_packed { + NM_OPTION_BOOL_DEFAULT = -1, + NM_OPTION_BOOL_FALSE = 0, + NM_OPTION_BOOL_TRUE = 1, +} NMOptionBool; + +/*****************************************************************************/ + static inline gboolean nm_is_ascii(char ch) {