在写测试程序的时候,重复编写重控制台读取命令的函数是令人乏味的,那么我们是否可以通过C++的封装性来完成一些简单的功能,以便日后的子类再次利用呢?这片文章就是在种需求的促使下,给出了一个简单的解决方案,希望网友对此有所启发
。
/**
* @file Seminar1.cpp
* @author Wang yanqing
*/
#include <iostream>
#include <memory>
using namespace std;
#define SHOW_ME( name ) cout << #name"'s size = " << sizeof( name ) << endl
#define SHOW_PROMPT() cout << "$ "
#define SHOW_INV_CMD( executor, cmd ) cout << #executor" Executor: " << char(cmd) << ": command not found" << endl
#define HELP_ME( cmd, desc ) cout << #cmd"/t/t---/t"desc << endl
#define SHOW_OFFSET( type, member ) cout << #member"'s offset is " << /
(unsigned long) (&((type *) 0)->member) << " in the struct "#type << endl
#define SHOW_LINE() do {/
for ( int i = 0; i < 80; ++ i ) /
cout << "-"; /
cout << endl; /
} while ( 0 )
#define SHOW_STRUCT( name ) do {/
SHOW_LINE(); /
SHOW_OFFSET( name, c ); /
SHOW_OFFSET( name, s ); /
SHOW_OFFSET( name, i ); /
SHOW_LINE(); /
} while ( 0 )
class Executor
{
bool running;
protected:
virtual bool OnCommand( int cmd )
{
bool consumed = true;
switch ( cmd )
{
case 'q':
case 'Q':
running = false;
break;
case 'h':
case 'H':
help();
break;
default:
consumed = false;
break;
}
return consumed;
}
virtual void DoRun() = 0;
virtual void help() const
{
HELP_ME( q, "Quit" );
HELP_ME( h, "Help" );
}
public:
Executor(): running( true )
{
}
virtual ~Executor()
{
}
void Run()
{
help();
SHOW_PROMPT();
while ( running )
{
DoRun();
}
}
};
class WYQ: public Executor
{
struct WasteMemory
{
char c;
int i;
short s;
};
struct RationalMemory
{
char c;
short s;
int i;
};
struct CarfullMemory
{
char c;
char unused;
short s;
int i;
};
int GetCommand()
{
char cmd;
for ( ; ; )
{
//cin >> cmd;
cmd = getchar();
if ( cmd != '/n' )
break;
SHOW_PROMPT();
}
return cmd;
}
void ShowSize( void )
{
SHOW_ME( WasteMemory );
SHOW_ME( RationalMemory );
SHOW_ME( CarfullMemory );
}
void ShowStruct( void )
{
SHOW_STRUCT( WasteMemory );
SHOW_STRUCT( RationalMemory );
SHOW_STRUCT( CarfullMemory );
}
protected:
virtual bool OnCommand( int cmd )
{
bool consumed = true;
switch ( cmd )
{
case 'a':
case 'A':
ShowSize();
break;
case 'b':
case 'B':
ShowStruct();
break;
default:
consumed = Executor::OnCommand( cmd );
break;
}
return consumed;
}
virtual void DoRun()
{
int cmd = GetCommand();
if ( !OnCommand( cmd ) )
{
SHOW_INV_CMD( WYQ, cmd );
help();
}
}
virtual void help() const
{
HELP_ME( a, "Show structures' size" );
HELP_ME( b, "Show structures' distribution" );
Executor::help();
}
public:
virtual ~WYQ()
{
cout << "WYQ: byebye!" << endl;
}
};
int main( void )
{
auto_ptr<Executor> wyq( new WYQ );
wyq->Run();
return 0;
}
![](/fckeditor/editor/images/smiley/msn/teeth_smile.gif)
/**
* @file Seminar1.cpp
* @author Wang yanqing
*/
#include <iostream>
#include <memory>
using namespace std;
#define SHOW_ME( name ) cout << #name"'s size = " << sizeof( name ) << endl
#define SHOW_PROMPT() cout << "$ "
#define SHOW_INV_CMD( executor, cmd ) cout << #executor" Executor: " << char(cmd) << ": command not found" << endl
#define HELP_ME( cmd, desc ) cout << #cmd"/t/t---/t"desc << endl
#define SHOW_OFFSET( type, member ) cout << #member"'s offset is " << /
(unsigned long) (&((type *) 0)->member) << " in the struct "#type << endl
#define SHOW_LINE() do {/
for ( int i = 0; i < 80; ++ i ) /
cout << "-"; /
cout << endl; /
} while ( 0 )
#define SHOW_STRUCT( name ) do {/
SHOW_LINE(); /
SHOW_OFFSET( name, c ); /
SHOW_OFFSET( name, s ); /
SHOW_OFFSET( name, i ); /
SHOW_LINE(); /
} while ( 0 )
class Executor
{
bool running;
protected:
virtual bool OnCommand( int cmd )
{
bool consumed = true;
switch ( cmd )
{
case 'q':
case 'Q':
running = false;
break;
case 'h':
case 'H':
help();
break;
default:
consumed = false;
break;
}
return consumed;
}
virtual void DoRun() = 0;
virtual void help() const
{
HELP_ME( q, "Quit" );
HELP_ME( h, "Help" );
}
public:
Executor(): running( true )
{
}
virtual ~Executor()
{
}
void Run()
{
help();
SHOW_PROMPT();
while ( running )
{
DoRun();
}
}
};
class WYQ: public Executor
{
struct WasteMemory
{
char c;
int i;
short s;
};
struct RationalMemory
{
char c;
short s;
int i;
};
struct CarfullMemory
{
char c;
char unused;
short s;
int i;
};
int GetCommand()
{
char cmd;
for ( ; ; )
{
//cin >> cmd;
cmd = getchar();
if ( cmd != '/n' )
break;
SHOW_PROMPT();
}
return cmd;
}
void ShowSize( void )
{
SHOW_ME( WasteMemory );
SHOW_ME( RationalMemory );
SHOW_ME( CarfullMemory );
}
void ShowStruct( void )
{
SHOW_STRUCT( WasteMemory );
SHOW_STRUCT( RationalMemory );
SHOW_STRUCT( CarfullMemory );
}
protected:
virtual bool OnCommand( int cmd )
{
bool consumed = true;
switch ( cmd )
{
case 'a':
case 'A':
ShowSize();
break;
case 'b':
case 'B':
ShowStruct();
break;
default:
consumed = Executor::OnCommand( cmd );
break;
}
return consumed;
}
virtual void DoRun()
{
int cmd = GetCommand();
if ( !OnCommand( cmd ) )
{
SHOW_INV_CMD( WYQ, cmd );
help();
}
}
virtual void help() const
{
HELP_ME( a, "Show structures' size" );
HELP_ME( b, "Show structures' distribution" );
Executor::help();
}
public:
virtual ~WYQ()
{
cout << "WYQ: byebye!" << endl;
}
};
int main( void )
{
auto_ptr<Executor> wyq( new WYQ );
wyq->Run();
return 0;
}