C++中的动态绑定

chm*_*ike 10 c++ methods binding

我正在实现一个像服务器一样的CORBA.每个类都有远程可调用方法和一个带有两个可能输入的调度方法,一个标识方法的字符串或一个整数,它将是表中方法的索引.字符串到相应整数的映射将由映射实现.

调用者将在第一次调用时发送字符串并使用响应返回整数,以便它只需在后续调用时发送整数.这只是一个小优化.可以根据需要由服务器对象动态分配整数.服务器类可以从具有重写的虚拟方法的另一个类派生.

什么是定义方法绑定和调度方法的简单通用方法?

编辑:方法具有相同的签名(没有重载).这些方法没有参数并返回一个布尔值.它们可以是静态的,虚拟的或非静态的,覆盖基类方法与否.绑定必须正确处理方法覆盖.

该字符串是类层次结构绑定.如果我们有由字符串"A.foo"标识的A :: foo(),并且类B继承A并覆盖方法A :: foo(),它仍将被标识为"A.foo",但是如果服务器是A对象,调度程序将调用A :: foo,如果它是B对象,则调用B :: foo.

编辑(6 apr):换句话说,我需要使用动态调度方法实现我自己的虚方法表(vftable),使用字符串键来标识要调用的方法.vftable应该在同一个类的对象之间共享,并且与多态性的行为一样(继承的方法覆盖).

编辑(28 apr):看下面我自己的答案和最后的编辑.

chm*_*ike 0

这是我的实际方法的一个例子。它只是有效(c),但我很确定存在一种更干净、更好的方法。它按原样使用 g++ 4.4.2 编译并运行。删除构造函数中的指令会很棒,但我找不到实现此目的的方法。Dispatcher 类基本上是一个可分派方法表,每个实例都必须在其表上有一个指针。

注意:此代码将隐式地将所有分派方法设为虚拟。

#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;
}
Run Code Online (Sandbox Code Playgroud)

这是程序输出

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

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

B::method1()
B::method2()
A::method3()
Run Code Online (Sandbox Code Playgroud)

编辑(4 月 28 日):这个相关问题的答案很有启发性。使用带有内部静态变量的虚拟方法比使用需要在构造函数中初始化的成员指针变量更好。