Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7767317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:38:49+00:00 2026-06-01T15:38:49+00:00

I’m writing a tool which enables a user to interact with a bit of

  • 0

I’m writing a tool which enables a user to interact with a bit of hardware by changing settings and then streaming information.

To do this I have a couple of threads running: EquipmentInterface and DataProcessor which are connected by a Queue.

The EquipmentInterface thread has methods to alter settings on the equipment (Rotate and Refocus for example) and the resulting information (CurrentAngle and CurrentFocalDistance) is added to the Queue. Once the settings are correct there are methods to StartStreaming and StopStreaming and once streaming starts, data from the equipment is packetised and added onto the queue.

All of the information placed on the queue derives from a single BaseMessage 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.

The DataProcessor listens to the other end of the Queue and depending on the current angle / focal distance, processes the subsequent data.

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 BaseMessage 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.

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 what I’m doing is a bad code smell. I’ve seen a suggestion which use Boost, but they don’t look any cleaner than the switch statement to me (maybe I’m missing something?).

So my question is: Should I be trying to avoid the switch-statement / downcasting solution and if so, how?

My implementation is in C++/CLI so either .net or C++ solutions are what I’m after.

Edit – Based on the comments from iammilind and stfaanv, is this the sort of thing you’re suggesting:

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<Angle*>(item);
        if( pAngle != NULL )
        {
            ProcessAngle(pAngle);
        }
        FocalLength* pFocalLength = dynamic_cast<FocalLength*>(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;
}

…and can it be simplified? The ProcessMessages 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:38:51+00:00Added an answer on June 1, 2026 at 3:38 pm

    You could try a visitor design pattern: http://en.wikipedia.org/wiki/Visitor_pattern

    Each DataProcessor would inherit from a BaseVisitor class, which defines virtual method for handling each type of Message. Basically these methods are just noop.

    When you define a new message type you add a new virtual method with a noop implementation for this message type in the BaseVisitor. Then if a child DataProcessor class wants to process this message type you override the virtual method in this DataProcessor only. All other DataProcessorremain untouched.

        #include <iostream>
    
    
        class FocalLength;
        class Angle;
        class EquipmentVisitor;
    
        class QueuedItem
        {
        public:
                QueuedItem() { }
                virtual ~QueuedItem() { }
    
                virtual void AcceptVisitor(EquipmentVisitor& visitor) = 0;
        };
    
        class EquipmentVisitor
        {
        public:
                virtual ~EquipmentVisitor() {}
    
                virtual void Visit(FocalLength& item) {}
                virtual void Visit(Angle& item)       {}
    
                void ProcessMessages(QueuedItem* item)
                {
                        item->AcceptVisitor(*this);
                }
        };
    
        class Angle : public QueuedItem
        {
        public:
                Angle() {}
                virtual ~Angle() { }
    
                void AcceptVisitor(EquipmentVisitor& visitor) { visitor.Visit(*this); }
        };
    
        class FocalLength : public QueuedItem
        {
        public:
                FocalLength() {}
                virtual ~FocalLength() { }
    
                void AcceptVisitor(EquipmentVisitor& visitor) { visitor.Visit(*this); }
        private:
    
        };
    
        class MyDataProcessor : public EquipmentVisitor
        {
        public:
                virtual ~MyDataProcessor() {}
    
                void Visit(Angle& angle)             { std::cout << "Processing Angle" << std::endl; }
                void Visit(FocalLength& focalLength) { std::cout << "Processing FocalLength" << std::endl; }
        };
    
    
        int main(int argc, char const* 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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.