C++确定成员函数指针模板常量的类型

发布于 2025-01-01 12:42:03 字数 2225 浏览 1 评论 0原文

下面的代码片段编译&在 Visual Studio 2010 中为我工作。

现在我想改进一件小事,但我就是找不到方法。我尝试了一些模板模板技巧等,但无济于事。

我想要以下代码行:

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

看起来像

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

也就是说,我向 AttachListener 模板提供常量“void (TClass::*TMethod)(TEvent& _rEvent)”。我想从这个常量的类型中获取 TClass 和 TEvent 类型。

这可能吗?如果是,怎么办?

-马蒂亚斯

代码:

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2025-01-08 12:42:03

是的,使用元函数来提取 TClass 和 TEvent,如下所示:

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 );
}

警告:未经测试;-)

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 ;-)

桜花祭 2025-01-08 12:42:03

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

You need std::function or boost::function, really.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文