#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Question Suggestion for a menu programming structure


    I want to design a generic menu structure for easy management etc.

    I have something in mind like this:

    1- I plan to define a structure that fits into all menu item object types. It will contain the required values and functions. Then when i need a new menu item i'll allocate memory for it, set its parameters and add it to a dynamic list(like linked list). Then the menu engine will use this object.

    example structure:

    typedef struct MENU_ITEMS
    {
    unsigned int ID; // ID
    int value; // Some value
    void(*Draw)(void *); // Generic draw function
    }MENU;

    But there's a problem. Every menu item is not the same so they may have different parameters and most importantly they may have different draw functions with different number of paramaters and types. I want suggestions. How can I create such a structure for this kind of application that is flexible enough to expand easily and easily be managed. Thanks, i'll appreciate your ideas...
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    How about
    Code:
    typedef struct MENU_ITEMS
    {
        unsigned int ID; // ID
        int value; // Some value
        void *drawData;  // pointer to draw-specific data
        void(*Draw)(void *); // Generic draw function
    }MENU;
    So you call
    foo->Draw(foo->drawData);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by salem
    How about
    Code:
    typedef struct MENU_ITEMS
    {
        unsigned int ID; // ID
        int value; // Some value
        void *drawData;  // pointer to draw-specific data
        void(*Draw)(void *); // Generic draw function
    }MENU;
    So you call
    foo->Draw(foo->drawData);
    So you say; define function parameters as another member of the struct and it may point to another struct where the draw function has different number and type of structs so you get the flexibility you want. Thanks, this may be a good idea. Any other suggestions? Especially for this kind of applications don't stick with my idea. May be there's a whole different approach which is more elegant?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The problem with C is strong typing and exact declarations, though that is also its blessing. But it does get in the way of some things we might like to do, like have a function with variable numbers of arguments.

    What salem suggested is a simple and elegant solution that is used in multithreading, such that a standard thread function format can be declared that can handle any number and type of arguments via the void pointer.

    There are also variable length argument lists (as they're called in the index in K&R's The C Programming Language (2nd ed.). They're declared in stdarg.h and use a va_list datatype and functions va_list, va_end, and va_arg. A page that describes how to use them (assuming you don't have K&R's book) is at http://www.techpulp.com/blog/2008/11...rguments-in-c/, though I'm sure with the keywords I just gave you you could Google for lots more examples and explanations.

    I've never used va_arg's myself (learned about them in C class 23 years ago and never looked at them again until just now) and, from what I see, the approach borrowed from multithreading looks a lot simpler, cleaner, and more elegant.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by dwise1_aol
    I've never used va_arg's myself (learned about them in C class 23 years ago and never looked at them again until just now) and, from what I see, the approach borrowed from multithreading looks a lot simpler, cleaner, and more elegant.
    Hmm i've never paid attention to the argument list although i've seen it. It's an interesting idea, like overloading in c++ but as you suggested its usage seems a bit cumbersome. If we use this method(va_arg) how should we declare it in the structure?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I have no idea, having never played with that method. I recommend that you Google on the method and some of the examples may give you an idea of how to go about it.

IMN logo majestic logo threadwatch logo seochat tools logo