summary refs log tree commit diff
path: root/src/libnm-glib-aux/nm-value-type.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnm-glib-aux/nm-value-type.h')
-rw-r--r--src/libnm-glib-aux/nm-value-type.h134
1 files changed, 102 insertions, 32 deletions
diff --git a/src/libnm-glib-aux/nm-value-type.h b/src/libnm-glib-aux/nm-value-type.h
index f1cce480..0addeec6 100644
--- a/src/libnm-glib-aux/nm-value-type.h
+++ b/src/libnm-glib-aux/nm-value-type.h
@@ -7,13 +7,27 @@
 #define __NM_VALUE_TYPE_H__
 
 typedef enum _nm_packed {
+    NM_VALUE_TYPE_NONE   = 0,
     NM_VALUE_TYPE_UNSPEC = 1,
     NM_VALUE_TYPE_BOOL   = 2,
     NM_VALUE_TYPE_INT32  = 3,
     NM_VALUE_TYPE_INT    = 4,
     NM_VALUE_TYPE_INT64  = 5,
-    NM_VALUE_TYPE_UINT64 = 6,
-    NM_VALUE_TYPE_STRING = 7,
+    NM_VALUE_TYPE_UINT32 = 6,
+    NM_VALUE_TYPE_UINT   = 7,
+    NM_VALUE_TYPE_UINT64 = 8,
+
+    /* Flags are for G_TYPE_FLAGS. That is, internally they are tracked
+     * as a guint, they have a g_param_spec_flags() property and they are
+     * serialized on D-Bus as "u". */
+    NM_VALUE_TYPE_FLAGS = 9,
+
+    /* G_TYPE_ENUM */
+    NM_VALUE_TYPE_ENUM = 10,
+
+    NM_VALUE_TYPE_STRING = 11,
+
+    NM_VALUE_TYPE_BYTES = 12,
 } NMValueType;
 
 /*****************************************************************************/
@@ -35,37 +49,37 @@ typedef union {
 
 } NMValueTypUnion;
 
-    /* Set the NMValueTypUnion. You can also assign the member directly.
+/* Set the NMValueTypUnion. You can also assign the member directly.
  * The only purpose of this is that it also returns a pointer to the
  * union. So, you can do
  *
  *   ptr = NM_VALUE_TYP_UNION_SET (&value_typ_union_storage, v_bool, TRUE);
  */
-    #define NM_VALUE_TYP_UNION_SET(_arg, _type, _val) \
-        ({                                            \
-            NMValueTypUnion *const _arg2 = (_arg);    \
-                                                      \
-            *_arg2 = (NMValueTypUnion){               \
-                ._type = (_val),                      \
-            };                                        \
-            _arg2;                                    \
-        })
+#define NM_VALUE_TYP_UNION_SET(_arg, _type, _val) \
+    ({                                            \
+        NMValueTypUnion *const _arg2 = (_arg);    \
+                                                  \
+        *_arg2 = (NMValueTypUnion){               \
+            ._type = (_val),                      \
+        };                                        \
+        _arg2;                                    \
+    })
 
 typedef struct {
     bool            has;
     NMValueTypUnion val;
 } NMValueTypUnioMaybe;
 
-    #define NM_VALUE_TYP_UNIO_MAYBE_SET(_arg, _type, _val) \
-        ({                                                 \
-            NMValueTypUnioMaybe *const _arg2 = (_arg);     \
-                                                           \
-            *_arg2 = (NMValueTypUnioMaybe){                \
-                .has       = TRUE,                         \
-                .val._type = (_val),                       \
-            };                                             \
-            _arg2;                                         \
-        })
+#define NM_VALUE_TYP_UNIO_MAYBE_SET(_arg, _type, _val) \
+    ({                                                 \
+        NMValueTypUnioMaybe *const _arg2 = (_arg);     \
+                                                       \
+        *_arg2 = (NMValueTypUnioMaybe){                \
+            .has       = TRUE,                         \
+            .val._type = (_val),                       \
+        };                                             \
+        _arg2;                                         \
+    })
 
 /*****************************************************************************/
 
