signal_template.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Wed Jan 18 14:11:38 CET 2006  signal_template.hpp
00003 
00004                         signal_template.hpp -  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 
00040 #ifndef OROCOS_SIGNAL_TEMPLATE_HEADER_INCLUDED
00041 #define OROCOS_SIGNAL_TEMPLATE_HEADER_INCLUDED
00042 #include "SignalBase.hpp"
00043 #include "NA.hpp"
00044 
00045 #ifdef ORO_SIGNAL_USE_LIST_LOCK_FREE
00046 #ifndef USE_CPP11
00047 #include <boost/lambda/bind.hpp>
00048 #include <boost/bind.hpp>
00049 #include <boost/lambda/casts.hpp>
00050 #endif
00051 #else
00052 #include "../os/MutexLock.hpp"
00053 #endif
00054 #endif // !OROCOS_SIGNAL_TEMPLATE_HEADER_INCLUDED
00055 
00056 // Define class names used
00057 #define OROCOS_SIGNAL_N BOOST_JOIN(signal,OROCOS_SIGNATURE_NUM_ARGS)
00058 #define OROCOS_SIGNAL_CONNECTION_N BOOST_JOIN(connection,OROCOS_SIGNATURE_NUM_ARGS)
00059 
00060 
00061 namespace RTT {
00062 
00063     namespace internal {
00064 
00065         template<class SlotFunction>
00066         class OROCOS_SIGNAL_CONNECTION_N : public ConnectionBase
00067         {
00068         public:
00069             typedef SlotFunction slot_function;
00070             typedef SlotFunction function_type;
00071             OROCOS_SIGNATURE_TYPEDEFS
00072 
00073             OROCOS_SIGNAL_CONNECTION_N(SignalBase* s, const slot_function& f)
00074                 : ConnectionBase(s), func(f)
00075             {
00076             }
00077 
00078             void emit(OROCOS_SIGNATURE_PARMS)
00079             {
00080                 if (this->mconnected)
00081                     func(OROCOS_SIGNATURE_ARGS);
00082             }
00083         private:
00084             slot_function func;
00085         };
00086 
00087         template<typename R, OROCOS_SIGNATURE_TEMPLATE_PARMS OROCOS_SIGNATURE_COMMA_IF_NONZERO_ARGS
00088              class SlotFunctionT = OROCOS_SIGNATURE_FUNCTION_N< R OROCOS_SIGNATURE_COMMA_IF_NONZERO_ARGS OROCOS_SIGNATURE_TEMPLATE_ARGS> >
00089         class OROCOS_SIGNAL_N
00090         : public SignalBase
00091         {
00092                 OROCOS_SIGNAL_N(const OROCOS_SIGNAL_N< R, OROCOS_SIGNATURE_TEMPLATE_ARGS OROCOS_SIGNATURE_COMMA_IF_NONZERO_ARGS SlotFunctionT>& s);
00093 
00094         public:
00095         typedef SlotFunctionT slot_function_type;
00096         typedef OROCOS_SIGNAL_CONNECTION_N<SlotFunctionT> connection_impl;
00097 
00098         typedef R result_type;
00099         OROCOS_SIGNATURE_ARG_TYPES
00100 
00101 #if OROCOS_SIGNATURE_NUM_ARGS == 1
00102         typedef arg1_type first_argument_type;
00103 #endif
00104 #if OROCOS_SIGNATURE_NUM_ARGS == 2
00105         typedef arg1_type first_argument_type;
00106         typedef arg2_type second_argument_type;
00107 #endif
00108     private:
00109 #ifdef ORO_SIGNAL_USE_LIST_LOCK_FREE
00110         // required for GCC 4.0.2
00111         static connection_impl* applyEmit( connection_t c ) {
00112             return static_cast<connection_impl*> (c.get() );
00113         }
00114 #endif
00115     public:
00116                 OROCOS_SIGNAL_N()
00117                 {
00118                 }
00119 
00120         Handle connect(const slot_function_type& f )
00121         {
00122             Handle h = this->setup(f);
00123             h.connect();
00124             return h;
00125         }
00126 
00127         Handle setup(const slot_function_type& f )
00128                 {
00129                         connection_t conn(
00130                                 new connection_impl(this, f) );
00131             this->conn_setup( conn );
00132             return Handle(conn);
00133                 }
00134 
00135                 R emit(OROCOS_SIGNATURE_PARMS)
00136                 {
00137 #ifdef ORO_SIGNAL_USE_LIST_LOCK_FREE
00138             this->emitting = true;
00139 
00140             // this code did initially not work under gcc 4.0/ubuntu breezy.
00141             // connection_t::get() const becomes an undefined symbol.
00142             // works under gcc 3.4
00143 #ifdef USE_CPP11
00144             mconnections.apply( bind(&connection_impl::emit,
00145                                                     bind( &applyEmit, _1) // works for any compiler
00146 #else
00147             mconnections.apply( boost::lambda::bind(&connection_impl::emit,
00148                                                     boost::lambda::bind( &applyEmit, boost::lambda::_1) // works for any compiler
00149                                                     //not in gcc 4.0.2: boost::lambda::ll_static_cast<connection_impl*>(boost::lambda::bind(&connection_t::get, boost::lambda::_1))
00150 #endif
00151 #if OROCOS_SIGNATURE_NUM_ARGS != 0
00152                                                     ,OROCOS_SIGNATURE_ARGS
00153 #endif
00154                                                     ) );
00155             this->emitting = false;
00156 #else
00157             os::MutexLock lock(m);
00158             if (this->emitting)
00159                 return NA<R>::na(); // avoid uglyness : Handlers calling emit.
00160             this->emitting = true;
00161             iterator it = mconnections.begin();
00162             const_iterator end = mconnections.end();
00163             for (; it != end; ++it ) {
00164                 connection_impl* ci = static_cast<connection_impl*>( it->get() );
00165                 if (ci)
00166                     ci->emit(OROCOS_SIGNATURE_ARGS); // this if... race is guarded by the mutex.
00167             }
00168             this->emitting = false;
00169             this->cleanup();
00170 #endif
00171             return NA<R>::na();
00172                 }
00173 
00174                 R operator()(OROCOS_SIGNATURE_PARMS)
00175                 {
00176             return this->emit(OROCOS_SIGNATURE_ARGS);
00177                 }
00178 
00179                 R fire(OROCOS_SIGNATURE_PARMS)
00180                 {
00181             return this->emit(OROCOS_SIGNATURE_ARGS);
00182                 }
00183 
00184         virtual int arity() const { return OROCOS_SIGNATURE_NUM_ARGS; }
00185         };
00186 
00187 }} // namespace sigslot
00188 
00189 
00190 #undef OROCOS_SIGNAL_N
00191 #undef OROCOS_SIGNAL_CONNECTION_N
00192 


rtt
Author(s): RTT Developers
autogenerated on Fri Sep 9 2016 04:02:15