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;
}
我想从IMventEvent获取ArgumentType或从IEventHandler获取IKeyboardEvent。
如何从IMouseEvent
获取打包的可变参数模板类型?
我实际上正在使用IMouseEventHandler : public IEventHandler<IMouseEvent, TypeMouseEvent, int, int>
就像class IMouseEventHandler : public IEventHandler<IMouseEvent>
一样,从IMouseEvent
获取压缩的可变参数模板类型,以与DispatchEvent同步。 (不要制作重复的DispatchEvent方法)
答案 0 :(得分:0)
我认为您可以使用此代码:
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>
{
};
您可以查看this demo