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 "EventC.hpp"
00041 #include "FactoryExceptions.hpp"
00042 #include "Logger.hpp"
00043 #include <vector>
00044
00045 namespace RTT
00046 {
00047
00048
00049 class EventC::D
00050 {
00051 public:
00052 const EventService* mgcf;
00053 std::string mname;
00054 std::vector<DataSourceBase::shared_ptr> args;
00055 ActionInterface::shared_ptr m;
00056
00057 void checkAndCreate() {
00058 Logger::In in("EventC");
00059 if ( mgcf == 0 || mgcf->hasEvent(mname) == false ) {
00060 Logger::log() <<Logger::Error << "No '"<<mname<<"' event found in the EventService."<<Logger::endl;
00061 ORO_THROW(name_not_found_exception(mname));
00062 }
00063 size_t sz = mgcf->arity(mname);
00064 if ( sz == args.size() ) {
00065
00066 m.reset( mgcf->getEvent(mname, args ) );
00067 args.clear();
00068 if (!m)
00069 return;
00070 }
00071 }
00072
00073 void newarg(DataSourceBase::shared_ptr na)
00074 {
00075 this->args.push_back( na );
00076 this->checkAndCreate();
00077 }
00078
00079 D( const EventService* gcf, const std::string& name)
00080 : mgcf(gcf), mname(name), m()
00081 {
00082 this->checkAndCreate();
00083 }
00084
00085 D(const D& other)
00086 : mgcf( other.mgcf), mname(other.mname),
00087 args( other.args ), m( other.m )
00088 {
00089 }
00090
00091 ~D()
00092 {
00093 }
00094
00095 };
00096
00097 EventC::EventC(const EventService* gcf, const std::string& name)
00098 : d( new D( gcf, name) ), m()
00099 {
00100 if (d->m) {
00101 this->m = d->m;
00102 delete d;
00103 d = 0;
00104 }
00105 }
00106
00107 EventC::EventC()
00108 : d( 0 ), m()
00109 {
00110 }
00111
00112 EventC::EventC(const EventC& other)
00113 : d( other.d ? new D(*other.d) : 0 ), m(other.m)
00114 {
00115 }
00116
00117 EventC& EventC::operator=(const EventC& other)
00118 {
00119 delete d;
00120 d = ( other.d ? new D(*other.d) : 0 );
00121 m = other.m;
00122 return *this;
00123 }
00124
00125 EventC::~EventC()
00126 {
00127 delete d;
00128 }
00129
00130 EventC& EventC::arg( DataSourceBase::shared_ptr a )
00131 {
00132 if (d)
00133 d->newarg( a );
00134 else {
00135 Logger::log() <<Logger::Warning << "Extra argument discarded for EventC."<<Logger::endl;
00136 }
00137
00138 if (d && d->m) {
00139 this->m = d->m;
00140 delete d;
00141 d = 0;
00142 }
00143
00144 return *this;
00145 }
00146
00147 bool EventC::ready() const {
00148 return m;
00149 }
00150
00151 void EventC::emit() {
00152 if (m)
00153 m->execute();
00154 else {
00155 Logger::log() <<Logger::Error << "emit() called on incomplete EventC."<<Logger::endl;
00156 if (d) {
00157 size_t sz = d->mgcf->arity(d->mname);
00158 if ( m ) {
00159 Logger::log() <<Logger::Error << "Wrong number of arguments provided for event '"+d->mname+"'"<<Logger::nl;
00160 Logger::log() <<Logger::Error << "Expected "<< sz << ", got: " << d->args.size() <<Logger::endl;
00161 }
00162 }
00163 }
00164 }
00165 }