00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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 ) {}
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
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
00090 mevents.erase( mevents.find( ename ) );
00091
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();
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();
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();
00130 }
00131 detail::EventHookBase* ehi = mhooks.find(ename)->second->produce( args );
00132
00133
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 ,
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();
00145 }
00146 detail::EventHookBase* ehi = mhooks.find(ename)->second->produce( args );
00147
00148
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 }