Boost logo

Boost Users :

From: Ferentinos, Jerry (jerry_ferentinos_at_[hidden])
Date: 2008-03-25 18:29:49


Hi Robert,
 
You may not want your Packet clients to have to downcast through Packet
subtypes but you can define your Packet subtypes to rely on particular
clients, like so (untested, not compiled):
 
class Packet
{
public:
  virtual void process( PacketProcessor* p_PacketProcessor ) = 0;
};
 
class WalkPacket : public Packet
{
public:
  virtual void process(PacketProcessor* p_PacketProcessor )
  {
    WalkPacketProcessor* l_WalkPacketProcessor = dynamic_cast<
WalkPacketProcessor* >( p_PacketProcessor );
    if( l_WalkPacketProcessor )
      l_WalkPacketProcessor->handleWalkPacket( this );
  }
};
 
class PacketProcessor // base class for all PacketProcessors
{
public:
  virtual ~PacketProcessor() = 0; // define this in .cpp
};
 
class WalkPacketProcessor : public PacketProcessor
{
public:
  // the slot for any packet
  void handlePacket( Packet const& p )
  {
    p.process( this );
  }
 
  // the particular handler for WalkPackets
  virtual void handleWalkPacket( WalkPacket* p_WalkPacket )
  {
    // use this packet
  }
};
 
Jerry Ferentinos
Application Engineering Services, Montreal MRL - IT
Merck Frosst Center for Therapeutic Research
www.merckfrosstlab.ca
 

________________________________

From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Robert Dailey
Sent: Tuesday, March 25, 2008 5:23 PM
To: boost [usr]
Subject: [Boost-users] Need boost's help with poor design

Hi,

Currently I'm using Boost.Signals to transfer Packet objects to
subscribers. Right now my Boost.Signal looks like below:

boost::signal<void (Packet const& p)>

Right now Packet is the base class for several other packet types, each
having the specific data a subscriber needs access to. Of course, given
this design, the only way to obtain the data is to perform a downcast
via static_cast or dynamic_cast:

WalkPacket const& wp = static_cast<WalkPacket const&>( p ); // 'p' here
is a generic Packet object.

This is obviously bad design since it isn't type safe at all. It's also
repetitive and tedious since each subscriber must duplicate the logic to
perform the downcast. I'm wondering if there's anything in Boost you
guys can suggest that I use to make this a little better. I would prefer
to have a common Signal object that can take differently typed Slot
objects. For example, I want to pass in a slot that looks like: "void
(WalkPacket& p)" and have the signal utilize implicit conversions or
something. Templates aren't even being used here because I can't find a
way to fit them in, since everything is being done at runtime and not
compile time.

Keep this important note in mind: The type of the packet that will be
dispatched to subscribers can only be known at compile time via a
factory method. Data is received from the network, and that data is
packed into the correct packet and returned as a Packet* base class
pointer, so that we can transfer the data to subscribers through a
common interface.

------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD
and in Japan, as Banyu - direct contact information for affiliates is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and then
delete it from your system.

------------------------------------------------------------------------------


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net