BsMessageHandler.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Utility/BsModule.h"
  6. namespace bs
  7. {
  8. /** @addtogroup General
  9. * @{
  10. */
  11. /**
  12. * Allows you to transparently pass messages between different systems.
  13. *
  14. * @note Sim thread only.
  15. */
  16. class BS_UTILITY_EXPORT MessageHandler : public Module<MessageHandler>
  17. {
  18. struct MessageHandlerData
  19. {
  20. UINT32 id;
  21. std::function<void()> callback;
  22. };
  23. public:
  24. MessageHandler();
  25. /** Sends a message to all subscribed listeners. */
  26. void send(MessageId message);
  27. /**
  28. * Subscribes a message listener for the specified message. Provided callback will be triggered whenever that
  29. * message gets sent.
  30. *
  31. * @return A handle to the message subscription that you can use to unsubscribe from listening.
  32. */
  33. HMessage listen(MessageId message, std::function<void()> callback);
  34. private:
  35. friend class HMessage;
  36. void unsubscribe(UINT32 handleId);
  37. Map<UINT32, Vector<MessageHandlerData>> mMessageHandlers;
  38. Map<UINT32, UINT32> mHandlerIdToMessageMap;
  39. UINT32 mNextCallbackId;
  40. };
  41. /** @} */
  42. }