简体   繁体   中英

C++ Determine type of member function pointer template constant

The piece of code below compiles & works for me in Visual Studio 2010.

Now I'd like to improve 1 little thing, but I just can't find a way. I tried some template template trickery etc, but to no avail.

I want the following line of code:

Sender.AttachListener<XSelectionChanged, TestListener, &TestListener::OnSelectionChanged>(Listener);

to look like

Sender.AttachListener<&TestListener::OnSelectionChanged>(Listener);

That is, I feed a constant "void (TClass::*TMethod)(TEvent& _rEvent)" to the AttachListener template. From the type of this constant I want to get the TClass and TEvent type.

Is this possible? If yes, how?

-Matthias

Code:

#include <stdio.h>
#include <tchar.h>
#include <map>
#include <iostream>


struct XEvent
{};

struct XSelectionChanged : public XEvent
{};

struct XValueChanged : public XEvent
{};

template<typename TEvent, typename TClass>
struct TMethodType
{
    typedef void (TClass::*MethodType)(TEvent& _rEvent);
};

template<typename TEvent, typename TClass, void (TClass::*TMethod)(TEvent& _rEvent)>
struct TBoundMethod
{
    static void Dispatch(TEvent& _rEvent, TClass* _pInstance)
    {
        (_pInstance->*TMethod)(_rEvent);
    }
};


class CEventHost
{
public:
    template <typename TEvent, typename TClass, void (TClass::*TMethod)(TEvent& _rEvent)>
    void AttachListener(TClass& _rInstance)
    {
        TBoundMethod<TEvent, TClass, TMethod>::Dispatch( TEvent(), &_rInstance );
        //m_EventHost.Attach( &TBoundMethod<TClass, TEvent, TMethod>::Dispatch, &_rInstance );
    }

    template <typename TEvent>
    void SendEvent(TEvent& _rEvent)
    {
        //m_EventHost.Send( _rEvent );
    }

protected:
    //SHost m_EventHost;
};

class TestListener
{
public:
    void OnSelectionChanged(XSelectionChanged& _rEvent)
    {
        printf("Selection changed!\n");
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    CEventHost Sender, Sender2;
    TestListener Listener;

    Sender.AttachListener<XSelectionChanged, TestListener, &TestListener::OnSelectionChanged>(Listener);
    Sender.SendEvent(XSelectionChanged());
    Sender2.SendEvent(XSelectionChanged());
    Sender.SendEvent(XValueChanged());

    getchar();

    return 0;
}

Yes, use a meta-function to extract TClass and TEvent, something like this:

template<typename T>
class GetSignature {};

template<typename TClass, typename TEvent>
class GetSignature <void (TClass::*)(TEvent&)>
{
public:
  typedef TClass Class;
  typedef TEvent Event;
};

template <typename T>
void AttachListener(typename GetSignature<T>::Class& _rInstance)
{
    typedef GetSignature<T> Signature;
    typedef void (TClass::*MethodType)(TEvent&);
    TBoundMethod<
        typename Signature::Event,
        typename Signature::Class,
        MethodType
    >::Dispatch( TEvent(), &_rInstance );
    //m_EventHost.Attach( &TBoundMethod<TClass, TEvent, TMethod>::Dispatch, &_rInstance );
}

Warning: untested ;-)

您确实需要std::functionboost::function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM