C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 04:32 AM
serafon serafon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 serafon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 44 sec
Reputation 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...

Reply With Quote
  #2  
Old November 5th, 2012, 04:53 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old November 5th, 2012, 08:44 AM
serafon serafon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 serafon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old November 5th, 2012, 02:03 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,138 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 22 h 37 m 39 sec
Reputation Power: 1974
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/1...arguments-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.

Reply With Quote
  #5  
Old November 5th, 2012, 02:25 PM
serafon serafon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 serafon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #6  
Old November 5th, 2012, 02:59 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,138 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 22 h 37 m 39 sec
Reputation Power: 1974
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Suggestion for a menu programming structure

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap