Spinning Topp Logo BlackTopp Studios
inc
event.h
1 // © Copyright 2010 - 2016 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.
69  SlotContainer Slots;
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(FunctorEventSubscriber* Funct, Boole 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(FunctorEventSubscriber* 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 #ifndef SWIG // Since these functions differ only by constness, they make no sense to most(all?) scripting languages
137  /// @brief Gets an iterator to the first subscriber slot in this event.
138  /// @return Returns a const iterator to the first subscriber slot.
139  ConstSlotIterator SubscriberSlotBegin() const;
140  /// @brief Gets an iterator to one passed the last subscriber slot in this event.
141  /// @return Returns a const iterator to one passed the last subscriber slot.
142  ConstSlotIterator SubscriberSlotEnd() const;
143 #endif
144 
145  ///////////////////////////////////////////////////////////////////////////////
146  // Internal Methods
147 
148  /// @internal
149  /// @brief Notifies all subscribers of this event that this event is firing.
150  /// @param Args The arguments and extra data related to this event.
151  void _FireEvent(EventArgumentsPtr Args);
152  };//Event
153 }//Mezzanine
154 
155 #endif
std::pair< UInt8, EventSubscriberSlot * > SlotPair
An std::pair type for working with stored EventSubscriberSlot instances.
Definition: event.h:62
Basic class definition for functors used by a FunctorSubscriberSlot.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
This class represents a given event that can be subscribed to and/or fired.
Definition: event.h:52
This is a base class for all classes that subscribe to events.
A simple reference counting pointer.
Definition: countedptr.h:70
const String EventName
The name of this Event.
Definition: event.h:66
SlotContainer::const_iterator ConstSlotIterator
Const Iterator type for EventSubscriberSlot instances stored by this class.
Definition: event.h:60
std::vector< EventSubscriberSlot * > SlotContainer
Basic container type for EventSubscriberSlot storage by this class.
Definition: event.h:56
The interface for a script.
Definition: script.h:70
void( SubscriberFunction)(EventArgumentsPtr Args)
This is a convenience typedef for a c-style method that accepts EventArguments.
This class represents a slot in an event that can be subscribed to via subscribers, functors, or methods.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
SlotContainer::iterator SlotIterator
Iterator type for EventSubscriberSlot instances stored by this class.
Definition: event.h:58
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
SlotContainer Slots
A container storing all the EventSubscriberSlot instances to subscribers.
Definition: event.h:69