I was wanting to take a stab at creating an EventAggregator in c++. It would work like so. Each game object that wanted to be notified of specific game events would subscribe to the event using the event aggregator like so.
struct SomeSubscriber : Subscriber
{
SomeSubscriber(std::shared_ptr<EventAggregator> ea)
{
ea->Subscribe(this, L"EnemySpotted");
}
};
All Subscribers would derive from the following class
struct Subscriber
{
virtual ~Subscriber()
{
}
virtual void Handle(Event & e) = 0;
};
The Event Aggregator would look something like this
struct EventAggregator
{
std::map<std::wstring, std::vector<std::weak_ptr<Subscriber>>> m_subscriptions;
void Publish(Event & e);
void Subscribe(std::weak_ptr<Subscriber> subscriber, std::wstring eventType);
};
The problem i have is i would like the gameobjects to subscribe to the event aggregator in their constructor but i'm not sure how to do this since the EventAggregator expects a shared_ptr