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               NameServerRegistrator<EventType*>(nameserver, name, this),
00119               mname(name)
00120         {
00121             Logger::log() << Logger::Debug << "Event Created with name  : "<< name << Logger::endl;
00122         }
00123 
00127         Event()
00128         {
00129         }
00130 
00136         Event(const Event& m)
00137             : Base(m),
00138               mname(m.mname)
00139         {}
00140 
00148         Event& operator=(const Event& m)
00149         {
00150             if ( this == &m )
00151                 return *this;
00152             mname = m.mname;
00153             this->impl = m.impl;
00154             return *this;
00155         }
00156 
00163         Event(boost::shared_ptr<ActionInterface> implementation)
00164             : Base( boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation) ),
00165               mname()
00166         {
00167             if ( !this->impl && implementation ) {
00168                 log(Error) << "Tried to construct Event from incompatible type."<< endlog();
00169             }
00170         }
00171 
00180         Event& operator=(boost::shared_ptr<ActionInterface> implementation)
00181         {
00182             if (this->impl == implementation)
00183                 return *this;
00184             this->impl = boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation);
00185             if ( !this->impl && implementation ) {
00186                 log(Error) << "Tried to assign Event '"<< mname <<"' from incompatible type."<< endlog();
00187             }
00188             return *this;
00189         }
00190 
00196         bool ready() const {
00197             return this->impl;
00198         }
00199 
00203         const std::string& getName() const { return mname; }
00204 
00208         int arity() const { return this->impl ? this->impl->arity() : -1; }
00209 
00213         Handle connect(const SlotFunction& f)
00214         {
00215             return this->impl ? this->impl->connect( f ) : Handle();
00216         }
00217 
00221         Handle connect( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00222         {
00223             return this->impl ? ep->connect( l, *this, t ) : Handle();
00224         }
00225 
00229         Handle setup(const SlotFunction& f)
00230         {
00231             return this->impl ? this->impl->setup( f ) : Handle();
00232         }
00233 
00237         Handle setup( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00238         {
00239             return this->impl ? ep->setup( l, *this, t ) : Handle();
00240         }
00241 
00242         EventBasePtr getImplementation() const { return this->impl; }
00243 
00249         static NameServer<EventType*> nameserver;
00250     };
00251 
00252 
00253     template<
00254         typename SignatureT
00255     >
00256     NameServer<Event<SignatureT>*>
00257     Event<SignatureT>::nameserver;
00258 
00259 }
00260 
00261 #endif

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