EventManager.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. //========================================================================
  3. // EventManager.h : implements a multi-listener multi-sender event system
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include <strstream>
  40. #include <functional>
  41. #include <FastDelegate.h>
  42. //---------------------------------------------------------------------------------------------------------------------
  43. // Forward declaration & typedefs
  44. //---------------------------------------------------------------------------------------------------------------------
  45. class IEventData;
  46. typedef unsigned long EventType;
  47. typedef std::shared_ptr<IEventData> IEventDataPtr;
  48. typedef fastdelegate::FastDelegate1<IEventDataPtr> EventListenerDelegate;
  49. // typedef concurrent_queue<IEventDataPtr> ThreadSafeEventQueue;
  50. //---------------------------------------------------------------------------------------------------------------------
  51. // IEventData - Chapter 11, page 310
  52. // Base type for event object hierarchy, may be used itself for simplest event notifications such as those that do
  53. // not carry additional payload data. If any event needs to propagate with payload data it must be defined separately.
  54. //---------------------------------------------------------------------------------------------------------------------
  55. class IEventData
  56. {
  57. public:
  58. virtual ~IEventData(void) {}
  59. virtual const EventType& VGetEventType(void) const = 0;
  60. virtual float GetTimeStamp(void) const = 0;
  61. virtual void VSerialize(std::ostrstream& out) const = 0;
  62. virtual void VDeserialize(std::istrstream& in) = 0;
  63. virtual IEventDataPtr VCopy(void) const = 0;
  64. virtual const char* GetName(void) const = 0;
  65. //GCC_MEMORY_WATCHER_DECLARATION();
  66. };
  67. //---------------------------------------------------------------------------------------------------------------------
  68. // class BaseEventData - Chapter 11, page 311
  69. //---------------------------------------------------------------------------------------------------------------------
  70. class BaseEventData : public IEventData
  71. {
  72. const float m_timeStamp;
  73. public:
  74. explicit BaseEventData(const float timeStamp = 0.0f) : m_timeStamp(timeStamp) { }
  75. // Returns the type of the event
  76. virtual const EventType& VGetEventType(void) const = 0;
  77. float GetTimeStamp(void) const { return m_timeStamp; }
  78. // Serializing for network input / output
  79. virtual void VSerialize(std::ostrstream &out) const { }
  80. virtual void VDeserialize(std::istrstream& in) { }
  81. };
  82. //---------------------------------------------------------------------------------------------------------------------
  83. // IEventManager Description Chapter 11, page 314
  84. //
  85. // This is the object which maintains the list of registered events and their listeners.
  86. //
  87. // This is a many-to-many relationship, as both one listener can be configured to process multiple event types and
  88. // of course multiple listeners can be registered to each event type.
  89. //
  90. // The interface to this construct uses smart pointer wrapped objects, the purpose being to ensure that no object
  91. // that the registry is referring to is destroyed before it is removed from the registry AND to allow for the registry
  92. // to be the only place where this list is kept ... the application code does not need to maintain a second list.
  93. //
  94. // Simply tearing down the registry (e.g.: destroying it) will automatically clean up all pointed-to objects (so long
  95. // as there are no other outstanding references, of course).
  96. //---------------------------------------------------------------------------------------------------------------------
  97. class IEventManager
  98. {
  99. public:
  100. enum eConstants { kINFINITE = 0xffffffff };
  101. explicit IEventManager(const char* pName, bool setAsGlobal);
  102. virtual ~IEventManager(void);
  103. // Registers a delegate function that will get called when the event type is triggered. Returns true if
  104. // successful, false if not.
  105. virtual bool VAddListener(const EventListenerDelegate& eventDelegate, const EventType& type) = 0;
  106. // Removes a delegate / event type pairing from the internal tables. Returns false if the pairing was not found.
  107. virtual bool VRemoveListener(const EventListenerDelegate& eventDelegate, const EventType& type) = 0;
  108. // Fire off event NOW. This bypasses the queue entirely and immediately calls all delegate functions registered
  109. // for the event.
  110. virtual bool VTriggerEvent(const IEventDataPtr& pEvent) const = 0;
  111. // Fire off event. This uses the queue and will call the delegate function on the next call to VTick(), assuming
  112. // there's enough time.
  113. virtual bool VQueueEvent(const IEventDataPtr& pEvent) = 0;
  114. // virtual bool VThreadSafeQueueEvent(const IEventDataPtr& pEvent) = 0;
  115. // Allow for processing of any queued messages, optionally specify a processing time limit so that the event
  116. // processing does not take too long. Note the danger of using this artificial limiter is that all messages
  117. // may not in fact get processed.
  118. //
  119. // returns true if all messages ready for processing were completed, false otherwise (e.g. timeout )
  120. virtual bool VUpdate(unsigned long maxMillis = kINFINITE) = 0;
  121. // Getter for the main global event manager. This is the event manager that is used by the majority of the
  122. // engine, though you are free to define your own as long as you instantiate it with setAsGlobal set to false.
  123. // It is not valid to have more than one global event manager.
  124. static IEventManager* Get(void);
  125. };