MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventpublisher.h
1 // © Copyright 2010 - 2012 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _eventpublisher_h
41 #define _eventpublisher_h
42 
43 #include "event.h"
44 #include "exception.h"
45 
46 namespace Mezzanine
47 {
48  class Event;
49  ///////////////////////////////////////////////////////////////////////////////
50  /// @class EventPublisher
51  /// @headerfile eventpublisher.h
52  /// @brief This is the base class for any class that generates and publishes events to subscribers.
53  /// @details
54  ///////////////////////////////////////
56  {
57  public:
58  /// @brief Basic container type for @ref Event storage by this class.
59  typedef std::map<String,Event*> EventContainer;
60  /// @brief Iterator type for @ref Event instances stored by this class.
61  typedef EventContainer::iterator EventIterator;
62  /// @brief Const Iterator type for @ref Event instances stored by this class.
63  typedef EventContainer::const_iterator ConstEventIterator;
64  protected:
65  /// @internal
66  /// @brief A container storing all the Events published by this class by name.
68 
69  /// @internal
70  /// @brief Creates a new event this Publisher can fire.
71  /// @note If the event already exists, this will return the created event instead.
72  /// @param EventName The name to be given to the new event.
73  /// @return Returns a pointer to the created or existing event.
74  Event* AddEvent(const String& EventName);
75  /// @internal
76  /// @brief Fires an event.
77  /// @param Args The arguments/event specific data related to this event.
78  void FireEvent(const EventArguments& Args);
79  /// @internal
80  /// @brief Removes an existing event in this Publisher.
81  /// @param EventName The name of the event to be removed.
82  void RemoveEvent(const String& EventName);
83  /// @internal
84  /// @brief Removes all events in this Publisher.
85  void RemoveAllEvents();
86  public:
87  /// @brief Class constructor.
89  /// @brief Class destructor.
90  virtual ~EventPublisher();
91 
92  ///////////////////////////////////////////////////////////////////////////////
93  // Utility
94 
95  /// @brief Gets an event in this publisher.
96  /// @param EventName The name of the event to retrieve.
97  /// @return Returns a pointer to the requested event.
98  Event* GetEvent(const String& EventName) const;
99  /// @brief Gets an event in this publisher.
100  /// @exception This version differs from the non-except version in that if it fails to find the event specified
101  /// it will throw a "II_IDENTITY_NOT_FOUND_EXCEPTION".
102  /// @param EventName The name of the event to retrieve.
103  /// @return Returns a pointer to the requested event.
104  Event* GetEventExcept(const String& EventName) const;
105 
106  ///////////////////////////////////////////////////////////////////////////////
107  // Subscribe Methods
108 
109  /// @brief Adds a subscriber to this event.
110  /// @param EventName The name of the event to subscribe to.
111  /// @param Sub The custom event subscriber.
112  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
113  EventSubscriberSlot* Subscribe(const String& EventName, EventSubscriber* Sub);
114  /// @brief Subscribes a functor object to this event.
115  /// @param EventName The name of the event to subscribe to.
116  /// @param Funct The functor to call when the event is fired.
117  /// @param CleanUpAfter Whether or not to delete the functor when this subscriber is no longer subscribed to any events.
118  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
119  EventSubscriberSlot* Subscribe(const String& EventName, FunctorSubscriberSlot::FunctorDefinition* Funct, Boolean CleanUpAfter);
120  /// @brief Subscribes a C-style function to this event.
121  /// @param EventName The name of the event to subscribe to.
122  /// @param CFunct The C-style function to call when the event is fired.
123  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
124  EventSubscriberSlot* Subscribe(const String& EventName, CFunctionSubscriberSlot::SubscriberFunction* CFunct);
125  /// @brief Subscribes a script to this event.
126  /// @param EventName The name of the event to subscribe to.
127  /// @param SubScript The subscribed script to execute when the event is fired.
128  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
129  EventSubscriberSlot* Subscribe(const String& EventName, Scripting::iScript* SubScript);
130 
131  ///////////////////////////////////////////////////////////////////////////////
132  // Unsubscribe Methods
133 
134  /// @brief Unsubscribes a single subscriber all events in this publisher.
135  /// @param Subscriber The EventSubscriberSlot (and the subscriber it is holding) to be removed.
136  void Unsubscribe(EventSubscriber* Subscriber);
137  /// @brief Unsubscribes a single subscriber all events in this publisher.
138  /// @param Funct The functor to be removed.
139  void Unsubscribe(FunctorSubscriberSlot::FunctorDefinition* Funct);
140  /// @brief Unsubscribes a single subscriber from all events in this publisher.
141  /// @param CFunct The function to be removed.
142  void Unsubscribe(CFunctionSubscriberSlot::SubscriberFunction* CFunct);
143  /// @brief Unsubscribes a single subscriber from all events in this publisher.
144  /// @param SubScript The Script to be removed.
145  void Unsubscribe(Scripting::iScript* SubScript);
146  /// @brief Unsubscribes a single subscriber from all events in this publisher.
147  /// @param SubSlot The EventSubscriberSlot (and the subscriber it is holding) to be removed.
148  void Unsubscribe(EventSubscriberSlot* SubSlot);
149  /// @brief Unsubscribes all subscribers from all events in this publisher.
150  /// @return Returns the number of subscribers removed.
151  Whole UnsubscribeAll();
152 
153  /// @brief Unsubscribes a single subscriber from the named event.
154  /// @param EventName The name of the event to unsubscribe from.
155  /// @param Subscriber The EventSubscriberSlot (and the subscriber it is holding) to be removed.
156  void Unsubscribe(const String& EventName, EventSubscriber* Subscriber);
157  /// @brief Unsubscribes a single subscriber from the named event.
158  /// @param EventName The name of the event to unsubscribe from.
159  /// @param Funct The functor to be removed.
160  void Unsubscribe(const String& EventName, FunctorSubscriberSlot::FunctorDefinition* Funct);
161  /// @brief Unsubscribes a single subscriber from the named event.
162  /// @param EventName The name of the event to unsubscribe from.
163  /// @param CFunct The function to be removed.
164  void Unsubscribe(const String& EventName, CFunctionSubscriberSlot::SubscriberFunction* CFunct);
165  /// @brief Unsubscribes a single subscriber from the named event.
166  /// @param EventName The name of the event to unsubscribe from.
167  /// @param SubScript The Script to be removed.
168  void Unsubscribe(const String& EventName, Scripting::iScript* SubScript);
169  /// @brief Unsubscribes a single subscriber from the named event.
170  /// @param EventName The name of the event to unsubscribe from.
171  /// @param SubSlot The EventSubscriberSlot (and the subscriber it is holding) to be removed.
172  void Unsubscribe(const String& EventName, EventSubscriberSlot* SubSlot);
173  /// @brief Unsubscribes all subscribers from the named Event.
174  /// @param EventName The name of the event to unsubscribe from.
175  /// @return Returns the number of subscribers removed.
176  Whole UnsubscribeAll(const String& EventName);
177  };//EventPublisher
178 }//Mezzanine
179 
180 #endif