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

  • SEARCH
  • Home
  • 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 727647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:35:29+00:00 2026-05-14T06:35:29+00:00

I’m implementing a CORBA like server. Each class has remotely callable methods and a

  • 0

I’m implementing a CORBA like server. Each class has remotely callable methods and a dispatch method with two possible input, a string identifying the method or an integer which would be the index of the method in a table. A mapping of the string to the corresponding integer would be implemented by a map.

The caller would send the string on the first call and get back the integer with the response so that it simply has to send the integer on subsequent calls. It is just a small optimization. The integer may be assigned dynamically on demand by the server object.
The server class may be derived from another class with overridden virtual methods.

What could be a simple and general way to define the method binding and the dispatch method ?

Edit: The methods have all the same signature (no overloading). The methods have no parameters and return a boolean. They may be static, virtual or not, overriding a base class method or not. The binding must correctly handle method overriding.

The string is class hierarchy bound. If we have A::foo() identified by the string “A.foo”, and a class B inherits A and override the method A::foo(), it will still be identified as “A.foo”, but the dispatcher will call A::foo if the server is an A object and B::foo if it is a B object.

Edit (6 apr): In other words, I need to implement my own virtual method table (vftable) with a dynamic dispatch method using a string key to identify the method to call. The vftable should be shared among objects of the same class and behave as expected for polymorphism (inherited method override).

Edit (28 apr): See my own answer below and the edit at the end.

  • 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-05-14T06:35:30+00:00Added an answer on May 14, 2026 at 6:35 am

    Here is an example of my actual method. It Just Works (c) but I’m pretty sure a much cleaner and better way exist. It compiles and runs with g++ 4.4.2 as is. Removing the instruction in the constructor would be great, but I couldn’t find a way to achieve this. The Dispatcher class is basically a dispatchable method table and each instance must have a pointer on its table.

    Note: This code will implicitly make all dispatched methods virtual.

    #include <iostream>
    #include <map>
    #include <stdexcept>
    #include <cassert>
    
    // Forward declaration
    class Dispatchable;
    
    //! Abstract base class for method dispatcher class
    class DispatcherAbs
    {
    public:
        //! Dispatch method with given name on object
        virtual void dispatch( Dispatchable *obj, const char *methodName ) = 0;
    
        virtual ~DispatcherAbs() {}
    };
    
    //! Base class of a class with dispatchable methods
    class Dispatchable
    {
    public:
        virtual ~Dispatchable() {}
    
        //! Dispatch the call
        void dispatch( const char *methodName )
        {
            // Requires a dispatcher singleton assigned in derived class constructor
            assert( m_dispatcher != NULL );
            m_dispatcher->dispatch( this, methodName );
        }
    
    protected:
        DispatcherAbs *m_dispatcher; //!< Pointer on method dispatcher singleton
    };
    
    //! Class type specific method dispatcher
    template <class T>
    class Dispatcher : public DispatcherAbs
    {
    public:
        //! Define a the dispatchable method type
        typedef void (T::*Method)();
    
        //! Get dispatcher singleton for class of type T
        static Dispatcher *singleton()
        {
            static Dispatcher<T> vmtbl;
            return &vmtbl;
        }
    
        //! Add a method binding
        void add( const char* methodName, Method method )
            { m_map[methodName] = method; }
    
        //! Dispatch method with given name on object
        void dispatch( Dispatchable *obj, const char *methodName )
        {
            T* tObj = dynamic_cast<T*>(obj);
            if( tObj == NULL )
                throw std::runtime_error( "Dispatcher: class mismatch" );
            typename MethodMap::const_iterator it = m_map.find( methodName );
            if( it == m_map.end() )
                throw std::runtime_error( "Dispatcher: unmatched method name" );
            // call the bound method
            (tObj->*it->second)();
        }
    
    protected:
        //! Protected constructor for the singleton only
        Dispatcher() { T::initDispatcher( this ); }
    
        //! Define map of dispatchable method
        typedef std::map<const char *, Method> MethodMap;
    
        MethodMap m_map; //! Dispatch method map
    };
    
    
    //! Example class with dispatchable methods
    class A : public Dispatchable
    {
    public:
        //! Construct my class and set dispatcher
        A() { m_dispatcher = Dispatcher<A>::singleton(); }
    
        void method1() { std::cout << "A::method1()" << std::endl; }
    
        virtual void method2() { std::cout << "A::method2()" << std::endl; }
    
        virtual void method3() { std::cout << "A::method3()" << std::endl; }
    
        //! Dispatcher initializer called by singleton initializer
        template <class T>
        static void initDispatcher( Dispatcher<T> *dispatcher )
        {
            dispatcher->add( "method1", &T::method1 );
            dispatcher->add( "method2", &T::method2 );
            dispatcher->add( "method3", &T::method3 );
        }
    };
    
    //! Example class with dispatchable methods
    class B : public A
    {
    public:
        //! Construct my class and set dispatcher
        B() { m_dispatcher = Dispatcher<B>::singleton(); }
    
        void method1() { std::cout << "B::method1()" << std::endl; }
    
        virtual void method2() { std::cout << "B::method2()" << std::endl; }
    
        //! Dispatcher initializer called by singleton initializer
        template <class T>
        static void initDispatcher( Dispatcher<T> *dispatcher )
        {
            // call parent dispatcher initializer
            A::initDispatcher( dispatcher );
            dispatcher->add( "method1", &T::method1 );
            dispatcher->add( "method2", &T::method2 );
        }
    };
    
    int main( int , char *[] )
    {
        A *test1 = new A;
        A *test2 = new B;
        B *test3  = new B;
    
        test1->dispatch( "method1" );
        test1->dispatch( "method2" );
        test1->dispatch( "method3" );
    
        std::cout << std::endl;
    
        test2->dispatch( "method1" );
        test2->dispatch( "method2" );
        test2->dispatch( "method3" );
    
        std::cout << std::endl;
    
        test3->dispatch( "method1" );
        test3->dispatch( "method2" );
        test3->dispatch( "method3" );
    
        return 0;
    }
    

    Here is the program output

    A::method1()
    A::method2()
    A::method3()
    
    B::method1()
    B::method2()
    A::method3()
    
    B::method1()
    B::method2()
    A::method3()
    

    Edit (28 apr): The answers to this related question was enlightening. Using a virtual method with an internal static variable is preferable to using a member pointer variable that needs to be initialized in the constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
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 want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.