"Chris ( Val )" <ch******@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
On Sep 28, 10:33 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>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.
I don't completely understand why you require 2 classes, but you
could look into template specialisation to see if that helps you.
I just wanted to make the point that unless it has changed in the
recent standard, relying on an accurate string from the 'typeid'
"name()" member is not reliable, and implementation dependant; if
a string is returned at all.
Additionally, I don't think it is being used effectively the way
you have it.
For example, the construct "typeid( float ).name()" will always
produce the same hard coded result on one side of the evaluation,
provided a valid result is available for evaluation.
Much better to use something like the following, so you're
not bound to that hard coded construct:
template<typename A, typename Bbool isEqual( A a, B b ) {
return typeid( a ) == typeid( b );
}
int main()
{
char A(0);
int B(0);
long C(0);
std::cout << std::boolalpha << isEqual( A, A ) << '\n';
std::cout << std::boolalpha << isEqual( A, B ) << '\n';
std::cout << std::boolalpha << isEqual( A, C ) << '\n';
return 0;
}
-- OUTPUT --
true
false
false
I would rather not hard code any types at all. I'm working with existing
code that recieves messages such as:
bool MinerGlobalState::OnMessage(Miner* pMiner, const Telegram& msg)
With Telegram defined as:
struct Telegram
{
int Sender;
int Receiver;
int Msg;
double DispatchTime;
void* ExtraInfo;
// constructor etc...
}
It is the void* ExtraInfo I am trying to make a little more friendly. In the
OnMessage I have to do things such as:
int Amount = *reinterpret_cast<int*>( msg.ExtraInfo );
Which, okay, usually, is okay. But, somewhere someone may have stuck an int
in there instead of a float or such. Not very typesafe. And I don't like
the reinterpret_cast.
What would be ideal would be to move the ExtraInfo out of Telegram and make
it a paramater in OnMessage changing the signature to:
bool MinerGlobalState::OnMessage(Miner* pMiner, const Telegram& msg, const
float& ExtraInfo)
A different class may have it declared:
bool CookStew::OnMessage(MinersWife* wife, const Telegram& msg, const
SomeClass& ExtraInfo)
or such.
The existing code I'm working with has many source files and I don't really
want to have to redesign the whole class heirarchy to use the visitor
pattern, and also there may be a case where one message handler may need to
have the ExtraInfo as different typed depending on the Msg paramater of the
Telegram.
I know that void*'s were used a lot in C callbacks and had hoped that by now
someone had a good way to deal with them in C++. Upcasting doesn't seem to
be working. Trying to upcast an AIMsg* to a Message<float>* didn't work and
got me nowhere.
Trying to google for "change void* in C++" doesn't help has void is found
too many places in function/method returns. It looks like somewhere I'm
going to have to do a dymaic_cast or a reinterpret_cast and am trying to
stick any such ugliness away from the onmessage methods.