我爲我的遊戲寫了一個事件系統。它工作正常,但有一個很大的缺陷 - 它不是類型安全的,因此需要訂閱的回調函數手動強制接收基本事件。現在,我正在嘗試使用模板實現類型安全版本。我通常對這個話題很滿意,但顯然不是專家。類型安全的事件系統實現
首先,這裏是一些代碼演示如何我想使用的事件:
定義的衍生事件
// Derived Events
class ClickEvent : public Event
{
public:
float x;
float y;
};
class RenderNodeCreatedEvent : public Event
{
public:
unsigned long long int renderNodeId;
};
創建它們並(在主程序例如用於測試目的)使用它們
// Create the on event functions
std::function<void(const ClickEvent &)> onClickFunction = [](const ClickEvent & event)
{
std::cout << std::endl << "Mouse clicked at position: " << event.x << event.y;
};
std::function<void(const RenderNodeCreatedEvent &)> onRenderNodeCreatedFunction = [](const RenderNodeCreatedEvent & event)
{
std::cout << std::endl << "Render node created with id: " << event.renderNodeId;
};
// Create the events
ClickEvent clickEvent;
clickEvent.x = 300.f;
clickEvent.y = 255.5f;
RenderNodeCreatedEvent renderNodeCreatedEvent;
renderNodeCreatedEvent.renderNodeId = 26234628374324;
// Create the event manager and subscribe the event functions
EventManager eventManager;
eventManager.Subscribe(onClickFunction);
eventManager.Subscribe(onRenderNodeCreatedFunction);
// Raise the events
eventManager.Raise(clickEvent);
eventManager.Raise(renderNodeCreatedEvent);
這是我試圖產生std::size_t
類型的唯一ID爲每個派生事件類:
class BaseEvent
{
public:
typedef std::size_t ID;
BaseEvent() = default;
virtual ~BaseEvent() = default;
protected:
static ID GetNextID()
{
static ID id = 0;
return id++;
}
};
class Event : public BaseEvent
{
public:
Event() = default;
virtual ~Event() = default;
// Sets a unique id for the event the first time this function is called
ID GetID()
{
static ID id = BaseEvent::GetNextID();
return id;
}
};
最後,這是我的(可怕的)嘗試在事件管理器,可以提供上述功能。我非常努力地管理正確的投射和/或存儲不同類型的回調函數。回調包裝器不起作用 - 它只是一個基本的想法,我必須解決這個問題,所以我把它包含在帖子中。
class EventManager
{
public:
// Define the template callback type
template <class DerivedEvent>
using TCallback = std::function<void(const DerivedEvent &)>;
public:
EventManager() = default;
~EventManager() = default;
template<class DerivedEvent>
void Subscribe(TCallback<DerivedEvent> callback)
{
// Get the index of the callback list this callback will be added to
Event::ID id = DerivedEvent::GetID();
// This won't work sinve TCallback is a different type than TCallback<Event>
callbackListList[id].push_back(callback);
}
template <class DerivedEvent>
void Raise(DerivedEvent event)
{
// Get the type of the event and therefore the index in the callbackListList
Event::ID = DerivedEvent::GetID();
/// How to cast the events back
// Get the respective list of callback functions
std::vector<TCallback<DerivedEvent>> /*&*/ callbackList;
// Create a callback wrapper of with the type 'derived event' ?????
CallbackWrapper<DerivedEvent> callbackWrapper(/*derived callback*/);
// Call the callback wrapper using the base event ?????
}
template <typename DerivedEvent>
class CallbackWrapper
{
public:
CallbackWrapper(TCallback<DerivedEvent> callback) : callback(callback) {}
void operator() (const Event & event)
{
callback(static_cast<const Event<DerivedEvent> &>(event).event);
}
private:
TCallback<DerivedEvent> callback;
};
private:
std::vector<std::vector<TCallback<Event>>> callbackListList;
};
我知道這是很多代碼,但我覺得這是展示我所談論的最簡單的方法。
感謝您的幫助,阿德里安
編輯: 事件類都必須聲明爲模板,以獲得唯一ID派生類型。現在
template <class DerivedEvent>
class Event : public BaseEvent
從事件繼承時:
class ClickEvent : public Event<ClickEvent>
class RenderNodeCreatedEvent : public Event<RenderNodeCreatedEvent>
最後,eventmanager進行只能存儲類型的回調的矢量的矢量BaseEvent
private:
std::vector<std::vector<TCallback<BaseEvent>>> callbackListList;
也許相關:https://stackoverflow.com/questions/47337029/handling-function-pointer-with-covariant-types-uniformly-how-to-call-callbacks – geza
很顯然,你不能使用類型** BaseEvent **的回調向量。將對象存儲到基礎對象類型中時,會發生切片,並且會丟失派生對象及其數據的類型。順便說一句,通過回調類型來管理經理可能更容易。也就是說,'EventManager'可能應該是一個管理特定回調類型的模板。這樣,就很容易實現類型安全。 – Phil1970
@ Phil1970是的,我將不得不在列表中存儲指針。關於使事件管理器成爲一個模板的觀點:由於我想要的是單一事件系統(如果需要的話,消息系統),所有系統都可以用來通信。 –