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 "MethodC.hpp"
00040 #include "FactoryExceptions.hpp"
00041 #include "DataSourceCommand.hpp"
00042 #include "MethodRepository.hpp"
00043 #include "Logger.hpp"
00044 #include <Exceptions.hpp>
00045 #include <vector>
00046
00047 namespace RTT
00048 {
00049
00050
00051 class MethodC::D
00052 {
00053 public:
00054 const MethodRepository::Factory* mmr;
00055 std::string mname;
00056 std::vector<DataSourceBase::shared_ptr> args;
00057 AttributeBase* rta;
00058 DataSourceBase::shared_ptr m;
00059
00060 void checkAndCreate() {
00061 Logger::In in("MethodC");
00062 if ( mmr ) {
00063 if ( mmr->hasMember(mname) == false ) {
00064 Logger::log() <<Logger::Error << "No '"<<mname<<"' method in this Method Repository."<<Logger::endl;
00065 ORO_THROW(name_not_found_exception(mname));
00066 }
00067 size_t sz = mmr->getArity(mname);
00068 if ( sz == args.size() ) {
00069
00070 m = mmr->produce(mname, args );
00071 args.clear();
00072 if ( !m )
00073 return;
00074 if (rta)
00075 m = new DataSourceCommand( rta->getDataSource()->updateCommand( m.get() ) );
00076 }
00077 }
00078 }
00079
00080 void newarg(DataSourceBase::shared_ptr na)
00081 {
00082 this->args.push_back( na );
00083 this->checkAndCreate();
00084 }
00085
00086 void ret(AttributeBase* r)
00087 {
00088 if (rta)
00089 delete rta;
00090 this->rta = r->clone();
00091 }
00092
00093 D( const MethodRepository::Factory* mr, const std::string& name)
00094 : mmr(mr), mname(name), rta(0), m()
00095 {
00096 this->checkAndCreate();
00097 }
00098
00099 D(const D& other)
00100 : mmr(other.mmr), mname(other.mname),
00101 args( other.args ), rta( other.rta ? other.rta->clone() : 0 ), m( other.m )
00102 {
00103 }
00104
00105 ~D()
00106 {
00107 delete rta;
00108 }
00109
00110 };
00111
00112 MethodC::MethodC()
00113 : d(0), m()
00114 {
00115 }
00116
00117 MethodC::MethodC(const MethodRepository::Factory* mr, const std::string& name)
00118 : d( mr ? new D( mr, name) : 0 ), m()
00119 {
00120 if ( d->m ) {
00121 this->m = d->m;
00122 delete d;
00123 d = 0;
00124 }
00125 }
00126
00127 MethodC::MethodC(const MethodC& other)
00128 : d( other.d ? new D(*other.d) : 0 ), m( other.m ? other.m : 0)
00129 {
00130 }
00131
00132 MethodC& MethodC::operator=(const MethodC& other)
00133 {
00134 delete d;
00135 d = ( other.d ? new D(*other.d) : 0 );
00136 m = other.m;
00137 return *this;
00138 }
00139
00140 MethodC::~MethodC()
00141 {
00142 delete d;
00143 }
00144
00145 MethodC& MethodC::arg( DataSourceBase::shared_ptr a )
00146 {
00147 if (d)
00148 d->newarg( a );
00149 else {
00150 Logger::log() <<Logger::Warning << "Extra argument discarded for MethodC."<<Logger::endl;
00151 }
00152 if ( d && d->m ) {
00153 this->m = d->m;
00154 delete d;
00155 d = 0;
00156 }
00157 return *this;
00158 }
00159
00160 MethodC& MethodC::ret( AttributeBase* r )
00161 {
00162 if (d)
00163 d->ret( r );
00164 else {
00165 m = new DataSourceCommand(r->getDataSource()->updateCommand( m.get() ) );
00166 }
00167 if ( d && d->m ) {
00168 this->m = d->m;
00169 delete d;
00170 d = 0;
00171 }
00172 return *this;
00173 }
00174
00175
00176 bool MethodC::execute() {
00177 if (m)
00178 return m->evaluate();
00179 else {
00180 Logger::log() <<Logger::Error << "execute() called on incomplete MethodC."<<Logger::endl;
00181 if (d) {
00182 size_t sz;
00183 sz = d->mmr->getArity( d->mname );
00184 Logger::log() <<Logger::Error << "Wrong number of arguments provided for method '"+d->mname+"'"<<Logger::nl;
00185 Logger::log() <<Logger::Error << "Expected "<< sz << ", got: " << d->args.size() <<Logger::endl;
00186 }
00187 }
00188 return false;
00189 }
00190
00191 void MethodC::reset()
00192 {
00193 if (m)
00194 m->reset();
00195 }
00196
00197 bool MethodC::ready() const
00198 {
00199 return m;
00200 }
00201
00202 DataSourceBase::shared_ptr MethodC::getDataSource() { return m; }
00203 }