[virt-tools-list] [PATCH virt-viewer 01/11] ovirt-foreign-menu: Rework states logic

Eduardo Lima (Etrunko) etrunko at redhat.com
Mon Jul 18 02:13:01 UTC 2016


Use switch/case instead of lots of conditional blocks

Signed-off-by: Eduardo Lima (Etrunko) <etrunko at redhat.com>
---
 src/ovirt-foreign-menu.c | 76 +++++++++++++++++++++++-------------------------
 1 file changed, 36 insertions(+), 40 deletions(-)

diff --git a/src/ovirt-foreign-menu.c b/src/ovirt-foreign-menu.c
index 33ff4f1..b0b8fec 100644
--- a/src/ovirt-foreign-menu.c
+++ b/src/ovirt-foreign-menu.c
@@ -312,51 +312,47 @@ ovirt_foreign_menu_next_async_step(OvirtForeignMenu *menu,
     g_return_if_fail(current_state >= STATE_0);
     g_return_if_fail(current_state < STATE_ISOS);
 
-    current_state++;
-
-    if (current_state == STATE_API) {
-        if (menu->priv->api == NULL) {
-            ovirt_foreign_menu_fetch_api_async(menu);
-        } else {
-            current_state++;
+    switch (++current_state) {
+        case STATE_API: {
+            if (menu->priv->api == NULL) {
+                ovirt_foreign_menu_fetch_api_async(menu);
+                break;
+            }
         }
-    }
-
-    if (current_state == STATE_VM) {
-        if (menu->priv->vm == NULL) {
-            ovirt_foreign_menu_fetch_vm_async(menu);
-        } else {
-            current_state++;
+        case STATE_VM: {
+            if (menu->priv->vm == NULL) {
+                ovirt_foreign_menu_fetch_vm_async(menu);
+                break;
+            }
         }
-    }
-
-    if (current_state == STATE_STORAGE_DOMAIN) {
-        if (menu->priv->files == NULL) {
-            ovirt_foreign_menu_fetch_storage_domain_async(menu);
-        } else {
-            current_state++;
+        case STATE_STORAGE_DOMAIN: {
+            if (menu->priv->files == NULL) {
+                ovirt_foreign_menu_fetch_storage_domain_async(menu);
+                break;
+            }
         }
-    }
-
-    if (current_state == STATE_VM_CDROM) {
-        if (menu->priv->cdrom == NULL) {
-            ovirt_foreign_menu_fetch_vm_cdrom_async(menu);
-        } else {
-            current_state++;
+        case STATE_VM_CDROM: {
+            if (menu->priv->cdrom == NULL) {
+                ovirt_foreign_menu_fetch_vm_cdrom_async(menu);
+                break;
+            }
         }
-    }
-
-    if (current_state == STATE_CDROM_FILE) {
-        ovirt_foreign_menu_refresh_cdrom_file_async(menu);
-    }
-
-    if (current_state == STATE_ISOS) {
-        g_warn_if_fail(menu->priv->api != NULL);
-        g_warn_if_fail(menu->priv->vm != NULL);
-        g_warn_if_fail(menu->priv->files != NULL);
-        g_warn_if_fail(menu->priv->cdrom != NULL);
+        case STATE_CDROM_FILE: {
+            ovirt_foreign_menu_refresh_cdrom_file_async(menu);
+            break;
+        }
+        case STATE_ISOS: {
+            g_warn_if_fail(menu->priv->api != NULL);
+            g_warn_if_fail(menu->priv->vm != NULL);
+            g_warn_if_fail(menu->priv->files != NULL);
+            g_warn_if_fail(menu->priv->cdrom != NULL);
 
-        ovirt_foreign_menu_refresh_iso_list(menu);
+            ovirt_foreign_menu_refresh_iso_list(menu);
+            break;
+        }
+        default: {
+            g_warn_if_reached();
+        }
     }
 }
 
-- 
2.7.4




More information about the virt-tools-list mailing list