EventService.cpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Wed Jan 18 14:11:40 CET 2006  EventService.cxx 
00003 
00004                         EventService.cxx -  description
00005                            -------------------
00006     begin                : Wed January 18 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.be
00009  
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037  
00038  
00039 #include "EventService.hpp"
00040 #include "mystd.hpp"
00041 
00042 #include "ExecutionEngine.hpp"
00043 #include <CompletionProcessor.hpp>
00044 
00045 
00046 
00047 namespace RTT
00048 {
00049     using namespace boost;
00050 
00051     EventService::EventService( ExecutionEngine* ee ) : eeproc(ee), eproc( 0 ) {}
00052     EventService::EventService( EventProcessor* ep ) : eeproc(0), eproc( ep ) {} // ep may be null.
00053 
00054     EventProcessor* EventService::getEventProcessor() {
00055         return eeproc ? eeproc->events()
00056             : (eproc ? eproc : CompletionProcessor::Instance());
00057     }
00058 
00059     void EventService::setEventProcessor(EventProcessor* ep) {
00060         eproc = ep;
00061     }
00062 
00063     bool EventService::hasEvent(const std::string& ename) const
00064     {
00065         return mevents.count(ename) == 1;
00066     }
00067 
00068     std::vector<std::string> EventService::getEvents() const
00069     {
00070         return keys( mevents );
00071     }
00072 
00073     int EventService::arity(const std::string& name) const
00074     {
00075         return this->getArity(name);
00076     }
00077             
00078     bool EventService::removeEvent( const std::string& ename ) {
00079         if ( mevents.count(ename) == 0 )
00080             return false;
00081         // first erase the hook.
00082         for (Hooks::iterator it = mhooks.begin(); it !=mhooks.end(); ++it ) {
00083             if ( it->first == ename ) {
00084                 delete it->second;
00085                 mhooks.erase( it );
00086                 break;
00087             }
00088         }
00089         // next erase the emittor.
00090         mevents.erase( mevents.find( ename ) );
00091         // next erase the factory, if any:
00092         this->remove( ename );
00093         return true;
00094     }
00095         
00096     void EventService::clear() {
00097         for (Hooks::iterator it = mhooks.begin(); it !=mhooks.end(); ++it )
00098             delete it->second;
00099         OperationFactory< ActionInterface*>::clear();
00100         mevents.clear();
00101     }
00102 
00103 
00104     EventService::~EventService() {
00105         clear();
00106     }
00107 
00108     EventC EventService::setupEmit(const std::string& ename) const {
00109         if ( !this->hasMember(ename) ) {
00110             log(Error) << "Can not initialize EventC for '"<<ename<<"': no such Event."<<endlog();
00111             return EventC(); // empty handle.
00112         }
00113         return EventC(this, ename);
00114     }
00115 
00116     ConnectionC EventService::setupConnection(const std::string& ename) const {
00117         if ( mhooks.count(ename) != 1 ) {
00118             log(Error) << "Can not initialize ConnectionC for '"<<ename<<"': no such Event."<<endlog();
00119             return ConnectionC(); // empty handle.
00120         }
00121         return ConnectionC(this, ename );
00122     }
00123 
00124     Handle EventService::setupSyn(const std::string& ename,
00125                                                boost::function<void(void)> func,          
00126                                                std::vector<DataSourceBase::shared_ptr> args ) const {
00127         if ( mhooks.count(ename) != 1 ) {
00128             log(Error) << "Can not create connection to '"<<ename<<"': no such Event."<<endlog();
00129             return Handle(); // empty handle.
00130         }
00131         detail::EventHookBase* ehi = mhooks.find(ename)->second->produce( args );
00132 
00133         // ehi is stored _inside_ the connection object !
00134         return ehi->setupSyn( func ); 
00135     }
00136         
00137     Handle EventService::setupAsyn(const std::string& ename,
00138                                                 boost::function<void(void)> afunc,          
00139                                                 const std::vector<DataSourceBase::shared_ptr>& args,
00140                                                 EventProcessor* ep /* = CompletionProcessor::Instance()*/,
00141                                                 EventProcessor::AsynStorageType s_type) const {
00142         if ( mhooks.count(ename) != 1 ) {
00143             log(Error) << "Can not create connection to '"<<ename<<"': no such Event."<<endlog();
00144             return Handle(); // empty handle.
00145         }
00146         detail::EventHookBase* ehi = mhooks.find(ename)->second->produce( args );
00147 
00148         // ehi is stored _inside_ the connection object !
00149         return ehi->setupAsyn( afunc, ep, s_type ); 
00150     }
00151 
00152     ActionInterface* EventService::getEvent(const std::string& ename,const std::vector<DataSourceBase::shared_ptr>& args) const
00153     {
00154         return this->produce(ename, args);
00155     }
00156 
00157 
00158 }

Generated on Tue Mar 25 17:41:46 2008 for OrocosReal-TimeToolkit by  doxygen 1.5.3