Event.hpp

00001 /***************************************************************************
00002  tag: Peter Soetens  Wed Apr 17 13:49:58 CEST 2002  Event.h
00003 
00004                        Event.hpp -  description
00005                           -------------------
00006    begin                : Wed April 17 2002
00007    copyright            : (C) 2002 Peter Soetens
00008    email                : peter.soetens@mech.kuleuven.ac.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 #ifndef ORO_CORELIB_EVENT_HPP
00040 #define ORO_CORELIB_EVENT_HPP
00041 
00042 #include "Signal.hpp"
00043 #include "Handle.hpp"
00044 #include "LocalEvent.hpp"
00045 #include <boost/call_traits.hpp>
00046 #include <boost/function.hpp>
00047 #include "NameServerRegistrator.hpp"
00048 #include "Invoker.hpp"
00049 
00050 #include "Logger.hpp"
00051 #include "EventProcessor.hpp"
00052 #include <cassert>
00053 
00054 namespace RTT
00055 {
00056 
00081     template<
00082         typename SignatureT // function type R (T1, T2, ..., TN)
00083     >
00084     class Event
00085         : public detail::InvokerSignature<boost::function_traits<SignatureT>::arity,
00086                                           SignatureT,
00087                                           boost::shared_ptr< detail::EventBase<SignatureT> > >,
00088           private NameServerRegistrator<Event<SignatureT>*>
00089     {
00090         typedef SignatureT FunctionT;
00091         std::string mname;
00092         typedef boost::shared_ptr< detail::EventBase<FunctionT> > EventBasePtr;
00093         typedef detail::InvokerSignature<boost::function_traits<FunctionT>::arity,
00094                                          FunctionT,
00095                                          EventBasePtr > Base;
00096     public:
00100         typedef EventProcessor::AsynStorageType AsynStorageType;
00101 
00102         typedef boost::function<SignatureT> SlotFunction;
00103 
00104         typedef Event<SignatureT> EventType;
00105 
00106         typedef SignatureT Signature;
00107 
00111         typedef typename boost::function_traits<SignatureT>::result_type result_type;
00112 
00116         explicit Event(const std::string name )
00117             : Base( EventBasePtr(new detail::LocalEvent<Signature>()) ),
00118               mname(name)
00119         {
00120             Logger::log() << Logger::Debug << "Event Created with name  : "<< name << Logger::endl;
00121         }
00122 
00126         Event()
00127         {
00128         }
00129 
00135         Event(const Event& m)
00136             : Base(m),
00137               mname(m.mname)
00138         {}
00139 
00147         Event& operator=(const Event& m)
00148         {
00149             if ( this == &m )
00150                 return *this;
00151             mname = m.mname;
00152             this->impl = m.impl;
00153             return *this;
00154         }
00155 
00162         Event(boost::shared_ptr<ActionInterface> implementation)
00163             : Base( boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation) ),
00164               mname()
00165         {
00166             if ( !this->impl && implementation ) {
00167                 log(Error) << "Tried to construct Event from incompatible type."<< endlog();
00168             }
00169         }
00170 
00179         Event& operator=(boost::shared_ptr<ActionInterface> implementation)
00180         {
00181             if (this->impl == implementation)
00182                 return *this;
00183             this->impl = boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation);
00184             if ( !this->impl && implementation ) {
00185                 log(Error) << "Tried to assign Event '"<< mname <<"' from incompatible type."<< endlog();
00186             }
00187             return *this;
00188         }
00189 
00195         bool ready() const {
00196             return this->impl;
00197         }
00198 
00202         const std::string& getName() const { return mname; }
00203 
00207         int arity() const { return this->impl ? this->impl->arity() : -1; }
00208 
00212         Handle connect(const SlotFunction& f)
00213         {
00214             return this->impl ? this->impl->connect( f ) : Handle();
00215         }
00216 
00220         Handle connect( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00221         {
00222             return this->impl ? ep->connect( l, *this, t ) : Handle();
00223         }
00224 
00228         Handle setup(const SlotFunction& f)
00229         {
00230             return this->impl ? this->impl->setup( f ) : Handle();
00231         }
00232 
00236         Handle setup( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00237         {
00238             return this->impl ? ep->setup( l, *this, t ) : Handle();
00239         }
00240 
00241         EventBasePtr getImplementation() const { return this->impl; }
00242 
00248         static NameServer<EventType*> nameserver;
00249     };
00250 
00251 
00252     template<
00253         typename SignatureT
00254     >
00255     NameServer<Event<SignatureT>*>
00256     Event<SignatureT>::nameserver;
00257 
00258 }
00259 
00260 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3