title: How can I avoid downcasting when passing information through a queue?
body: <p>I'm writing a tool which enables a user to interact with a bit of hardware by changing settings and then streaming information.</p>
  
  <p>To do this I have a couple of threads running: <code>EquipmentInterface</code> and <code>DataProcessor</code> which are connected by a <code>Queue</code>.</p>
  
  <p>The <code>EquipmentInterface</code> thread has methods to alter settings on the equipment (<code>Rotate</code> and <code>Refocus</code> for example) and the resulting information (<code>CurrentAngle</code> and <code>CurrentFocalDistance</code>) is added to the <code>Queue</code>. Once the settings are correct there are methods to <code>StartStreaming</code> and <code>StopStreaming</code> and once streaming starts, data from the equipment is packetised and added onto the queue.</p>
  
  <p>All of the information placed on the queue derives from a single <code>BaseMessage</code> class which includes an indication of the message type. I then have derived message types for angles, focal distances, beginning and ending streaming and of course, the data itself.</p>
  
  <p>The <code>DataProcessor</code> listens to the other end of the Queue and depending on the current angle / focal distance, processes the subsequent data.</p>
  
  <p>Now, the thing is, I have a function in the data processor which uses a switch statement to type-check the messages coming in. Those messages are then down-casted to the appropriate type and passed to an appropriate handler. In reality, there's more than just a DataProcessor listening to a single queue, but in fact multiple listeners on multiple queues (some store to disk, some display information on a gui). Every time I add some information I have to create a new <code>BaseMessage</code> derived class, add a new type to that base class and then update the switch statements in each of the consumers to cope with the new message.</p>
  
  <p>Something about this architecture feels wrong to me and I've been reading a lot about down-casting recently. From what I've seen, the general consensus seems to be that <a href="http://codebetter.com/jeremymiller/2006/12/26/downcasting-is-a-code-smell/" rel="nofollow noreferrer">what I'm doing is a bad code smell</a>. I've seen <a href="https://stackoverflow.com/questions/9321497/design-pattern-to-avoid-downcasting-in-message-passing">a suggestion which use Boost</a>, but they don't look any cleaner than the switch statement to me (maybe I'm missing something?).</p>
  
  <p>So my question is: Should I be trying to avoid the switch-statement / downcasting solution and if so, how?</p>
  
  <p>My implementation is in C++/CLI so either .net or C++ solutions are what I'm after.</p>
  
  <p><strong>Edit</strong> - Based on the comments from iammilind and stfaanv, is this the sort of thing you're suggesting:</p>
  
  <pre><code>class QueuedItem
  {
  public:
      QueuedItem() { }
      virtual ~QueuedItem() { }
  
  };
  
  class Angle : public QueuedItem
  {
  public:
      Angle() {}
      virtual ~Angle() { }
  };
  
  class FocalLength : public QueuedItem
  {
  public:
      FocalLength() {}
      virtual ~FocalLength() { }
  private:
  
  };
  
  
  class EquipmentHandler
  {
  protected:
      virtual void ProcessAngle(Angle* angle) {}; 
      virtual void ProcessFocalLength(FocalLength* focalLength) {};   
  
  public:
      void ProcessMessages(QueuedItem* item)
      {
          Angle* pAngle = dynamic_cast&lt;Angle*&gt;(item);
          if( pAngle != NULL )
          {
              ProcessAngle(pAngle);
          }
          FocalLength* pFocalLength = dynamic_cast&lt;FocalLength*&gt;(item);
          if( pFocalLength != NULL )
          {
              ProcessFocalLength(pFocalLength);
          }
  
      }
  };
  
  class MyDataProcessor : public EquipmentHandler
  {
  protected:
      virtual void ProcessAngle(Angle* angle) override { printf("Processing Angle"); }
      virtual void ProcessFocalLength(FocalLength* focalLength) override { printf("Processing FocalLength"); };   
  };
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
  
      // Equipment interface thread...
      FocalLength* f = new FocalLength();
      QueuedItem* item = f; // This gets stuck onto the queue
  
      // ...DataProcessor thread (after dequeuing)
      QueuedItem* dequeuedItem = item;
  
      // Example of a DataProcessor implementation.
      // In reality, this would 
      MyDataProcessor dataProc;
      dataProc.ProcessMessages(dequeuedItem);
  
      return 0;
  }
  </code></pre>
  
  <p>...and can it be simplified? The <code>ProcessMessages</code> feels a bit clunky but that's the only way I could see to do it without a switch statement and some sort of enumerated message type identifier in the base class. </p>