MethodC.cpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Wed Jan 18 14:11:40 CET 2006  MethodC.cxx 
00003 
00004                         MethodC.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 "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                     // may throw or return nill
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 }

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