如何获得包装的可变参数类型?

俊龙珠
template<class... ArgumentType>
    class IEvent
    {
    public:

        virtual ~IEvent() = default;
        virtual bool Dispatch(ArgumentType...) = 0;
    };

    // Event Interfaces Impl
    class IMouseEvent : public IEvent<TypeMouseEvent, int, int>
    {
    public:
        IMouseEvent() : IEvent() { }
        virtual ~IMouseEvent() { }

        // override to get mouse event.
        virtual bool LMouseUp(unsigned x, unsigned y) { return true; }
        virtual bool LMouseDown(unsigned x, unsigned y) { return true; }
        virtual bool RMouseUp(unsigned x, unsigned y) { return true; }
        virtual bool RMouseDown(unsigned x, unsigned y) { return true; }
        virtual bool MouseMove(unsigned x, unsigned y) { return true; }

        bool Dispatch(TypeMouseEvent Type, int x, int y) override;
    };

    class IKeyboardEvent : public IEvent<TypeKeyboardEvent, UINT_PTR>
    {
    public:

        IKeyboardEvent() : IEvent() { }
        virtual ~IKeyboardEvent() { }

        // override to get keyboard event.
        virtual bool vKeyDown(UINT_PTR vKeyCode) { return true; }
        virtual bool vKeyUp(UINT_PTR vKeyCode) { return true; }
        virtual bool KeyCode(UINT_PTR KeyCode) { return true; }

        bool Dispatch(TypeKeyboardEvent Type, UINT_PTR Key) override;
};

template<class EventType, class... ArguementType>
class IEventHandler
{
protected:
    // Cancelable Events
    std::vector<EventType*>              m_PreEvents;
    // None-Cancelable Events
    std::vector<EventType*>              m_Events;

public:

    void RegisterPreEvent(EventType* eventObject) { m_PreEvents.push_back(eventObject); }
    void RegisterEvent(EventType* eventObject) { m_Events.push_back(eventObject); }
    bool DispatchEvent(ArguementType... Arguments);
};

template <class EventType, class ... ArgumentType>
inline bool IEventHandler<EventType, ArgumentType...>::DispatchEvent(ArgumentType... Arguments)
{
    bool isSuccessed = true;

    auto tryDispatch = [Arguments...](auto begin, auto end, bool fKeepExcute) -> bool
    {
        for (auto Iterator = begin; Iterator != end; ++Iterator)
        {
            if ((*Iterator)->Dispatch(Arguments...) == false && fKeepExcute == false)
            {
                return false;
            }
        }
        return true;
    };

    if (tryDispatch(m_PreEvents.begin(), m_PreEvents.end(), false))
    {
        isSuccessed = true;
        tryDispatch(m_Events.begin(), m_Events.end(), true);
    }

    return isSuccessed;
}

我想从IMouseEvent或从IEventHandler的IKeyboardEvent获取ArgumentType。

如何从中获取打包的可变参数模板类型IMouseEvent

我实际上使用像 IMouseEventHandler : public IEventHandler<IMouseEvent, TypeMouseEvent, int, int>

就像class IMouseEventHandler : public IEventHandler<IMouseEvent>从中获取打包的可变参数模板类型IMouseEvent以与DispatchEvent同步一样。(不要制作重复的DispatchEvent方法)

名称

我认为您可以使用以下代码:

template<class... Args>
class IEvent
{
public:
    using event_type = IEvent;
};
template <typename , typename>
struct IEventHandlerImpl;
template <typename Event, typename... Args>
struct IEventHandlerImpl<Event, IEvent<Args...>> {
    Event event;
    bool DispatchEvent(Args... args) {
        return event.Dispatch(args...);
    }
};

template <typename EventType>
struct IEventHandler
     : IEventHandlerImpl <EventType, typename EventType::event_type>
{
};

您可以看一下这个演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章