@@ -80,16 +94,27 @@ nm_value_type_cmp(NMValueType value_type, gconstpointer p_a, gconstpointer p_b)
         NM_CMP_DIRECT(*((const gint32 *) p_a), *((const gint32 *) p_b));
         return 0;
     case NM_VALUE_TYPE_INT:
+    case NM_VALUE_TYPE_ENUM:
         NM_CMP_DIRECT(*((const int *) p_a), *((const int *) p_b));
         return 0;
     case NM_VALUE_TYPE_INT64:
         NM_CMP_DIRECT(*((const gint64 *) p_a), *((const gint64 *) p_b));
         return 0;
+    case NM_VALUE_TYPE_UINT32:
+        NM_CMP_DIRECT(*((const guint32 *) p_a), *((const guint32 *) p_b));
+        return 0;
+    case NM_VALUE_TYPE_UINT:
+    case NM_VALUE_TYPE_FLAGS:
+        NM_CMP_DIRECT(*((const guint *) p_a), *((const guint *) p_b));
+        return 0;
     case NM_VALUE_TYPE_UINT64:
         NM_CMP_DIRECT(*((const guint64 *) p_a), *((const guint64 *) p_b));
         return 0;
     case NM_VALUE_TYPE_STRING:
         return nm_strcmp0(*((const char *const *) p_a), *((const char *const *) p_b));
+    case NM_VALUE_TYPE_BYTES:
+        return nm_g_bytes_equal0(*((const GBytes *const *) p_a), *((const GBytes *const *) p_b));
+    case NM_VALUE_TYPE_NONE:
     case NM_VALUE_TYPE_UNSPEC:
         break;
     }
@@ -114,21 +139,39 @@ nm_value_type_copy(NMValueType value_type, gpointer dst, gconstpointer src)
         (*((gint32 *) dst) = *((const gint32 *) src));
         return;
     case NM_VALUE_TYPE_INT:
+    case NM_VALUE_TYPE_ENUM:
         (*((int *) dst) = *((const int *) src));
         return;
     case NM_VALUE_TYPE_INT64:
         (*((gint64 *) dst) = *((const gint64 *) src));
         return;
+    case NM_VALUE_TYPE_UINT32:
+        (*((guint32 *) dst) = *((const guint32 *) src));
+        return;
+    case NM_VALUE_TYPE_UINT:
+    case NM_VALUE_TYPE_FLAGS:
+        (*((guint *) dst) = *((const guint *) src));
+        return;
     case NM_VALUE_TYPE_UINT64:
         (*((guint64 *) dst) = *((const guint64 *) src));
         return;
     case NM_VALUE_TYPE_STRING:
         /* self assignment safe! */
         if (*((char **) dst) != *((const char *const *) src)) {
-            g_free(*((char **) dst));
+            _nm_unused char *old = *((char **) dst);
+
             *((char **) dst) = g_strdup(*((const char *const *) src));
         }
         return;
+    case NM_VALUE_TYPE_BYTES:
+        /* self assignment safe! */
+        if (*((GBytes **) dst) != *((const GBytes *const *) src)) {
+            _nm_unused gs_unref_bytes GBytes *old = *((GBytes **) dst);
+
+            *((GBytes **) dst) = g_bytes_ref(*((GBytes *const *) src));
+        }
+        return;
+    case NM_VALUE_TYPE_NONE:
     case NM_VALUE_TYPE_UNSPEC:
         break;
     }
