menu mechanize with threading

G

Gary Wessle

Hi

The program I am trying to make is a basic one, it provides a command
line menu to the user and accepts the choice given by the user
selection from the main menu. it then spawns a new thread to preform
the task chosen while at the same time offers a new sub-menu and a new
prompt according to the selection the user just made.

the program uses composition and inheritance of different types.

the issue I have been pondering about is, how can I set it up so that
by minimum work I can add/remove menu trees as well as firing items
from the main menu as well as the sub-menu.

my thoughts are as follows, "need some guidance":
present each task "menu item" by a Task abstract type.

****************************************************************
class Task
{
public:
Task()
{
}
virtual void print_item_name()=0;
};
****************************************************************

for the sake of this discussion, say we have task A and B being the
main menu items.

task A has sub_items "sub_tasks" being A and B, which we will
represent here as AA and AB.

task B has "for now" sub_items "sub_tasks" being A, which we will
represent here as BA,

so we write
****************************************************************
class A : public Task
{
string item_name;
string sub_list; //A comma separated list (item1,item2,...)
public:
A(string in, string sl): item_name(in), sub_list(sl)
{
}
void print_item_name()
{
cout << item_name << endl;
}
/* fired from main e.g
A a("a", "a,b");
boost::thread thrd( a )
*/
void A::eek:perator()()
{
/* do work */
/* call methods */
}
****************************************************************
and the same thing for Task B.

now to the main thing
****************************************************************
int main(){
/* Create the main tasks. */
A a("a", "a,b");
B b("b", "a");

/* Place them in a vector container to use index for selection. */
vector<Task*> vTp;
vTp.push_back(a);
vTp.push_back(b);

/* Show menu and thread the task */
for( ;; ) {
for( unsigned short i=0; i<vTp.size(); i++)
cout << "[" << i << "]" << vTp->print_item();

cout << "Select an item from the menu\n:> ";
unsigned short opt;
cin << opt;
if( opt==9 ) break,
boost::thread thrd( vTp[opt] ); // is this syntax correct?
}
****************************************************************

now, can I do the same thing
/* Create the SUB tasks. */
/* Place them in a vector container to use index for selection. */
/* Show menu and thread the task */
for the sub menu which has to be placed in each parent menu
operator()() function?
e.g

****************************************************************
void A::eek:perator()()
{

/* Create the SUB tasks. */
AA aa("aa", "a,b,c,d,e"); //has more sub menus a,b,c,d,e
AB bb("ab", "a,b");

/* Place them in a vector container to use index for selection. */
vector<Task*> vTp;
vTp.push_back(aa);
vTp.push_back(ab);

/* Show menu and thread the task */
for( ;; ) {
for( unsigned short i=0; i<vTp.size(); i++)
cout << "[" << i << "]" << vTp->print_item();

cout << "Select an item from the menu\n:> ";
unsigned short opt;
cin << opt;
if( opt==9 ) break,
boost::thread thrd( vTp[opt] ); // is this syntax correct?


/* do work */
/* call methods */
}
****************************************************************


now I can just add any item to the main menu or sub items to any sub
menu and write their corresponding operator()() function and I am
done??

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top