MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventsubscriberslot.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 _eventsubscriberslot_h
41 #define _eventsubscriberslot_h
42 
43 #include "eventarguments.h"
44 
45 namespace Mezzanine
46 {
47  class Event;
48  class EventSubscriber;
49  namespace Scripting
50  {
51  class iScript;
52  }
53  ///////////////////////////////////////////////////////////////////////////////
54  /// @brief This class represents a slot in an event that can be subscribed to via subscribers, functors, or methods.
55  /// @details
56  ///////////////////////////////////////
58  {
59  public:
60  /// @brief This enum is used to describe the type of SubscriberSlot an instance is, to be used for casting.
61  enum SlotType
62  {
63  ST_Custom = 1,
64  ST_Functor = 2,
65  ST_CFunction = 3,
66  ST_Script = 4,
67  ST_MemberFunction = 5
68  };
69  protected:
70  /// @internal
71  /// @brief A pointer to the connected event.
73  public:
74  /// @brief Class constructor.
75  /// @param Ev The event this subscriber slot belongs to.
77  /// @brief Class destructor.
78  virtual ~EventSubscriberSlot();
79 
80  ///////////////////////////////////////////////////////////////////////////////
81  // Utility
82 
83  /// @brief Gets the set of events the subscriber is subscribed to.
84  /// @return Returns a reference to the set of events this is subscribed to.
85  Event* GetEvent() const;
86  /// @brief Gets the type of subscriber slot this is.
87  /// @return Returns a SlotType enum value representing the type of subscriber slot this is.
88  virtual SlotType GetType() const = 0;
89 
90  ///////////////////////////////////////////////////////////////////////////////
91  // Internal Methods
92 
93  /// @internal
94  /// @brief Notifies this subscriber of an event being fired.
95  /// @param Args The arguments containing specific information regarding this event.
96  virtual void _NotifyEvent(const EventArguments& Args) = 0;
97  };//EventSubscriberSlot
98 
99  ///////////////////////////////////////////////////////////////////////////////
100  /// @brief This is a subscriber slot class that passes on the event firing to a custom subscriber class.
101  /// @details
102  ///////////////////////////////////////
104  {
105  protected:
106  /// @internal
107  /// @brief A pointer to the custom subscriber that the event will be passed on to.
109  public:
110  /// @brief Class constructor.
111  /// @param Ev The event this subscriber slot belongs to.
112  /// @param SubScript The subscriber script to be called when the event is fired.
114  /// @brief Class destructor.
115  virtual ~CustomSubscriberSlot();
116 
117  ///////////////////////////////////////////////////////////////////////////////
118  // Utility
119 
120  /// @brief Gets a pointer to the subscriber used by this subscriber slot.
121  /// @return Returns a pointer to the custom subscriber being used in this subscriber slot.
122  EventSubscriber* GetSubscriber() const;
123  /// @copydoc EventSubscriberSlot::GetType() const
124  virtual SlotType GetType() const;
125 
126  ///////////////////////////////////////////////////////////////////////////////
127  // Internal Methods
128 
129  /// @copydoc EventSubscriberSlot::_NotifyEvent(const EventArguments& Args)
130  virtual void _NotifyEvent(const EventArguments& Args);
131  };// ©ustomSubscriberSlot
132 
133  ///////////////////////////////////////////////////////////////////////////////
134  /// @brief This is a subscriber slot class that makes the appropriate call on a functor.
135  /// @details
136  ///////////////////////////////////////
138  {
139  public:
140  /// @brief Basic class definition for functors used by this subscriber slot.
142  {
143  public:
144  /// @brief Class destructor.
145  virtual ~FunctorDefinition() { };
146  /// @brief Executes subscriber specific functionality when the event is fired.
147  /// @param Args The arguments that describe the fired event.
148  virtual void operator()(const EventArguments& Args) = 0;
149  };//FunctorDefinition
150  protected:
151  /// @internal
152  /// @brief A pointer to the functor to be called when the event is fired.
154  /// @internal
155  /// @brief Stores whether or not the functor is to be deleted when this subscriber is destructed.
156  Boolean CleanUp;
157  public:
158  /// @brief Class constructor.
159  /// @param Ev The event this subscriber slot belongs to.
160  /// @param Funct The Functor object to be called when the event is fired.
161  /// @param CleanUpAfter Whether or not to delete the functor when this object is deleted.
162  FunctorSubscriberSlot(Event* Ev, FunctorDefinition* Funct, bool CleanUpAfter);
163  /// @brief Class destructor.
164  virtual ~FunctorSubscriberSlot();
165 
166  ///////////////////////////////////////////////////////////////////////////////
167  // Utility
168 
169  /// @brief Gets a pointer to the functor used by this subscriber slot.
170  /// @return Returns a pointer to the functor subscriber being used in this subscriber slot.
171  FunctorDefinition* GetFunctor() const;
172  /// @copydoc EventSubscriberSlot::GetType() const
173  virtual SlotType GetType() const;
174 
175  ///////////////////////////////////////////////////////////////////////////////
176  // Internal Methods
177 
178  /// @copydoc EventSubscriberSlot::_NotifyEvent(const EventArguments& Args)
179  virtual void _NotifyEvent(const EventArguments& Args);
180  };//FunctorSubscriberSlot
181 
182  ///////////////////////////////////////////////////////////////////////////////
183  /// @brief This is a subscriber slot class that triggers a Free/C-style function.
184  /// @details
185  ///////////////////////////////////////
187  {
188  public:
189  /// @brief This is a convenience typedef for a c-style method that accepts EventArguments.
190  typedef void (SubscriberFunction)(const EventArguments& Args);
191  protected:
192  /// @internal
193  /// @brief A pointer to the c-style function to be called when the event is fired.
194  SubscriberFunction* Function;
195  public:
196  /// @brief Class constructor.
197  /// @param Ev The event this subscriber slot belongs to.
198  /// @param Funct The C-style function to be called when the event is fired.
199  CFunctionSubscriberSlot(Event* Ev, SubscriberFunction* Funct);
200  /// @brief Class destructor.
201  virtual ~CFunctionSubscriberSlot();
202 
203  ///////////////////////////////////////////////////////////////////////////////
204  // Utility
205 
206  /// @brief Gets a pointer to the function used by this subscriber slot.
207  /// @return Returns a pointer to the function subscriber being used in this subscriber slot.
208  SubscriberFunction* GetFunction() const;
209  /// @copydoc EventSubscriberSlot::GetType() const
210  virtual SlotType GetType() const;
211 
212  ///////////////////////////////////////////////////////////////////////////////
213  // Internal Methods
214 
215  /// @copydoc EventSubscriberSlot::_NotifyEvent(const EventArguments& Args)
216  virtual void _NotifyEvent(const EventArguments& Args);
217  };// ©FunctionSubscriberSlot
218 
219  ///////////////////////////////////////////////////////////////////////////////
220  /// @brief This is a subscriber slot class that triggers a provided script.
221  /// @details
222  ///////////////////////////////////////
224  {
225  protected:
226  /// @internal
227  /// @brief A pointer to the script to be executed when the event is fired.
229  public:
230  /// @brief Class constructor.
231  /// @param Ev The event this subscriber slot belongs to.
232  /// @param SubScript The subscriber script to be called when the event is fired.
234  /// @brief Class destructor.
235  virtual ~ScriptSubscriberSlot();
236 
237  ///////////////////////////////////////////////////////////////////////////////
238  // Utility
239 
240  /// @brief Gets a pointer to the function used by this subscriber slot.
241  /// @return Returns a pointer to the function subscriber being used in this subscriber slot.
242  Scripting::iScript* GetScript() const;
243  /// @copydoc EventSubscriberSlot::GetType() const
244  virtual SlotType GetType() const;
245 
246  ///////////////////////////////////////////////////////////////////////////////
247  // Internal Methods
248 
249  /// @copydoc EventSubscriberSlot::_NotifyEvent(const EventArguments& Args)
250  virtual void _NotifyEvent(const EventArguments& Args);
251  };//ScriptSubscriberSlot
252 }//Mezzanine
253 
254 #endif