AnnUserSpaceEventLauncher

AnnUserSpaceEventLauncher is a utility class that permit you to send AnnUserSpaceEvent via the AnnEventManager.

Every AnnUserSpaceSubSystem inherit from this class, and provide you as interface a protected final override of the dispatchEvent() method.

Example

Here's a practical example on how to setup a custom event, a listener for it, and a subsystem that sends this event each few seconds.

Here's the class definition for the event, note the string passed to the parent AnnUserSpaceEvent constructor. This is what will permit you to identify the type of the event.

class TextMessageEvent : public AnnUserSpaceEvent
{
public:
	TextMessageEvent(string message) : AnnUserSpaceEvent("TextMessage"),
		enclosedMessage(message)
	{
	}
 
	~TextMessageEvent()
	{
		//This is just to check that everything is in order
		//AnnDebug() << "event destructed!";
	}
 
	string getMessage() const { return enclosedMessage; };
 
private:
	string enclosedMessage;
};

Here's a listener that will listen to this even via overriding the “EventFromUserSubsystem” method. Not that we're using an hashed version of the same “identifier string” as above.

class SomeEventListener : LISTENER
{
public:
	SomeEventListener() : constructListener(),
		hashTypeCheckTextEvent(AnnGetStringUtility()->hash("TextMessage"))
	{
	}
 
	void EventFromUserSubsystem(AnnUserSpaceEvent& e, AnnUserSpaceEventLauncher* origin) override
	{
		if (e.getType() == hashTypeCheckTextEvent)
			AnnDebug() << "SomeEventListener got the TextMessageEvent \""
			<< dynamic_cast<TextMessageEvent&>(e).getMessage()
			<< "\" from " << origin;
	}
 
private:
	const AnnUserSpaceEvent::AnnUserSpaceEventTypeHash hashTypeCheckTextEvent;
};

Here's a user space subsystem that send this custom event every few seconds:

class SomeSubSystem : public AnnUserSubSystem
{
public:
	SomeSubSystem(double updateRate = 3) : AnnUserSubSystem("Useless Subsystem"),
		now(0),
		last(0),
		delay(updateRate)
	{
	}
 
	void update() override
	{
		now = AnnGetEngine()->getTimeFromStartupSeconds();
 
		if (now - last > delay)
		{
			static unsigned int count = 0;
			last = now;
			dispatchEvent(make_shared<TextMessageEvent>("Useless message! " + to_string(++count)));
		}
	}
 
	bool needUpdate() override
	{
		return true;
	}
 
private:
	double now, last, delay;
};