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 8101397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:58:19+00:00 2026-06-05T22:58:19+00:00

I’m searching a way to avoid systematic dynamic cast in the following problem. I

  • 0

I’m searching a way to avoid systematic dynamic cast in the following problem.

I have Action objects and Message objects. Action objects have methods that emits messages, and other methods that accept messages. There are of course derived Action classes and Messages classes. The objects and their methods are connected dynamically at run time by string identification based on a configuration file defining the network of interconnected objects.

This is equivalent to a signal slot system with a strong constrain on the number and type of the arguments. All signals emit a message derived of the class Message and all slots accept a message derived from the class Message.

Connections are NxN. Thus signals are multicast and slots can accept signals from multiple sources.

The current implementation is to use a Link class instantiating the connection between a signal and a slot. The signals and slots are functor member variables. Each Action object has a two maps. One from string name to signal and one from string name to slot. There is also a global map of Action names to instance. The benefit of keeping track of links in the target Action (slots) is to properly disconnect all links when the instance is destroyed.

In a first pass all Action instances defined in the configuration file are instantiated, In a second pass links are instantiated connecting signals to slots.

The question is how would you implement this so that a message type matching check is only performed when the link is instantiated and so that a dynamic cast is only performed if required.

For instance if we have the message base class M and a subclass M1 of M and a subclass M2 of M1, a link from signal(M2) to slot(M1) or slot(M) would not perform a dynamic cast, and a link from signal(M1) or signal(M) to slot(M2) would perform a dynamic cast. The slot method is only called is the dynamic cast succeeds (doesn’t return nullptr).

An implementation where a dynamic cast is performed at each call is trivial. I seek a solution to avoid this if possible.

My current understanding is that I can’t use boost::signals because of the dynamic binding.

  • 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-05T22:58:20+00:00Added an answer on June 5, 2026 at 10:58 pm

    I’ve solved my problem. Here is the code which is nicely concise.
    The signal is also a template class which simply defines the message types it may emit.
    Thus, when connecting the signal with a slot, the Link constructor can check the polymorphic compatibility of the message type emitted by the signal and the message type accepted by the slot. It then copies the appropriate function pointer whether a dynamic cast is required or not. What is not shown here is the Type class and how it is used to check message type compatibility.

    The answer was how to define the two slot functions and their function pointers.

    Here is an example how the slots would be defined in a class:

    class MyAction : public Action
    {
    public:
        //! Define shared pointer on object
        typedef std::shared_ptr<MyAction> Ptr;
    
        //! Constructor
        MyAction( const std::string& name )
            : Action(name),
              m_slotMsgM( this ),
              m_slotMsgA( this ),
              m_slotMsgB( this )
        {
        }
    
        //! Register the slots with their name for dynamic linking
        void configure()
        {
            add("processMsgM", &m_slotMsgM );
            add("processMsgA", &m_slotMsgA );
            add("processMsgB", &m_slotMsgB );
        }
    
        //! Slot method 
        void processMsgM( Message::Ptr msg, Link * link = nullptr )
        {
            cout << "MyAction::processMsgM: Msg " << msg->type().name() << endl;
        }
    
        //! Slot method 
        void processMsgA( MsgA::Ptr msg, Link * link = nullptr )
        {
            cout << "MyAction::processMsgA: Msg " << msg->type().name() << endl;
        }
    
        //! Slot method 
        void processMsgB( MsgB::Ptr msg, Link * link = nullptr )
        {
            cout << "MyAction::processMsgB: Msg " << msg->type().name() << endl;
        }
    
    protected:
        //! Define slots
        SlotT<MyAction, Message, &MyAction::processMsgM> m_slotMsgM;
        SlotT<MyAction, MsgA, &MyAction::processMsgA> m_slotMsgA;
        SlotT<MyAction, MsgB, &MyAction::processMsgB> m_slotMsgB;
    };
    

    Here is the Slot and SlotT classes definition.

    class Link;
    typedef std::set<Link*> LinkSet;
    
    //! Base class for Slot template class
    class Slot
    {
        friend class Link;
    public:
        //! Slot function pointer
        typedef std::function<void ( Message::Ptr, Link* )> Function;
    
        //! Disconnect all links
        ~Slot();
    
        //! Return the type of message accepted by this Slot function
        const TypeDef& messageType() const { return m_msgType; }
    
        //! Return slot function applying a dynamic cast on the message pointer
        Function getDynamicCastFunction() const
            { return m_dynamicCastFunction; }
    
        //! Return slot function applying a static cast on the message pointer
        Function getStaticCastFunction() const
            { return m_staticCastFunction; }
    
        //! Operator () using the dynamic cast
        void operator()(Message::Ptr msg, Link * link = nullptr )
            { m_dynamicCastFunction( msg, link); }
    
    protected:
    
        //! Construct Slot by derived class instance construction only
        Slot( const TypeDef& type, Function dynamicCastFunction,
              Function staticCastFunction ) :
            m_msgType(type),
            m_dynamicCastFunction(dynamicCastFunction),
            m_staticCastFunction(staticCastFunction)
        {
        }
    
        //! Insert link in set
        void connect( Link* link )
            { m_links.insert( link ); }
    
        //! Remove link from set
        void disconnect( Link* link )
            { m_links.erase( link ); }
    
        //! Set of active links
        LinkSet m_links;
    
        //! Type of accepted messages
        const TypeDef& m_msgType;
    
        //! Slot method usind dynamic cast on message pointer
        const Function m_dynamicCastFunction;
    
        //! Slot method using static cast on message pointer
        const Function m_staticCastFunction;
    };
    
    
    template <class TObj, class TMsg, 
              void (TObj::*TMethod)(typename TMsg::Ptr, Link*)>
    class SlotT : public Slot
    {
    public:
    
        //! SlotT constructor with templated type
        SlotT( TObj* obj )
            : Slot(TMsg::Type(),
              std::bind( &SlotT<TObj,TMsg,TMethod>::dynamicCastFunction, obj,
                                          std::placeholders::_1,
                                          std::placeholders::_2 ),
              std::bind( &SlotT<TObj,TMsg,TMethod>::staticCastFunction, obj,
                                               std::placeholders::_1,
                                               std::placeholders::_2 ) )
        {
        }
    
    private:
        //! dynamic cast function
        static void dynamicCastFunction( TObj* obj, 
                                         typename Message::Ptr msg, 
                                         Link* link )
        {
            typename TMsg::Ptr m = std::dynamic_pointer_cast<TMsg>(msg);
            if( m && obj )
                (obj->*TMethod)(m, link);
        }
    
        //! static cast function
        static void staticCastFunction( TObj* obj, 
                                        typename Message::Ptr msg, 
                                        Link* link )
        {
            typename TMsg::Ptr m = std::static_pointer_cast<TMsg>(msg);
            if( m && obj )
                (obj->*TMethod)(m, link);
        }    
    };
    
    • 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 have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.