I'm intersting to see an example prototype for this. can you please show
me an example.
thanks a lot
yous
A very simple one using a fixed size array of 256.
typedef std::basic_string< unsigned char > ustring;
class MessageProcessor
{
public:
virtual void processMessage( const ustring & msg ) = 0;
virtual ~MessageProcessor() {}
};
class MessageProcessorTable
{
private:
enum { tableSize=256 };
MessageProcessor* itsTable[ tableSize ];
public:
MessageProcessorTable()
{
std::fill_n( &itsTable[0], tableSize, 0 );
}
void addProcessor( unsigned char idx, MessageProcessor * pmp )
{
itsTable[ idx ] = pmp;
}
bool processMessage( const ustring & msg ) const
{
if ( !msg.empty() )
{
MessageProcessor * pmp = itsTable[ msg[0] ];
if ( pmp )
{
pmp->processMessage( msg );
return true;
}
}
return false;
}
};
Obviously you then create an instance of the table, instances of each
message and add them to the table, then process your messages as they
come in. And you can introduce error handling features where a message
could not be processed - log the error, throw an exception or however
you want to handle it.
And note this is an illustration.In reality you might separate the
definition and implementation of the classes into .h and .cpp files.