All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Campbell <ian.campbell@citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Jackson <Ian.Jackson@eu.citrix.com>
Subject: [PATCH 6 of 6 V2] libxl: Keyed unions key off an enum instead of an arbitrary expression
Date: Mon, 18 Jul 2011 14:57:15 +0100	[thread overview]
Message-ID: <3e9a884f93b77ba3b885.1310997435@localhost.localdomain> (raw)
In-Reply-To: <patchbomb.1310997429@localhost.localdomain>

# HG changeset patch
# User Ian Campbell <ian.campbell@citrix.com>
# Date 1310997150 -3600
# Node ID 3e9a884f93b77ba3b885dcd99518aac267d97ff8
# Parent  686d683a3a57f7a1b315c49e10d5e60ec4d11133
libxl: Keyed unions key off an enum instead of an arbitrary expression

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

diff -r 686d683a3a57 -r 3e9a884f93b7 tools/libxl/gentypes.py
--- a/tools/libxl/gentypes.py	Mon Jul 18 14:52:30 2011 +0100
+++ b/tools/libxl/gentypes.py	Mon Jul 18 14:52:30 2011 +0100
@@ -81,12 +81,13 @@ def libxl_C_type_destroy(ty, v, indent =
     if isinstance(ty, libxltypes.KeyedUnion):
         if parent is None:
             raise Exception("KeyedUnion type must have a parent")
+        s += "switch (%s) {\n" % (parent + ty.keyvar_name)        
         for f in ty.fields:
             (nparent,fexpr) = ty.member(v, f, parent is None)
-            keyvar_expr = f.keyvar_expr % (parent + ty.keyvar_name)
-            s += "if (" + keyvar_expr + ") {\n"
+            s += "case %s:\n" % f.enumname
             s += libxl_C_type_destroy(f.type, fexpr, indent + "    ", nparent)
-            s += "}\n"
+            s += "    break;\n"
+        s += "}\n"
     elif isinstance(ty, libxltypes.Struct) and (parent is None or ty.destructor_fn is None):
         for f in [f for f in ty.fields if not f.const]:
             (nparent,fexpr) = ty.member(v, f, parent is None)
diff -r 686d683a3a57 -r 3e9a884f93b7 tools/libxl/idl.txt
--- a/tools/libxl/idl.txt	Mon Jul 18 14:52:30 2011 +0100
+++ b/tools/libxl/idl.txt	Mon Jul 18 14:52:30 2011 +0100
@@ -127,13 +127,9 @@ libxltype.KeyedUnion
  upon another member in the containing type.
 
  The KeyedUnion.keyvar_name must contain the name of the member of the
- containing type which determines the valid member of the union
-
- The fields in a KeyedUnion have an extra Field.keyvar_expr
- property. This must be a string containing a single "%s" format
- specifier such that when "%s" is substited by an instance of
- KeyedUnion.keyvar_name it becomes a C expression which evaluates to
- True IFF this field currently contains valid data.
+ containing type which determines the valid member of the union. The
+ member referenced by KeyedUnion.keyvar_name has type
+ KeyedUnion.keyvar_type which must be an instance of the Enumeration type.
 
 Standard Types
 --------------
diff -r 686d683a3a57 -r 3e9a884f93b7 tools/libxl/libxl.idl
--- a/tools/libxl/libxl.idl	Mon Jul 18 14:52:30 2011 +0100
+++ b/tools/libxl/libxl.idl	Mon Jul 18 14:52:30 2011 +0100
@@ -159,30 +159,28 @@ libxl_domain_build_info = Struct("domain
     ("disable_migrate", bool),
     ("cpuid",           libxl_cpuid_policy_list),
     ("type",            libxl_domain_type),
-    ("u", KeyedUnion(None, "type",
-                [("hvm", "%s == LIBXL_DOMAIN_TYPE_HVM", Struct(None,
-                                       [("firmware", string),
-                                        ("pae", bool),
-                                        ("apic", bool),
-                                        ("acpi", bool),
-                                        ("nx", bool),
-                                        ("viridian", bool),
-                                        ("timeoffset", string),
-                                        ("hpet", bool),
-                                        ("vpt_align", bool),
-                                        ("timer_mode", integer),
-                                        ("nested_hvm", bool),
-                                        ])),
-                 ("pv", "%s == LIBXL_DOMAIN_TYPE_PV", Struct(None,
-                                       [("kernel", libxl_file_reference),
-                                        ("slack_memkb", uint32),
-                                        ("bootloader", string),
-                                        ("bootloader_args", string),
-                                        ("cmdline", string),
-                                        ("ramdisk", libxl_file_reference),
-                                        ("features", string, True),
-                                        ("e820_host", bool, False, "Use host's E820 for PCI passthrough."),
-                                        ])),
+    ("u", KeyedUnion(None, libxl_domain_type, "type",
+                [("hvm", Struct(None, [("firmware", string),
+                                       ("pae", bool),
+                                       ("apic", bool),
+                                       ("acpi", bool),
+                                       ("nx", bool),
+                                       ("viridian", bool),
+                                       ("timeoffset", string),
+                                       ("hpet", bool),
+                                       ("vpt_align", bool),
+                                       ("timer_mode", integer),
+                                       ("nested_hvm", bool),
+                                       ])),
+                 ("pv", Struct(None, [("kernel", libxl_file_reference),
+                                      ("slack_memkb", uint32),
+                                      ("bootloader", string),
+                                      ("bootloader_args", string),
+                                      ("cmdline", string),
+                                      ("ramdisk", libxl_file_reference),
+                                      ("features", string, True),
+                                      ("e820_host", bool, False, "Use host's E820 for PCI passthrough."),
+                                      ])),
                  ])),
     ],
     comment =
diff -r 686d683a3a57 -r 3e9a884f93b7 tools/libxl/libxltypes.py
--- a/tools/libxl/libxltypes.py	Mon Jul 18 14:52:30 2011 +0100
+++ b/tools/libxl/libxltypes.py	Mon Jul 18 14:52:30 2011 +0100
@@ -116,6 +116,11 @@ class Enumeration(Type):
             self.values.append(EnumerationValue(self, num, name,
                                                 comment=comment,
                                                 typename=self.rawname))
+    def lookup(self, name):
+        for v in self.values:
+            if v.valuename == str.upper(name):
+                return v
+        return ValueError
         
 class Field(object):
     """An element of an Aggregate type"""
@@ -124,7 +129,7 @@ class Field(object):
         self.name = name
         self.const = kwargs.setdefault('const', False)
         self.comment = kwargs.setdefault('comment', None)
-        self.keyvar_expr = kwargs.setdefault('keyvar_expr', None)
+        self.enumname = kwargs.setdefault('enumname', None)
 
 class Aggregate(Type):
     """A type containing a collection of other types"""
@@ -181,18 +186,21 @@ class Union(Aggregate):
 
 class KeyedUnion(Aggregate):
     """A union which is keyed of another variable in the parent structure"""
-    def __init__(self, name, keyvar_name, fields, **kwargs):
+    def __init__(self, name, keyvar_type, keyvar_name, fields, **kwargs):
         Aggregate.__init__(self, "union", name, [], **kwargs)
 
+        if not isinstance(keyvar_type, Enumeration):
+            raise ValueError
+        
         self.keyvar_name = keyvar_name
+        self.keyvar_type = keyvar_type
 
         for f in fields:
-            # (name, keyvar_expr, type)
-
-            # keyvar_expr must contain exactly one %s which will be replaced with the keyvar_name
-
-            n, kve, ty = f
-            self.fields.append(Field(ty, n, keyvar_expr=kve))
+            # (name, enum, type)
+            e, ty = f
+            ev = keyvar_type.lookup(e)
+            en = ev.name
+            self.fields.append(Field(ty, e, enumname=en))
 
 #
 # Standard Types

  parent reply	other threads:[~2011-07-18 13:57 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-12 18:37 [RFC 0/3] libxl event handling Ian Jackson
2011-07-12 18:37 ` [PATCH 1/3] RFC: libxl: API changes re domain type (and keyed union semantics) Ian Jackson
2011-07-12 18:37   ` [PATCH 2/3] RFC: libxl: API changes re event handling Ian Jackson
2011-07-12 18:37     ` [PATCH 3/3] RFC: libxl: internal API for " Ian Jackson
2011-07-13 10:22       ` Ian Campbell
2011-07-13 10:21     ` [PATCH 2/3] RFC: libxl: API changes re " Ian Campbell
2011-07-13 14:03       ` Ian Jackson
2011-07-13 16:08         ` Ian Campbell
2011-07-13 16:17           ` Ian Jackson
2011-07-13 16:33             ` Ian Campbell
2011-07-13 17:04               ` Ian Jackson
2011-07-13  9:19   ` [PATCH 1/3] RFC: libxl: API changes re domain type (and keyed union semantics) Ian Campbell
2011-07-15 12:45   ` Ian Campbell
2011-07-15 13:13     ` Ian Jackson
2011-07-18  9:06       ` [PATCH 0 of 6] libxl: IDL improvements Ian Campbell
2011-07-18  9:06         ` [PATCH 1 of 6] libxl: Give the HVM domain type the name "HVM" Ian Campbell
2011-07-18  9:06         ` [PATCH 2 of 6] libxl: replace libxl__domain_is_hvm with libxl__domain_type Ian Campbell
2011-07-18  9:06         ` [PATCH 3 of 6] libxl: specify HVM vs PV in build_info using libxl_domain_type enum Ian Campbell
2011-07-18  9:06         ` [PATCH 4 of 6] libxl: specify HVM vs PV in create_info " Ian Campbell
2011-07-18  9:06         ` [PATCH 5 of 6] libxl: use libxl_domain_type enum with libxl__domain_suspend_common Ian Campbell
2011-07-18  9:06         ` [PATCH 6 of 6] libxl: Keyed unions key off an enum instead of an arbitrary expression Ian Campbell
2011-07-18 13:57       ` [PATCH 0 of 6 V2] libxl: IDL improvements Ian Campbell
2011-07-18 13:57         ` [PATCH 1 of 6 V2] libxl: Give the HVM domain type the name "HVM" Ian Campbell
2011-07-18 14:56           ` Wei LIU
2011-07-18 15:20             ` Ian Jackson
2011-07-18 13:57         ` [PATCH 2 of 6 V2] libxl: replace libxl__domain_is_hvm with libxl__domain_type Ian Campbell
2011-07-18 13:57         ` [PATCH 3 of 6 V2] libxl: specify HVM vs PV in build_info using libxl_domain_type enum Ian Campbell
2011-07-18 13:57         ` [PATCH 4 of 6 V2] libxl: specify HVM vs PV in create_info " Ian Campbell
2011-07-18 13:57         ` [PATCH 5 of 6 V2] libxl: use libxl_domain_type enum with libxl__domain_suspend_common Ian Campbell
2011-07-18 13:57         ` Ian Campbell [this message]
2011-07-18 16:11         ` [PATCH 0 of 6 V2] libxl: IDL improvements Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3e9a884f93b77ba3b885.1310997435@localhost.localdomain \
    --to=ian.campbell@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.