J
Jim Langston
I am using some code that I got that uses a form a message dispatching where
the data is passed via a void*. I don't like void*'s so am experimenting
with a different way to do them in C++. I don't use boost, and this is what
I've come up with so far, but it seems fairly ugly.
In actual use the final handler that handles the data would know what type
the data should be based on other paramaters in the function call, so this
is just proof of concept.
Has anyone a better idea? I tend to like MessageHandler2 using a reference
instead of a pointer.
#include <iostream>
#include <string>
struct AIMsg
{
public:
AIMsg( const std::string& MsgType ): MsgType( MsgType ) {}
std::string MsgType;
virtual ~AIMsg() {}
};
template <class T> class Message: public AIMsg
{
public:
Message(): AIMsg( typeid(T).name() ) {}
T Value;
};
void MessageHandler( AIMsg* Msg )
{
if ( Msg->MsgType == typeid(float).name() )
std::cout << dynamic_cast<Message<float>*>( Msg )->Value << "\n";
else if ( Msg->MsgType == typeid(int).name() )
std::cout << dynamic_cast<Message<int>*>( Msg )->Value << "\n";
}
void MessageHandler2( AIMsg& Msg )
{
if ( Msg.MsgType == typeid(float).name() )
std::cout << dynamic_cast<Message<float>* >( &Msg )->Value << "\n";
if ( Msg.MsgType == typeid(int).name() )
std::cout << dynamic_cast<Message<int>* >( &Msg )->Value << "\n";
}
int main()
{
Message<float> Bar;
Bar.Value = 54321.123f;
MessageHandler( &Bar );
MessageHandler2( Bar );
Message<int> Bar2;
Bar2.Value = 123;
MessageHandler( &Bar2 );
MessageHandler2( Bar2 );
return 0;
}
the data is passed via a void*. I don't like void*'s so am experimenting
with a different way to do them in C++. I don't use boost, and this is what
I've come up with so far, but it seems fairly ugly.
In actual use the final handler that handles the data would know what type
the data should be based on other paramaters in the function call, so this
is just proof of concept.
Has anyone a better idea? I tend to like MessageHandler2 using a reference
instead of a pointer.
#include <iostream>
#include <string>
struct AIMsg
{
public:
AIMsg( const std::string& MsgType ): MsgType( MsgType ) {}
std::string MsgType;
virtual ~AIMsg() {}
};
template <class T> class Message: public AIMsg
{
public:
Message(): AIMsg( typeid(T).name() ) {}
T Value;
};
void MessageHandler( AIMsg* Msg )
{
if ( Msg->MsgType == typeid(float).name() )
std::cout << dynamic_cast<Message<float>*>( Msg )->Value << "\n";
else if ( Msg->MsgType == typeid(int).name() )
std::cout << dynamic_cast<Message<int>*>( Msg )->Value << "\n";
}
void MessageHandler2( AIMsg& Msg )
{
if ( Msg.MsgType == typeid(float).name() )
std::cout << dynamic_cast<Message<float>* >( &Msg )->Value << "\n";
if ( Msg.MsgType == typeid(int).name() )
std::cout << dynamic_cast<Message<int>* >( &Msg )->Value << "\n";
}
int main()
{
Message<float> Bar;
Bar.Value = 54321.123f;
MessageHandler( &Bar );
MessageHandler2( Bar );
Message<int> Bar2;
Bar2.Value = 123;
MessageHandler( &Bar2 );
MessageHandler2( Bar2 );
return 0;
}