[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 24 of 32 RFC] libxl: add new "defbool" built in type



# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1326470399 0
# Node ID 7e9f3ce2cd1f05ae30727bed05f295c7fdfbb2ea
# Parent  0d3abdb6c01894e4e07400317a0b49433dbaf1a5
libxl: add new "defbool" built in type.

This type is a but like a "boolean" but with a third state "default" (so really
I suppose its a tristate).

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/gentest.py
--- a/tools/libxl/gentest.py    Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/gentest.py    Fri Jan 13 15:59:59 2012 +0000
@@ -19,7 +19,8 @@ def randomize_case(s):
 def randomize_enum(e):
     return random.choice([v.name for v in e.values])
 
-handcoded = ["libxl_cpumap", "libxl_key_value_list",
+handcoded = ["libxl_defbool", # Temp until a user appears in the next patch
+             "libxl_cpumap", "libxl_key_value_list",
              "libxl_cpuid_policy_list", "libxl_file_reference",
              "libxl_string_list", "libxl_cpuarray"]
 
@@ -54,6 +55,8 @@ def gen_rand_init(ty, v, indent = "    "
               ty.pass_arg(v, parent is None))
     elif ty.typename in ["bool"]:
         s += "%s = rand() %% 2;\n" % v
+    elif ty.typename in ["libxl_defbool"]:
+        s += "libxl_defbool_set(%s, !!rand() %% 1);\n" % v
     elif ty.typename in ["char *"]:
         s += "%s = rand_str();\n" % v
     elif ty.private:
diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/libxl.c       Fri Jan 13 15:59:59 2012 +0000
@@ -126,6 +126,47 @@ void libxl_key_value_list_dispose(libxl_
     free(kvl);
 }
 
+#define LIBXL__DEFBOOL_DEFAULT (0)
+#define LIBXL__DEFBOOL_FALSE (-1)
+#define LIBXL__DEFBOOL_TRUE (1)
+
+void libxl_defbool_set(libxl_defbool *db, bool b)
+{
+    db->val = b ? LIBXL__DEFBOOL_TRUE : LIBXL__DEFBOOL_FALSE;
+}
+
+void libxl_defbool_unset(libxl_defbool *db)
+{
+    db->val = LIBXL__DEFBOOL_DEFAULT;
+}
+
+bool libxl_defbool_is_default(libxl_defbool db)
+{
+    return !db.val;
+}
+
+void libxl_defbool_setdefault(libxl_defbool *db, bool b)
+{
+    if (libxl_defbool_is_default(*db))
+        libxl_defbool_set(db, b);
+}
+
+bool libxl_defbool_val(libxl_defbool db)
+{
+    assert(!libxl_defbool_is_default(db));
+    return db.val > 0;
+}
+
+const char *libxl_defbool_to_string(libxl_defbool b)
+{
+    if (b.val < 0)
+        return "False";
+    else if (b.val > 0)
+        return "True";
+    else
+        return "<default>";
+}
+
 
/******************************************************************************/
 
 
diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/libxl.h
--- a/tools/libxl/libxl.h       Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/libxl.h       Fri Jan 13 15:59:59 2012 +0000
@@ -199,6 +199,30 @@ typedef struct {
     int v;
 } libxl_enum_string_table;
 
+/*
+ * A boolean variable with an explicit default state.
+ *
+ * Users should treat this struct as opaque and use the following
+ * defined macros and accessor functions.
+ *
+ * To allow users of the library to naively select all defaults this
+ * state is represented as 0. False is < 0 and True is > 0.
+ */
+typedef struct {
+    int val;
+} libxl_defbool;
+
+void libxl_defbool_set(libxl_defbool *db, bool b);
+/* Resets to default */
+void libxl_defbool_unset(libxl_defbool *db);
+/* Sets db only if it is currently == default */
+void libxl_defbool_setdefault(libxl_defbool *db, bool b);
+bool libxl_defbool_is_default(libxl_defbool db);
+/* db must not be == default */
+bool libxl_defbool_val(libxl_defbool db);
+
+const char *libxl_defbool_to_string(libxl_defbool b);
+
 typedef struct libxl__ctx libxl_ctx;
 
 #include "_libxl_types.h"
diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/libxl_json.c
--- a/tools/libxl/libxl_json.c  Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/libxl_json.c  Fri Jan 13 15:59:59 2012 +0000
@@ -87,6 +87,12 @@ yajl_gen_status libxl__yajl_gen_enum(yaj
 /*
  * YAJL generators for builtin libxl types.
  */
+yajl_gen_status libxl_defbool_gen_json(yajl_gen hand,
+                                       libxl_defbool *db)
+{
+    return libxl__yajl_gen_asciiz(hand, libxl_defbool_to_string(*db));
+}
+
 yajl_gen_status libxl_uuid_gen_json(yajl_gen hand,
                                     libxl_uuid *uuid)
 {
diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/libxl_types.idl
--- a/tools/libxl/libxl_types.idl       Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/libxl_types.idl       Fri Jan 13 15:59:59 2012 +0000
@@ -5,6 +5,8 @@
 
 namespace("libxl_")
 
+libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE)
+
 libxl_domid = Builtin("domid", json_fn = "yajl_gen_integer", autogenerate_json 
= False)
 libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
 libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/libxlu_cfg.c
--- a/tools/libxl/libxlu_cfg.c  Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/libxlu_cfg.c  Fri Jan 13 15:59:59 2012 +0000
@@ -235,6 +235,17 @@ int xlu_cfg_get_long(const XLU_Config *c
     return 0;
 }
 
+int xlu_cfg_get_defbool(const XLU_Config *cfg, const char *n, libxl_defbool *b,
+                     int dont_warn)
+{
+    int ret;
+    long l;
+
+    ret = xlu_cfg_get_long(cfg, n, &l, dont_warn);
+    if (ret) return ret;
+    libxl_defbool_set(b, !!l);
+    return 0;
+}
 
 int xlu_cfg_get_list(const XLU_Config *cfg, const char *n,
                      XLU_ConfigList **list_r, int *entries_r, int dont_warn) {
diff -r 0d3abdb6c018 -r 7e9f3ce2cd1f tools/libxl/libxlutil.h
--- a/tools/libxl/libxlutil.h   Thu Jan 12 17:11:49 2012 +0000
+++ b/tools/libxl/libxlutil.h   Fri Jan 13 15:59:59 2012 +0000
@@ -52,6 +52,8 @@ int xlu_cfg_replace_string(const XLU_Con
                            char **value_r, int dont_warn);
 int xlu_cfg_get_long(const XLU_Config*, const char *n, long *value_r,
                      int dont_warn);
+int xlu_cfg_get_defbool(const XLU_Config*, const char *n, libxl_defbool *b,
+                     int dont_warn);
 
 int xlu_cfg_get_list(const XLU_Config*, const char *n,
                      XLU_ConfigList **list_r /* may be 0 */,

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.