MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
event.cpp
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_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