Spinning Topp Logo BlackTopp Studios
inc
eventpublisher.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 _eventpublisher_cpp
41 #define _eventpublisher_cpp
42 
43 #include "eventpublisher.h"
44 
45 namespace Mezzanine
46 {
48  MuteEvents(false)
49  { }
50 
52  { this->RemoveAllEvents(); }
53 
55  {
56  Event* Ev = this->GetEvent(EventName);
57  if( Ev != NULL ) {
58  return Ev;
59  }else{
60  Ev = new Event(EventName);
61  this->Events[EventName] = Ev;
62  return Ev;
63  }
64  }
65 
67  {
68  if( !this->MuteEvents ) {
69  this->GetEventExcept(Args->EventName)->_FireEvent(Args);
70  }
71  }
72 
73  void EventPublisher::RemoveEvent(const String& EventName)
74  {
75  EventIterator EvIt = this->Events.find(EventName);
76  if( EvIt != this->Events.end() ) {
77  delete (*EvIt).second;
78  this->Events.erase(EvIt);
79  }
80  }
81 
83  {
84  for( EventIterator EvIt = this->Events.begin() ; EvIt != this->Events.end() ; ++EvIt )
85  {
86  delete (*EvIt).second;
87  }
88  this->Events.clear();
89  }
90 
91  ///////////////////////////////////////////////////////////////////////////////
92  // Utility
93 
95  { this->MuteEvents = Mute; }
96 
98  { return this->MuteEvents; }
99 
100  Event* EventPublisher::GetEvent(const String& EventName) const
101  {
102  ConstEventIterator EvIt = this->Events.find(EventName);
103  if( EvIt != this->Events.end() ) return (*EvIt).second;
104  else return NULL;
105  }
106 
107  Event* EventPublisher::GetEventExcept(const String& EventName) const
108  {
109  ConstEventIterator EvIt = this->Events.find(EventName);
110  if( EvIt != this->Events.end() ) {
111  return (*EvIt).second;
112  }else{
113  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,"Event name \"" + EventName + "\" not found in publisher.");
114  }
115  return NULL;
116  }
117 
118  ///////////////////////////////////////////////////////////////////////////////
119  // Subscribe Methods
120 
122  { return this->GetEventExcept(EventName)->Subscribe(Sub); }
123 
125  { return this->GetEventExcept(EventName)->Subscribe(Funct,CleanUpAfter); }
126 
128  { return this->GetEventExcept(EventName)->Subscribe(CFunct); }
129 
131  { return this->GetEventExcept(EventName)->Subscribe(SubScript); }
132 
133  ///////////////////////////////////////////////////////////////////////////////
134  // Unsubscribe Methods
135 
137  {
138  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
139  { (*EventIt).second->Unsubscribe(Subscriber); }
140  }
141 
143  {
144  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
145  { (*EventIt).second->Unsubscribe(Funct); }
146  }
147 
149  {
150  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
151  { (*EventIt).second->Unsubscribe(CFunct); }
152  }
153 
155  {
156  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
157  { (*EventIt).second->Unsubscribe(SubScript); }
158  }
159 
161  {
162  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
163  { (*EventIt).second->Unsubscribe(SubSlot); }
164  }
165 
167  {
168  Whole Ret = 0;
169  for( EventIterator EventIt = this->Events.begin() ; EventIt != this->Events.begin() ; ++EventIt )
170  { Ret += (*EventIt).second->UnsubscribeAll(); }
171  return Ret;
172  }
173 
174  void EventPublisher::Unsubscribe(const String& EventName, EventSubscriber* Subscriber)
175  { this->GetEventExcept(EventName)->Unsubscribe(Subscriber); }
176 
178  { this->GetEventExcept(EventName)->Unsubscribe(Funct); }
179 
181  { this->GetEventExcept(EventName)->Unsubscribe(CFunct); }
182 
183  void EventPublisher::Unsubscribe(const String& EventName, Scripting::iScript* SubScript)
184  { this->GetEventExcept(EventName)->Unsubscribe(SubScript); }
185 
186  void EventPublisher::Unsubscribe(const String& EventName, EventSubscriberSlot* SubSlot)
187  { this->GetEventExcept(EventName)->Unsubscribe(SubSlot); }
188 
190  { return this->GetEventExcept(EventName)->UnsubscribeAll(); }
191 }//Mezzanine
192 
193 #endif
Basic class definition for functors used by a FunctorSubscriberSlot.
Event * GetEventExcept(const String &EventName) const
Gets an event in this publisher. version differs from the non-except version in that if it fails to f...
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
EventPublisher()
Class constructor.
Event * GetEvent(const String &EventName) const
Gets an event in this publisher.
This class represents a given event that can be subscribed to and/or fired.
Definition: event.h:52
EventContainer Events
A container storing all the Events published by this class by name.
Thrown when the requested identity could not be found.
Definition: exception.h:94
This is a base class for all classes that subscribe to events.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
A simple reference counting pointer.
Definition: countedptr.h:70
EventSubscriberSlot * Subscribe(const String &EventName, EventSubscriber *Sub)
Adds a subscriber to this event.
virtual ~EventPublisher()
Class destructor.
EventContainer::const_iterator ConstEventIterator
Const Iterator type for Event instances stored by this class.
The interface for a script.
Definition: script.h:70
Whole UnsubscribeAll()
Unsubscribes all subscribers from this Event.
Definition: event.cpp:205
void RemoveAllEvents()
Removes all events in this Publisher.
void( SubscriberFunction)(EventArgumentsPtr Args)
This is a convenience typedef for a c-style method that accepts EventArguments.
void SetMuteEvents(const Boole Mute)
Sets whether or not event firings by this publisher will be suppressed.
void Unsubscribe(EventSubscriber *Subscriber)
Unsubscribes a single subscriber all events in this publisher.
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
Event * AddEvent(const String &EventName)
Creates a new event this Publisher can fire.
This class represents a slot in an event that can be subscribed to via subscribers, functors, or methods.
void FireEvent(EventArgumentsPtr Args)
Fires an event.
Boole MuteEvents
Stores whether or not events will actually be fired when requested.
EventSubscriberSlot * Subscribe(EventSubscriber *Subscriber)
Adds a subscriber to this event.
Definition: event.cpp:66
Whole UnsubscribeAll()
Unsubscribes all subscribers from all events in this publisher.
void RemoveEvent(const String &EventName)
Removes an existing event in this Publisher.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
EventContainer::iterator EventIterator
Iterator type for Event instances stored by this class.
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Boole GetMuteEvents() const
Gets whether or not event firings by this publisher will be suppressed.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159