Spinning Topp Logo BlackTopp Studios
inc
event.cpp
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_cpp
41 #define _event_cpp
42 
43 #include "event.h"
44 #include "eventsubscriberslot.h"
45 
46 #include <algorithm>
47 
48 namespace Mezzanine
49 {
50  Event::Event(const String& Name) :
51  EventName(Name)
52  { }
53 
55  { this->UnsubscribeAll(); }
56 
57  ///////////////////////////////////////////////////////////////////////////////
58  // Utility
59 
60  const String& Event::GetName() const
61  { return this->EventName; }
62 
63  ///////////////////////////////////////////////////////////////////////////////
64  // Subscription Methods
65 
67  {
68  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
69  {
70  if( (*Current)->GetType() == EventSubscriberSlot::ST_Custom &&
71  static_cast<CustomSubscriberSlot*>( (*Current) )->GetSubscriber() == Subscriber )
72  {
73  // Just escape from here, returning the pre-existing slot
74  return (*Current);
75  }
76  }
77 
78  CustomSubscriberSlot* NewSlot = new CustomSubscriberSlot(this,Subscriber);
79  this->Slots.push_back( NewSlot );
80  return NewSlot;
81  }
82 
84  {
85  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
86  {
87  if( (*Current)->GetType() == EventSubscriberSlot::ST_Functor &&
88  static_cast<FunctorSubscriberSlot*>( (*Current) )->GetFunctor() == Funct )
89  {
90  // Just escape from here, returning the pre-existing slot
91  return (*Current);
92  }
93  }
94 
95  FunctorSubscriberSlot* NewSlot = new FunctorSubscriberSlot(this,Funct,CleanUpAfter);
96  this->Slots.push_back( NewSlot );
97  return NewSlot;
98  }
99 
101  {
102  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
103  {
104  if( (*Current)->GetType() == EventSubscriberSlot::ST_CFunction &&
105  static_cast<CFunctionSubscriberSlot*>( (*Current) )->GetFunction() == CFunct )
106  {
107  // Just escape from here, returning the pre-existing slot
108  return (*Current);
109  }
110  }
111 
112  CFunctionSubscriberSlot* NewSlot = new CFunctionSubscriberSlot(this,CFunct);
113  this->Slots.push_back( NewSlot );
114  return NewSlot;
115  }
116 
118  {
119  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
120  {
121  if( (*Current)->GetType() == EventSubscriberSlot::ST_Script &&
122  static_cast<ScriptSubscriberSlot*>( (*Current) )->GetScript() == SubScript )
123  {
124  // Just escape from here, returning the pre-existing slot
125  return (*Current);
126  }
127  }
128 
129  ScriptSubscriberSlot* NewSlot = new ScriptSubscriberSlot(this,SubScript);
130  this->Slots.push_back( NewSlot );
131  return NewSlot;
132  }
133 
134  ///////////////////////////////////////////////////////////////////////////////
135  // Unsubscribe Methods
136 
138  {
139  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
140  {
141  if( (*Current)->GetType() == EventSubscriberSlot::ST_Custom &&
142  static_cast<CustomSubscriberSlot*>( (*Current) )->GetSubscriber() == Subscriber )
143  {
144  delete (*Current);
145  this->Slots.erase(Current);
146  return;
147  }
148  }
149  }
150 
152  {
153  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
154  {
155  if( (*Current)->GetType() == EventSubscriberSlot::ST_Functor &&
156  static_cast<FunctorSubscriberSlot*>( (*Current) )->GetFunctor() == Funct )
157  {
158  delete (*Current);
159  this->Slots.erase(Current);
160  return;
161  }
162  }
163  }
164 
166  {
167  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
168  {
169  if( (*Current)->GetType() == EventSubscriberSlot::ST_CFunction &&
170  static_cast<CFunctionSubscriberSlot*>( (*Current) )->GetFunction() == CFunct )
171  {
172  delete (*Current);
173  this->Slots.erase(Current);
174  return;
175  }
176  }
177  }
178 
180  {
181  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
182  {
183  if( (*Current)->GetType() == EventSubscriberSlot::ST_Script &&
184  static_cast<ScriptSubscriberSlot*>( (*Current) )->GetScript() == SubScript )
185  {
186  delete (*Current);
187  this->Slots.erase(Current);
188  return;
189  }
190  }
191  }
192 
194  {
195  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
196  {
197  if( (*Current) == SubSlot ) {
198  delete (*Current);
199  this->Slots.erase(Current);
200  return;
201  }
202  }
203  }
204 
206  {
207  Whole PrevSize = this->Slots.size();
208  for( SlotIterator Current = this->Slots.begin() ; Current != this->Slots.end() ; ++Current )
209  { delete (*Current); }
210  this->Slots.clear();
211  return PrevSize;
212  }
213 
214  ///////////////////////////////////////////////////////////////////////////////
215  // Subscriber Access Methods
216 
218  { return this->Slots.begin(); }
219 
221  { return this->Slots.end(); }
222 
224  { return this->Slots.begin(); }
225 
227  { return this->Slots.end(); }
228 
229  ///////////////////////////////////////////////////////////////////////////////
230  // Internal Methods
231 
233  {
234  for( SlotIterator SlotIt = this->Slots.begin() ; SlotIt != this->Slots.end() ; ++SlotIt )
235  {
236  (*SlotIt)->_NotifyEvent(Args);
237  }
238  }
239 }
240 
241 #endif
Basic class definition for functors used by a FunctorSubscriberSlot.
Event(const String &Name)
Class constructor.
Definition: event.cpp:50
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
This is a subscriber slot class that triggers a Free/C-style function.
This is a base class for all classes that subscribe to events.
SlotIterator SubscriberSlotEnd()
Gets an iterator to one passed the last subscriber slot in this event.
Definition: event.cpp:220
A simple reference counting pointer.
Definition: countedptr.h:70
This is a subscriber slot class that passes on the event firing to a custom subscriber class...
const String EventName
The name of this Event.
Definition: event.h:66
SlotIterator SubscriberSlotBegin()
Gets an iterator to the first subscriber slot in this event.
Definition: event.cpp:217
SlotContainer::const_iterator ConstSlotIterator
Const Iterator type for EventSubscriberSlot instances stored by this class.
Definition: event.h:60
The interface for a script.
Definition: script.h:70
Whole UnsubscribeAll()
Unsubscribes all subscribers from this Event.
Definition: event.cpp:205
void( SubscriberFunction)(EventArgumentsPtr Args)
This is a convenience typedef for a c-style method that accepts EventArguments.
This is a subscriber slot class that triggers a provided script.
void Unsubscribe(EventSubscriber *Subscriber)
Unsubscribes a single subscriber from this event.
Definition: event.cpp:137
void _FireEvent(EventArgumentsPtr Args)
Notifies all subscribers of this event that this event is firing.
Definition: event.cpp:232
This class represents a slot in an event that can be subscribed to via subscribers, functors, or methods.
~Event()
Class destructor.
Definition: event.cpp:54
This is a subscriber slot class that makes the appropriate call on a functor.
EventSubscriberSlot * Subscribe(EventSubscriber *Subscriber)
Adds a subscriber to this event.
Definition: event.cpp:66
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
const String & GetName() const
Gets the name of this event.
Definition: event.cpp:60