@@ -151,12 +194,16 @@ nm_value_type_get_from_variant(NMValueType value_type,
     case NM_VALUE_TYPE_INT64:
         *((gint64 *) dst) = g_variant_get_int64(variant);
         return;
+    case NM_VALUE_TYPE_UINT32:
+        *((guint32 *) dst) = g_variant_get_uint32(variant);
+        return;
     case NM_VALUE_TYPE_UINT64:
         *((guint64 *) dst) = g_variant_get_uint64(variant);
         return;
     case NM_VALUE_TYPE_STRING:
         if (clone) {
-            g_free(*((char **) dst));
+            _nm_unused gs_free char *old = *((char **) dst);
+
             *((char **) dst) = g_variant_dup_string(variant, NULL);
         } else {
             /* we don't clone the string, nor free the previous value. */
@@ -164,11 +211,16 @@ nm_value_type_get_from_variant(NMValueType value_type,
         }
         return;
 
+    case NM_VALUE_TYPE_BYTES:
     case NM_VALUE_TYPE_INT:
-        /* "int" also does not have a define variant type, because it's not
-         * clear how many bits we would need. */
+    case NM_VALUE_TYPE_UINT:
+    case NM_VALUE_TYPE_ENUM:
+    case NM_VALUE_TYPE_FLAGS:
+        /* These types don't have a defined variant type, because it's not
+         * clear how many bits we would need or how to handle the type. */
 
         /* fall-through */
+    case NM_VALUE_TYPE_NONE:
     case NM_VALUE_TYPE_UNSPEC:
         break;
     }
@@ -178,7 +230,8 @@ nm_value_type_get_from_variant(NMValueType value_type,
 static inline GVariant *
 nm_value_type_to_variant(NMValueType value_type, gconstpointer src)
 {
-    const char *v_string;
+    const char *  v_string;
+    const GBytes *v_bytes;
 
     switch (value_type) {
     case NM_VALUE_TYPE_BOOL:
@@ -187,17 +240,26 @@ nm_value_type_to_variant(NMValueType value_type, gconstpointer src)
         return g_variant_new_int32(*((const gint32 *) src));
     case NM_VALUE_TYPE_INT64:
         return g_variant_new_int64(*((const gint64 *) src));
+    case NM_VALUE_TYPE_UINT32:
+        return g_variant_new_uint32(*((const guint32 *) src));
     case NM_VALUE_TYPE_UINT64:
         return g_variant_new_uint64(*((const guint64 *) src));
     case NM_VALUE_TYPE_STRING:
         v_string = *((const char *const *) src);
         return v_string ? g_variant_new_string(v_string) : NULL;
+    case NM_VALUE_TYPE_BYTES:
+        v_bytes = *((const GBytes *const *) src);
+        return v_bytes ? nm_g_bytes_to_variant_ay(v_bytes) : NULL;
 
     case NM_VALUE_TYPE_INT:
-        /* "int" also does not have a define variant type, because it's not
-         * clear how many bits we would need. */
+    case NM_VALUE_TYPE_UINT:
+    case NM_VALUE_TYPE_ENUM:
+    case NM_VALUE_TYPE_FLAGS:
+        /* These types don't have a defined variant type, because it's not
+         * clear how many bits we would need or how to handle the type. */
 
         /* fall-through */
+    case NM_VALUE_TYPE_NONE:
     case NM_VALUE_TYPE_UNSPEC:
         break;
     }
@@ -215,16 +277,24 @@ nm_value_type_get_variant_type(NMValueType value_type)
         return G_VARIANT_TYPE_INT32;
     case NM_VALUE_TYPE_INT64:
         return G_VARIANT_TYPE_INT64;
+    case NM_VALUE_TYPE_UINT32:
+        return G_VARIANT_TYPE_UINT32;
     case NM_VALUE_TYPE_UINT64:
         return G_VARIANT_TYPE_UINT64;
     case NM_VALUE_TYPE_STRING:
         return G_VARIANT_TYPE_STRING;
+    case NM_VALUE_TYPE_BYTES:
+        return G_VARIANT_TYPE_BYTESTRING;
 
     case NM_VALUE_TYPE_INT:
-        /* "int" also does not have a define variant type, because it's not
-         * clear how many bits we would need. */
+    case NM_VALUE_TYPE_UINT:
+    case NM_VALUE_TYPE_ENUM:
+    case NM_VALUE_TYPE_FLAGS:
+        /* These types don't have a defined variant type, because it's not
+         * clear how many bits we would need or how to handle the type. */
 
         /* fall-through */
+    case NM_VALUE_TYPE_NONE:
     case NM_VALUE_TYPE_UNSPEC:
         break;
     }
@@ -232,7 +302,7 @@ nm_value_type_get_variant_type(NMValueType value_type)
     return NULL;
 }
 
-    /*****************************************************************************/
+/*****************************************************************************/
 
 #endif /* NM_VALUE_TYPE_DEFINE_FUNCTIONS */