Re: How to redesign big switch case?

From: Razvan Popovici (me_at_razvan_nospam.de)
Date: 03/22/04


Date: Mon, 22 Mar 2004 17:26:19 +0100


"Siemel Naran" <SiemelNaran@REMOVE.att.net> schrieb im Newsbeitrag
news:Ggu7c.14785$PY1.328948@bgtnsc05-news.ops.worldnet.att.net...
> How to redesign big switch case?
>
> I wrote an editor that reads chars, and does a switch (char) to process
the
> character. For example, if char is HOME then go to the beginning of the
> line, and if char is CTRL-RIGHT then go to the start of the next word. As
I
> added more cases and support for reverse incremental history mode, the
code
> grew longer and longer. It is now 562 lines, or 58% of the entire file.
> This seems a bit clumsy. Fortunately the code does not have any goto
> statements and seems pretty structured and clear to me, so it's not
exactly
> sphaghetti code. Is it worthwhile to change the code? If yes, then how?
> Finally, it would be nice to allow users to derive their own editor class
> from my one and add support for more special keys.
>
> Thanks.
>
>
Hi,

What about if you would write for each keystroke class a function? Class I
mean you will maybe threat the keys from 0 to 9 as a set, same for the
letters A to Z.
If you go OOP, then these functions need to be members of your class and
virtual, so you would be able to add more functionality to your object
though derivation.

Draft (sorry for writting the functions inline, no access specificators,
etc, etc, don't do it in "real"):

class CMyEdit
{
    virtual void onKey(Key k)
    {
       switch(k)
        {
               case PGDOWN:
                    onPgDn();
                    break:
                case '0'..'9':
                    onNumberKey(k);
                    break;
                // and so on with each
event/class............................
        }
    }
    virtual void onPgDn()
    {
        // do page down
    }
    virtual void onNumberKey(Key k)
    {
        // do page down
    }
   // and so on with each event function
};

Therefore, if you will need to write a new editor that works same as the old
one but beeps on each key and log each PgDn, then you write:

class CMyEditBeepLog: public CMyEdit
{
    virtual void onKey(Key k)
    {
        doBeep();
        CMyEdit::onKey(k);
    }

    virtual void onPgDn()
    {
        doLog();
        CMyEdit::onPgDn();
    }
};

In my oppinion using stl as suggested above won't bring much more
clarity/reusability.

Regards,
Razvan



Relevant Pages

  • How to redesign big switch case?
    ... I wrote an editor that reads chars, and does a switch to process the ... from my one and add support for more special keys. ...
    (comp.lang.cpp)
  • Re: How to redesign big switch case?
    ... > I wrote an editor that reads chars, and does a switch to process ... if char is HOME then go to the beginning of the ... it would be nice to allow users to derive their own editor class ... > from my one and add support for more special keys. ...
    (comp.lang.cpp)
  • Re: How to redesign big switch case?
    ... > I wrote an editor that reads chars, and does a switch to process the ... if char is HOME then go to the beginning of the ... > from my one and add support for more special keys. ...
    (comp.lang.cpp)
  • Re: Are _T() and TEXT() macros equivalent?
    ... are not concerned if a char takes 1 byte or 100. ... since the language defines an object ... various ramifications of using and not using the switch. ... Why the fixation on basing the meaning of the keyword "char" on a compiler ...
    (microsoft.public.vc.mfc)
  • Re: Nice problem to solve: switch via a table
    ... Implement a switch statement in Forth. ... char a char e char i char o char u char y ... Say we instead build something like a jump table. ... But I'm not sure that Forth needs giant switch statements ...
    (comp.lang.forth)