MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventpublisher.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 _eventpublisher_cpp
41 #define _eventpublisher_cpp
42 
43 #include "eventpublisher.h"
44 
45 namespace Mezzanine
46 {
48  { }
49 
51  { this->RemoveAllEvents(); }
52 
54  {
55  Event* Ev = this->GetEvent(EventName);
56  if( Ev != NULL ) {
57  return Ev;
58  }else{
59  Ev = new Event(EventName);
60  this->Events[EventName] = Ev;
61  return Ev;
62  }
63  }
64 
66  { this->GetEventExcept(Args.EventName)->_FireEvent(Args); }
67 
68  void EventPublisher::RemoveEvent(const String& EventName)
69  {
70  EventIterator EvIt = this->Events.find(EventName);
71  if( EvIt != this->Events.end() ) {
72  delete (*EvIt).second;
73  this->Events.erase(EvIt);
74  }
75  }
76 
78  {
79  for( EventIterator EvIt = this->Events.begin() ; EvIt != this->Events.end() ; ++EvIt )
80  {
81  delete (*EvIt).second;
82  }
83  this->Events.clear();
84  }
85 
86  ///////////////////////////////////////////////////////////////////////////////
87  // Utility
88 
89  Event* EventPublisher::GetEvent(const String& EventName) const
90  {
91  ConstEventIterator EvIt = this->Events.find(EventName);
92  if( EvIt != this->Events.end() ) return (*EvIt).second;
93  else return NULL;
94  }
95 
96  Event* EventPublisher::GetEventExcept(const String& EventName) const
97  {
98  ConstEventIterator EvIt = this->Events.find(EventName);
99  if( EvIt != this->Events.end() ) {
100  return (*EvIt).second;
101  }else{
102  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,"Event name \"" + EventName + "\" not found in publisher.");
103  }
104  return NULL;
105  }
106 
107  ///////////////////////////////////////////////////////////////////////////////
108  // Subscribe Methods
109 
111  { return this->GetEventExcept(EventName)->Subscribe(Sub); }
112 
114  { return this->GetEventExcept(EventName)->Subscribe(Funct,CleanUpAfter); }
115 
117  { return this->GetEventExcept(EventName)->Subscribe(CFunct); }
118 
120  { return this->GetEventExcept(EventName)->Subscribe(SubScript); }
121 
122  ///////////////////////////////////////////////////////////////////////////////
123  // Unsubscribe Methods
124 
126  {
127  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
128  { (*EventIt).second->Unsubscribe(Subscriber); }
129  }
130 
132  {
133  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
134  { (*EventIt).second->Unsubscribe(Funct); }
135  }
136 
138  {
139  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
140  { (*EventIt).second->Unsubscribe(CFunct); }
141  }
142 
144  {
145  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
146  { (*EventIt).second->Unsubscribe(SubScript); }
147  }
148 
150  {
151  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
152  { (*EventIt).second->Unsubscribe(SubSlot); }
153  }
154 
156  {
157  Whole Ret = 0;
158  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
159  { Ret += (*EventIt).second->UnsubscribeAll(); }
160  return Ret;
161  }
162 
163  void EventPublisher::Unsubscribe(const String& EventName, EventSubscriber* Subscriber)
164  { this->GetEventExcept(EventName)->Unsubscribe(Subscriber); }
165 
167  { this->GetEventExcept(EventName)->Unsubscribe(Funct); }
168 
170  { this->GetEventExcept(EventName)->Unsubscribe(CFunct); }
171 
172  void EventPublisher::Unsubscribe(const String& EventName, Scripting::iScript* SubScript)
173  { this->GetEventExcept(EventName)->Unsubscribe(SubScript); }
174 
175  void EventPublisher::Unsubscribe(const String& EventName, EventSubscriberSlot* SubSlot)
176  { this->GetEventExcept(EventName)->Unsubscribe(SubSlot); }
177 
179  { return this->GetEventExcept(EventName)->UnsubscribeAll(); }
180 }//Mezzanine
181 
182 #endif