Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIEvent.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIEvent.cpp
00003         created:        15/10/2004
00004         author:         Gerald Lindsly
00005         
00006         purpose:        Implements Event class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIEvent.h"
00027 
00028 #if defined (_MSC_VER)
00029 #       pragma warning(disable : 4251)
00030 #       pragma warning(disable : 4786)
00031 #       if !defined (_MSC_EXTENSIONS)
00032 #               pragma warning (disable : 4224)
00033 #       endif
00034 #endif
00035 
00036 
00037 // Start of CEGUI namespace section
00038 namespace CEGUI {
00039 
00040 class ConnectionImpl : public Event::ConnectionInterface {
00041 public:
00042         ConnectionImpl(Event* event_, Event::Group group_, Event::Subscriber subscriber_) : 
00043                 event(event_), group(group_), subscriber(subscriber_)
00044                 {
00045                 }
00046 
00047                 virtual bool connected()
00048                 {
00049                         return event != 0;
00050                 }
00051 
00052                 virtual void disconnect()
00053                 {
00054                         if (event)
00055                         {
00056                                 event->unsubscribe(subscriber, group);
00057                         }
00058 
00059                 }
00060 
00061 protected:
00062         Event* event;
00063         Event::Group group;
00064         Event::Subscriber subscriber;
00065 
00066         friend class Event;
00067 };
00068 
00069 
00070 Event::Event(const String& name) : d_name(name)
00071 {
00072 }
00073 
00074 
00075 Event::~Event()
00076 {
00077         ConnectionOrdering::iterator i = connectionOrdering.begin();
00078         for (;i != connectionOrdering.end(); i++)
00079         {
00080                 if (((ConnectionImpl*)i->second.get())->event)
00081                 {
00082                         i->first.subscriber.release();
00083                 }
00084 
00085         }
00086 }
00087 
00088 
00089 Event::Connection Event::subscribe(Group group, Subscriber subscriber)
00090 {
00091         ConnectionImpl* conn = new ConnectionImpl(this, group, subscriber);
00092         connectionOrdering[GroupSubscriber(group, subscriber)] = conn;
00093         return conn;
00094 }
00095 
00096 
00097 void Event::operator()(EventArgs& args)
00098 {
00099         ConnectionOrdering::iterator i = connectionOrdering.begin();
00100 
00101         for (;i != connectionOrdering.end(); ++i)
00102         {
00103                 if (((ConnectionImpl*)i->second.get())->event)
00104                 {
00105                         args.handled |= i->first.subscriber(args);
00106                 }
00107 
00108         }
00109 
00110 }
00111 
00112 
00113 bool Event::unsubscribe(Subscriber subscriber, Group group)
00114 {
00115         ConnectionOrdering::iterator j = connectionOrdering.find(GroupSubscriber(group, subscriber));
00116 
00117         if (j == connectionOrdering.end())
00118         {
00119                 return false;
00120         }
00121 
00122         ConnectionImpl* c = (ConnectionImpl*)j->second.get();
00123         c->event = 0;
00124         c->subscriber.release();
00125         connectionOrdering.erase(j);
00126         return true;
00127 }
00128 
00129 } // End of  CEGUI namespace section
00130 

Generated on Wed Feb 16 12:41:06 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1