Managed Menu

General discussion about C/C++

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Managed Menu

Postby __sorcerer__ » Sat Nov 26, 2005 2:58 am

Hi!
I am interesting waht you think about this CManageMenu class, and is this is a wright way to do it so. I am asking because I made this when I was helping some students ( final exams) in peogramming, and now all of them are using this code in their apps :D . So I thikng this was a good :idea: to make it. (It should runs on every compilators, tested on Linux, Windows, and Mac.)

[syntax="cpp"]

#ifndef CMANAGE_MENU_H
#define CMANAGE_MENU_H

#include <vector>

namespace Managed
{

template<class T>
class CManageMenu
{
private:
std::vector<T *> m_vMenus;
int m_count_menu;

struct _exit
{
bool isExit;
int which;
}exit;


public:
CManageMenu();
~CManageMenu();

void Add(T *menu, bool exitFunc = false);
void Select(int id);
int Count() const;

void Show() const;

bool IsExit() const;
};
};


// ----------------------------------------------------------------------------
template<class T>
inline Managed::CManageMenu<T>::CManageMenu()
{
m_count_menu = 0;
exit.isExit = false;
}



// ----------------------------------------------------------------------------
template<class T>
Managed::CManageMenu<T>::~CManageMenu()
{
typename std::vector<T *>::iterator it = m_vMenus.begin();
while(m_vMenus.end() != it)
{
if(NULL != (*it))
delete (*it);
it++;
}
}



// ----------------------------------------------------------------------------
template<class T>
void Managed::CManageMenu<T>::Add(T *menu, bool exitFunc)
{
m_vMenus.push_back(menu);
m_count_menu++;
menu->SetID(m_count_menu);
if(exitFunc)
exit.which = menu->GetID();
}



// ----------------------------------------------------------------------------
template<class T>
inline int Managed::CManageMenu<T>::Count() const
{
return m_count_menu;
}


// ----------------------------------------------------------------------------
template<class T>
void Managed::CManageMenu<T>::Select(int id)
{
if( (0 >= id) || (m_count_menu < id) )
return;

if(id == exit.which)
exit.isExit = true;

m_vMenus[id - 1]->Run();
}


// ----------------------------------------------------------------------------
template<class T>
void Managed::CManageMenu<T>::Show() const
{
typename std::vector<T *>::const_iterator it = m_vMenus.begin();
while(m_vMenus.end() != it)
{
(*it)->Show();
it++;
}
}

// ----------------------------------------------------------------------------
template<class T>
inline bool Managed::CManageMenu<T>::IsExit() const
{
return exit.isExit;
}


#endif
[/syntax]

Ok here is the code you can use with:
[syntax="cpp"]
#ifndef CBASE_MENU_H
#define CBASE_MENU_H

#include <string>

namespace Managed
{
class CBaseMenu
{
private:
std::string m_name;
int m_id;

public:
CBaseMenu(std::string name) { m_name = name; }
virtual ~CBaseMenu() = 0 { m_id = 0; }

public:
virtual void Run() = 0 {}

public:
void Show() const
{
std::cout << GetID() << ": " << GetName() << std::endl;
}
void SetID(int id) { m_id = id; }
int GetID() const { return m_id; }

std::string GetName() const { return m_name; }
};
};


#endif
[/syntax]
[syntax="cpp"]
#ifndef MYMENUCLASSES_H
#define MYMENUCLASSES_H


// ----------------------------------------------------------------------------
// CWczytajDane class
// ----------------------------------------------------------------------------
class CWczytajDane : public Managed::CBaseMenu
{
public:
CWczytajDane():Managed::CBaseMenu("Wczytaj dane") {}
void Run() {}
};


// ----------------------------------------------------------------------------
// CZapiszDane class
// ----------------------------------------------------------------------------
class CZapiszDane : public Managed::CBaseMenu
{
public:
CZapiszDane():Managed::CBaseMenu("Zapisz dane") {}
void Run() {}
};

class CZakoncz : public Managed::CBaseMenu
{
public:
CZakoncz():Managed::CBaseMenu("Zakoncz") {}
void Run() { }

#endif
[/syntax]

[syntax="cpp"]
#ifndef MESSAGE_H
#define MESSAGE_H

#include <iostream>
#include <string>

namespace Message
{
void Show(std::string message);
char Get(std::string message);
};

// ----------------------------------------------------------------------------
void Message::Show(std::string message)
{
std::cout << message ;
}


// ----------------------------------------------------------------------------
char Message::Get(std::string message)
{
std::cout << message ;

char choice;
std::cin.get(choice);
std::cin.ignore(1024, '\n');

return choice;
}

#endif
[/syntax]

And of course example main.cpp:
[syntax="cpp"]
#include "CManageMenu.h"
#include "CBaseMenu.h"
#include "MyMenuClasses.h"
#include "Message.h"


int main(int argc, char *argv[])
{
Managed::CManageMenu<Managed::CBaseMenu> mm;

mm.Add(new CWczytajDane);
mm.Add(new CZapiszDane);
mm.Add(new CZakoncz, true);



while(!mm.isExit())
{
mm.Show();

mm.Select(Message::Get("Podaj wybor: ") - '0');
}

return 0;
}
[/syntax]

So what you think about this??
( I have changed this code a bit, so you can easly use it in your game engines, and apps)
I can't look around (Its too much to take in)
I can't hold on (When I'm stretched so thin)
I can't slow down (Watching everything spin)
I can't look past (Its starting over again)

C++. The life of all!
User avatar
__sorcerer__
 
Posts: 95
Joined: Sat Nov 26, 2005 2:52 am
Location: Olszanka Poland

Return to General

Who is online

Users browsing this forum: Google Adsense [Bot] and 1 guest