EventC.cpp

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

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