S3FC project page S3FC home page

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

s3_event.tcc

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 #include <s3fc/s3_event.h>
00034 #include <s3fc/s3_exception.h>
00035 #include <sstream>
00036 
00037 //
00038 template<typename T_Event>
00039 void s3_event_dispatcher<T_Event>::subscribe_handler(event_handler_t& handler)
00040 {
00041    s3_event_dispatcher_lock.lock();
00042    // test precondition
00043    if ( is_subscribed(handler, 0) )
00044    {
00045       s3_event_dispatcher_lock.unlock();
00046       throw s3_generic_exception("s3_event_dispatcher::subscribe_handler()",
00047              "Precondition violation: "
00048              "is_subscribed(handler) != false ");
00049    }
00050    hlist.push_back(&handler);
00051    s3_event_dispatcher_lock.unlock();
00052 }
00053 
00054 //
00055 template<typename T_Event>
00056 void s3_event_dispatcher<T_Event>::unsubscribe_handler(event_handler_t& handler)
00057 {
00058    s3_event_dispatcher_lock.lock();
00059    // test precondition
00060    if ( ! is_subscribed(handler, 0) )
00061    {
00062       s3_event_dispatcher_lock.unlock();
00063       throw s3_generic_exception("s3_event_dispatcher::unsubscribe_handler()",
00064              "Precondition violation: "
00065              "is_subscribed(handler) != true ");
00066    }
00067    typename hlist_t::iterator iter =
00068       std::find(hlist.begin(), hlist.end(), &handler);
00069    hlist.erase(iter);
00070    s3_event_dispatcher_lock.unlock();
00071 }
00072 
00073 //
00074 template<typename T_Event>
00075 bool s3_event_dispatcher<T_Event>::is_subscribed(const 
00076                    event_handler_t& handler) const
00077 {
00078    bool rv;
00079    s3_event_dispatcher_lock.lock();
00080    rv = is_subscribed(handler, 0);
00081    s3_event_dispatcher_lock.unlock();
00082    return rv;
00083 }
00084 
00085 //
00086 template<typename T_Event>
00087 void s3_event_dispatcher<T_Event>::dispatch_event(const event_t& event)
00088 {
00089    s3_event_dispatcher_lock.lock();
00090    for( typename hlist_t::iterator iter = hlist.begin();
00091    iter != hlist.end();
00092    iter++ )
00093    {
00094       event_handler_t* h = (*iter);
00095       // invoke functor
00096       (*h)(event);
00097    }
00098    s3_event_dispatcher_lock.unlock();
00099 }
00100 
00101 //
00102 template<typename T_Event>
00103 bool s3_event_dispatcher<T_Event>::is_subscribed(const event_handler_t& handler,
00104                    int critical) const
00105 {
00106    return ( std::find(hlist.begin(), hlist.end(), &handler) != hlist.end() );
00107 }

Send comments to: s3fc@stonethree.com SourceForge Logo