S3FC project page S3FC home page

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

s3_event.h

Go to the documentation of this file.
00001 /*
00002  * Stone Three Foundation Class (s3fc) provides a number of utility classes.
00003  * Copyright (C) 2001 by Stone Three Signal Processing (Pty) Ltd.
00004  *
00005  * Authored by Stone Three Signal Processing (Pty) Ltd.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  * 
00021  * Please see the file 'COPYING' in the source root directory.
00022  */
00023 
00033 #ifndef S3_EVENT_H
00034 #define S3_EVENT_H
00035 
00036 #include <s3fc/s3_mutex.h>
00037 #include <list>
00038 #include <algorithm>
00039 
00081 template<typename T_Event>
00082 class s3_event_handler
00083 {
00084   public:
00088    typedef T_Event event_t;
00089   public:
00095    virtual void operator() (const event_t& event) = 0;
00096 };
00097 
00112 template<typename T_Event>
00113 class s3_event_dispatcher
00114 {
00115   public:
00119    typedef T_Event event_t;
00123    typedef s3_event_handler<event_t> event_handler_t;
00124   private:
00125    // type of handler list
00126    typedef std::list<event_handler_t*> hlist_t;
00127    // list of subscribed event handlers
00128    hlist_t hlist;
00129    // protect critical sections
00130    s3_mutex s3_event_dispatcher_lock;
00131    
00132   public:
00142     void subscribe_handler(event_handler_t& handler );
00150    void unsubscribe_handler(event_handler_t& handler);
00157    bool is_subscribed(const event_handler_t& handler) const;
00165    void dispatch_event(const event_t& event);
00166   private:
00167    // critical section of is_subscribed - this should ONLY be called with
00168    // the lock ALREADY in place. Use this to call is_subscribed from within
00169    // critical sections. Also use this to implement is_subscribed
00170    bool is_subscribed(const event_handler_t& handler, 
00171             int critical) const;
00172 };
00173 
00174 #include <s3fc/s3_event.tcc>
00175 
00176 #endif

Send comments to: s3fc@stonethree.com SourceForge Logo