1

各クラスが異なるDLLからのものである異なるクラスからのコールバックを登録して実行できるコールバックマネージャーを実装しようとしています。

これらの各クラスは、共通の基本クラスから派生します。単一のクラスが以下のようなテンプレート クラスを使用して独自の関数を登録および呼び出す方法は知っていますが、これをどのように適用して、同じコールバック マネージャーを共有する複数のクラスで使用できますか?

どんな助けでも大歓迎です。

file: callbacktemplate.h
------------------------
#include <functional>
#include <string>
template <class cInstance>
class cCallBackManager
{
private:
    typedef void (cInstance::*tFunction)();
    typedef std::map<std::string, tFunction> funcMap;
     funcMap i_funcMap;
public:
    void SetFunPointer(std::string funcName, tFunction function)
    {
        i_funcMap.insert(std::pair<std::string, tFunction>(funcName, function));            
    }

    void GetFunPointer(cInstance& obj) //how to call this without knowing the type?
    {
        for (funcMap::iterator it = i_funcMap.begin();it!=i_funcMap.end(); ++it)
        {
            (obj.*(it->second))(); 
        }
    }
};


file:example.h
---------------
#include "callbacktemplate.h"
class A: public base
{
private:
    cCallBackManager<A> callback;
public:
     A()
     {
         callback.SetFunPointer<A>("eventA", &A::testcallback);
         callback.GetFunPointer(&this);   //how to generalize this so this can be called from the callback manager with the class object?
     };
     ~A(){};
     void testCallback();
};

class B: public base
{
private:
    cCallBackManager<B> callback;
public:
     B()
     {
         callback.SetFunPointer<B>("eventB", &B::testcallback);
     };
     ~B(){};
     void testCallback();
};

file: main.cpp
------------------
#include "derived.h"

int main()
{
 A a;
 B b;
 //create a callback manager to execute the callback?
 callbackmgr.execute() //execute all the callback

 return 0;
}

テンプレート化されたコールバック マネージャーを使用しない場合、SetFunPointer(EVENT_NAME, (Base Class)A::testCallback) のようなものをどのように達成できますか?

4

2 に答える 2

1

みんなありがとう。私はあなたの「ポインター」で何かを思い付くことができました。:)

ファイル: cCallBackInterface.h

template<class cClass>
class cCallBackInterface
{
public:
cCallBackInterface(){};
~cCallBackInterface(){};

typedef void (cClass::*Function)();

cCallBackInterface(cClass* obj, Function _Function)
{
    cInstance = obj;
    m_Function = _Function;
}

void execute()
{
     (cInstance->*m_Function)();
}

private:
cClass* cInstance;
Function m_Function;
};

ファイル: base.h

class BaseModel;
typedef cCallBackInterface<BaseModel> CallBackInterface;
typedef void(BaseModel::*basefn)();

class BaseModel 
{
public:
BaseModel(){};
~BaseModel(){};

}
};


class derived : public BaseModel
{
public:
derived(){};
~derived(){};

void dosomething()
{
    cout << "derived class is doing something." << endl;
}


};

ファイル: main.cpp

int main()
{  
derived a;

std::vector<CallBackInterface> callback;

callback.push_back(CallBackInterface(&a, (basefn)(&derived::Adosomething)));

for(int i = 0; i < callback.size(); i++)
    callback[i].execute();

return 0;
}
于 2012-07-08T03:44:27.580 に答える
0

メンバー関数ポインターの使用に関するこの質問を見ることができます。

つまり、インスタンスとmem-funcポインターが必要であり、どこでも使用できる汎用ポインターを持つことはできません。

于 2012-07-07T11:15:03.537 に答える