MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
event.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 _event_h
41 #define _event_h
42 
43 #include "eventsubscriber.h"
44 #include "eventsubscriberslot.h"
45 
46 namespace Mezzanine
47 {
48  ///////////////////////////////////////////////////////////////////////////////
49  /// @brief This class represents a given event that can be subscribed to and/or fired.
50  /// @details
51  ///////////////////////////////////////
53  {
54  public:
55  /// @brief Basic container type for @ref EventSubscriberSlot storage by this class.
56  typedef std::vector<EventSubscriberSlot*> SlotContainer;
57  /// @brief Iterator type for @ref EventSubscriberSlot instances stored by this class.
58  typedef SlotContainer::iterator SlotIterator;
59  /// @brief Const Iterator type for @ref EventSubscriberSlot instances stored by this class.
60  typedef SlotContainer::const_iterator ConstSlotIterator;
61  /// @brief An std::pair type for working with stored @ref EventSubscriberSlot instances.
62  typedef std::pair<UInt8,EventSubscriberSlot*> SlotPair;
63  protected:
64  /// @internal
65  /// @brief The name of this Event.
67  /// @internal
68  /// @brief A container storing all the EventSubscriberSlot instances to subscribers.
70  public:
71  /// @brief Class constructor.
72  /// @param Name The name to be given to this event.
73  Event(const String& Name);
74  /// @brief Class destructor.
75  ~Event();
76 
77  ///////////////////////////////////////////////////////////////////////////////
78  // Utility
79 
80  /// @brief Gets the name of this event.
81  /// @return Returns a const string reference containing the name of this event.
82  const String& GetName() const;
83 
84  ///////////////////////////////////////////////////////////////////////////////
85  // Subscribe Methods
86 
87  /// @brief Adds a subscriber to this event.
88  /// @param Subscriber The custom event subscriber.
89  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
90  EventSubscriberSlot* Subscribe(EventSubscriber* Subscriber);
91  /// @brief Subscribes a functor object to this event.
92  /// @param Funct The functor to call when the event is fired.
93  /// @param CleanUpAfter Whether or not to delete the functor when this subscriber is no longer subscribed to any events.
94  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
95  EventSubscriberSlot* Subscribe(FunctorSubscriberSlot::FunctorDefinition* Funct, Boolean CleanUpAfter);
96  /// @brief Subscribes a C-style function to this event.
97  /// @param CFunct The C-style function to call when the event is fired.
98  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
100  /// @brief Subscribes a script to this event.
101  /// @param SubScript The subscribed script to execute when the event is fired.
102  /// @return Returns a pointer to the created Subscriber slot for the provided subscriber.
103  EventSubscriberSlot* Subscribe(Scripting::iScript* SubScript);
104 
105  ///////////////////////////////////////////////////////////////////////////////
106  // Unsubscribe Methods
107 
108  /// @brief Unsubscribes a single subscriber from this event.
109  /// @param Subscriber The EventSubscriberSlot (and the subscriber it is holding) to be removed.
110  void Unsubscribe(EventSubscriber* Subscriber);
111  /// @brief Unsubscribes a single subscriber from this event.
112  /// @param Funct The functor to be removed.
113  void Unsubscribe(FunctorSubscriberSlot::FunctorDefinition* Funct);
114  /// @brief Unsubscribes a single subscriber from this event.
115  /// @param CFunct The function to be removed.
116  void Unsubscribe(CFunctionSubscriberSlot::SubscriberFunction* CFunct);
117  /// @brief Unsubscribes a single subscriber from this event.
118  /// @param SubScript The Script to be removed.
119  void Unsubscribe(Scripting::iScript* SubScript);
120  /// @brief Unsubscribes a single subscriber from this event.
121  /// @param SubSlot The EventSubscriberSlot (and the subscriber it is holding) to be removed.
122  void Unsubscribe(EventSubscriberSlot* SubSlot);
123  /// @brief Unsubscribes all subscribers from this Event.
124  /// @return Returns the number of subscribers removed.
125  Whole UnsubscribeAll();
126 
127  ///////////////////////////////////////////////////////////////////////////////
128  // Subscriber Access Methods
129 
130  /// @brief Gets an iterator to the first subscriber slot in this event.
131  /// @return Returns an iterator to the first subscriber slot.
132  SlotIterator SubscriberSlotBegin();
133  /// @brief Gets an iterator to one passed the last subscriber slot in this event.
134  /// @return Returns an iterator to one passed the last subscriber slot.
135  SlotIterator SubscriberSlotEnd();
136  /// @brief Gets an iterator to the first subscriber slot in this event.
137  /// @return Returns a const iterator to the first subscriber slot.
138  ConstSlotIterator SubscriberSlotBegin() const;
139  /// @brief Gets an iterator to one passed the last subscriber slot in this event.
140  /// @return Returns a const iterator to one passed the last subscriber slot.
141  ConstSlotIterator SubscriberSlotEnd() const;
142 
143  ///////////////////////////////////////////////////////////////////////////////
144  // Internal Methods
145 
146  /// @internal
147  /// @brief Notifies all subscribers of this event that this event is firing.
148  /// @param Args The arguments and extra data related to this event.
149  void _FireEvent(const EventArguments& Args);
150  };//Event
151 }//Mezzanine
152 
153 #endif