Alternatives to nested switch-case?

I'm working with Irrlicht's GUI event handler and I found myself trapped in making nested switch-case statements. For example, the code show here is the outer nest and each helper function (e.g. onMenuItemSelected(), onButtonClicked(), onListBoxSelectedAgain(), etc.) may also contain nested switch-case statements. I have been using a helper function to define the body of each case. What alternatives are there to nested switch-case statements?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// Detects GUI events and determines actions.
    bool EditorGui::OnEvent( const irr::SEvent& event )
    {

        if ( event.EventType == irr::EET_GUI_EVENT )
        {
            // Detects gui id
            callerId = event.GUIEvent.Caller->getID();

            /// Detects events
            switch( event.GUIEvent.EventType )
            {
                /// If Menu Bar item was clicked
                case irr::gui::EGET_MENU_ITEM_SELECTED :
                {
                    return onMenuItemSelected( ( irr::gui::IGUIContextMenu* )event.GUIEvent.Caller );
                }

                /// If any button was clicked
                case irr::gui::EGET_BUTTON_CLICKED :
                {
                    return onButtonClicked( callerId );
                }

                /// Detect if a list item was double clicked
                case irr::gui::EGET_LISTBOX_SELECTED_AGAIN :
                {
                    return onListBoxSelectedAgain( callerId );
                }

                /// Detect if a table was double clicked
                case irr::gui::EGET_TABLE_SELECTED_AGAIN :
                {
                    return onTableSelectedAgain( callerId );
                }

                /// Detect tab that has mouse hover focus
                case irr::gui::EGET_ELEMENT_HOVERED :
                {
                    return onElementHovered( callerId );
                }

                /// Detect if tab control has changed
                case irr::gui::EGET_TAB_CHANGED :
                {
                    return onTabChanged();
                }

                default :
                    return false;
            }
        }

        return false;

    }


Example of helper function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// Does stuff when a Menu Bar item is selected.
    bool EditorGui::onMenuItemSelected( irr::gui::IGUIContextMenu* pMenuItem )
    {
        irr::s32 menuItemId = pMenuItem->getItemCommandId( pMenuItem->getSelectedItem() ); // Get id of calling menu item

        switch( menuItemId )
        {
            case GUID_FILE_NEWPROJECT :
            {
                return onFileNewProject();
            }

            case GUID_FILE_OPENPROJECT :
            {
                return onFileOpenProject();
            }

            case GUID_FILE_SAVEPROJECT :
            {
                return onFileSaveProject();
            }

            case GUID_FILE_EXIT :
            {
                onFileExit();
            }

            default :
                return false;
        }
    }
Last edited on
Topic archived. No new replies allowed.