FunctorFactory.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:05:59 CET 2006  FunctorFactory.hpp
00003 
00004                         FunctorFactory.hpp -  description
00005                            -------------------
00006     begin                : do november 02 2006
00007     copyright            : (C) 2006 FMTC
00008     email                : peter.soetens@fmtc.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_RTT_FUNCTOR_FACTORY_HPP
00040 #define ORO_RTT_FUNCTOR_FACTORY_HPP
00041 
00042 #include "DataSource.hpp"
00043 #include "FactoryExceptions.hpp"
00044 #include "Exceptions.hpp"
00045 
00046 namespace RTT
00047 {
00048     namespace detail
00049     {
00056         template<typename ResultT>
00057         class FunctorFactoryPart
00058         {
00059         public:
00060             virtual ~FunctorFactoryPart() {};
00061 
00066             virtual ResultT produce( const std::vector<DataSourceBase::shared_ptr>& args ) const = 0;
00067         };
00068 
00069         template<typename ResultT, typename FunctorT>
00070         class FunctorFactoryPart0
00071             : public FunctorFactoryPart<ResultT>
00072         {
00073             typedef FunctorT fun_t;
00074             fun_t fun;
00075         public:
00076             template<class FInit>
00077             FunctorFactoryPart0( FInit f )
00078                 : fun( f )
00079             {
00080             }
00081 
00082             ResultT produce(
00083                             const std::vector<DataSourceBase::shared_ptr>& args) const
00084             {
00085                 if ( ! args.empty() )
00086                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 0, args.size() ), ResultT());
00087                 return fun.create();
00088             }
00089         };
00090 
00091         template<typename ResultT, typename FunctorT, typename arg1_type = typename FunctorT::traits::arg1_type>
00092         class FunctorFactoryPart1
00093             : public FunctorFactoryPart<ResultT>
00094         {
00095             typedef FunctorT fun_t;
00096             fun_t fun;
00097         public:
00098             template<class FInit>
00099             FunctorFactoryPart1( FInit f)
00100                 : fun( f )
00101             {
00102             }
00103 
00104             ResultT produce(const std::vector<DataSourceBase::shared_ptr>& args) const
00105             {
00106                 if ( args.size() != 1 )
00107                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 1, args.size() ), ResultT());
00108                 typename DataSource<arg1_type>::shared_ptr a =
00109                     AdaptDataSource<arg1_type>()( DataSourceTypeInfo<arg1_type>::getTypeInfo()->convert(args[0]) );
00110                 if ( ! a )
00111                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 1, DataSource<arg1_type>::GetType(), args[0]->getType() ), ResultT());
00112                 return fun.create( a.get() );
00113             }
00114         };
00115 
00116         template<typename ResultT, typename FunctorT>
00117         class FunctorFactoryPart2
00118             : public FunctorFactoryPart<ResultT>
00119         {
00120             typedef FunctorT fun_t;
00121             typedef typename FunctorT::traits::arg1_type arg1_type;
00122             typedef typename FunctorT::traits::arg2_type arg2_type;
00123             fun_t fun;
00124         public:
00125             template<class FInit>
00126             FunctorFactoryPart2( FInit f)
00127                 : fun( f )
00128             {
00129             }
00130 
00131             ResultT produce(const std::vector<DataSourceBase::shared_ptr>& args) const
00132             {
00133                 if ( args.size() != 2 )
00134                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 2, args.size() ), ResultT());
00135 
00136                 typename DataSource<arg1_type>::shared_ptr a =
00137                     AdaptDataSource<arg1_type>()( DataSourceTypeInfo<arg1_type>::getTypeInfo()->convert(args[0]) );
00138                 if ( !a )
00139                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 1, DataSource<arg1_type>::GetType(), args[0]->getType() ), ResultT());
00140                 typename DataSource<arg2_type>::shared_ptr b =
00141                     AdaptDataSource<arg2_type>()( DataSourceTypeInfo<arg2_type>::getTypeInfo()->convert(args[1]) );
00142                 if ( !b )
00143                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 2, DataSource<arg2_type>::GetType(), args[1]->getType() ), ResultT());
00144 
00145                 return fun.create(a.get(), b.get() );
00146             }
00147         };
00148 
00149         template<typename ResultT, typename FunctorT>
00150         class FunctorFactoryPart3
00151             : public FunctorFactoryPart<ResultT>
00152         {
00153             typedef FunctorT fun_t;
00154             typedef typename FunctorT::traits::arg1_type arg1_type;
00155             typedef typename FunctorT::traits::arg2_type arg2_type;
00156             typedef typename FunctorT::traits::arg3_type arg3_type;
00157 
00158             fun_t fun;
00159         public:
00160             template<class FInit>
00161             FunctorFactoryPart3( FInit f)
00162                 : fun( f )
00163             {
00164             }
00165 
00166             ResultT produce(const std::vector<DataSourceBase::shared_ptr>& args) const
00167             {
00168                 if ( args.size() != 3 )
00169                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 3, args.size() ), ResultT());
00170 
00171                 typename DataSource<arg1_type>::shared_ptr a =
00172                     AdaptDataSource<arg1_type>()( DataSourceTypeInfo<arg1_type>::getTypeInfo()->convert(args[0]) );
00173                 if ( !a )
00174                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 1, DataSource<arg1_type>::GetType(), args[0]->getType() ), ResultT());
00175                 typename DataSource<arg2_type>::shared_ptr b =
00176                     AdaptDataSource<arg2_type>()( DataSourceTypeInfo<arg2_type>::getTypeInfo()->convert(args[1]) );
00177                 if ( !b )
00178                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 2, DataSource<arg2_type>::GetType(), args[1]->getType() ), ResultT());
00179                 typename DataSource<arg3_type>::shared_ptr c =
00180                     AdaptDataSource<arg3_type>()( DataSourceTypeInfo<arg3_type>::getTypeInfo()->convert(args[2]) );
00181                 if ( !c )
00182                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 3, DataSource<arg3_type>::GetType(), args[2]->getType() ), ResultT());
00183 
00184                 return fun.create(a.get(), b.get(), c.get() );
00185             }
00186         };
00187 
00188         template<typename ResultT, typename FunctorT>
00189         class FunctorFactoryPart4
00190             : public FunctorFactoryPart<ResultT>
00191         {
00192             typedef FunctorT fun_t;
00193             typedef typename FunctorT::traits::arg1_type arg1_type;
00194             typedef typename FunctorT::traits::arg2_type arg2_type;
00195             typedef typename FunctorT::traits::arg3_type arg3_type;
00196             typedef typename FunctorT::traits::arg4_type arg4_type;
00197 
00198             fun_t fun;
00199         public:
00200             template<class FInit>
00201             FunctorFactoryPart4( FInit f)
00202                 : fun( f )
00203             {
00204             }
00205 
00206             ResultT produce(const std::vector<DataSourceBase::shared_ptr>& args) const
00207             {
00208                 if ( args.size() != 4 )
00209                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 4, args.size() ), ResultT());
00210 
00211                 typename DataSource<arg1_type>::shared_ptr a =
00212                     AdaptDataSource<arg1_type>()( DataSourceTypeInfo<arg1_type>::getTypeInfo()->convert(args[0]) );
00213                 if ( !a )
00214                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 1, DataSource<arg1_type>::GetType(), args[0]->getType() ), ResultT());
00215                 typename DataSource<arg2_type>::shared_ptr b =
00216                     AdaptDataSource<arg2_type>()( DataSourceTypeInfo<arg2_type>::getTypeInfo()->convert(args[1]) );
00217                 if ( !b )
00218                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 2, DataSource<arg2_type>::GetType(), args[1]->getType() ), ResultT());
00219                 typename DataSource<arg3_type>::shared_ptr c =
00220                     AdaptDataSource<arg3_type>()( DataSourceTypeInfo<arg3_type>::getTypeInfo()->convert(args[2]) );
00221                 if ( !c )
00222                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 3, DataSource<arg3_type>::GetType(), args[2]->getType() ), ResultT());
00223                 typename DataSource<arg4_type>::shared_ptr d =
00224                     AdaptDataSource<arg4_type>()( DataSourceTypeInfo<arg4_type>::getTypeInfo()->convert(args[3]) );
00225                 if ( !d )
00226                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 4, DataSource<arg4_type>::GetType(), args[3]->getType() ), ResultT());
00227 
00228                 return fun.create(a.get(), b.get(), c.get(), d.get() );
00229             }
00230         };
00231 
00232         template<typename ResultT, typename FunctorT>
00233         class FunctorFactoryPart5
00234             : public FunctorFactoryPart<ResultT>
00235         {
00236             typedef FunctorT fun_t;
00237             typedef typename FunctorT::traits::arg1_type arg1_type;
00238             typedef typename FunctorT::traits::arg2_type arg2_type;
00239             typedef typename FunctorT::traits::arg3_type arg3_type;
00240             typedef typename FunctorT::traits::arg4_type arg4_type;
00241             typedef typename FunctorT::traits::arg5_type arg5_type;
00242 
00243             fun_t fun;
00244         public:
00245             template<class FInit>
00246             FunctorFactoryPart5( FInit f)
00247                 : fun( f )
00248             {
00249             }
00250 
00251             ResultT produce(const std::vector<DataSourceBase::shared_ptr>& args) const
00252             {
00253                 if ( args.size() != 5 )
00254                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 5, args.size() ), ResultT());
00255 
00256                 typename DataSource<arg1_type>::shared_ptr a =
00257                     AdaptDataSource<arg1_type>()( DataSourceTypeInfo<arg1_type>::getTypeInfo()->convert(args[0]) );
00258                 if ( !a )
00259                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 1, DataSource<arg1_type>::GetType(), args[0]->getType() ), ResultT());
00260                 typename DataSource<arg2_type>::shared_ptr b =
00261                     AdaptDataSource<arg2_type>()( DataSourceTypeInfo<arg2_type>::getTypeInfo()->convert(args[1]) );
00262                 if ( !b )
00263                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 2, DataSource<arg2_type>::GetType(), args[1]->getType() ), ResultT());
00264                 typename DataSource<arg3_type>::shared_ptr c =
00265                     AdaptDataSource<arg3_type>()( DataSourceTypeInfo<arg3_type>::getTypeInfo()->convert(args[2]) );
00266                 if ( !c )
00267                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 3, DataSource<arg3_type>::GetType(), args[2]->getType() ), ResultT());
00268                 typename DataSource<arg4_type>::shared_ptr d =
00269                     AdaptDataSource<arg4_type>()( DataSourceTypeInfo<arg4_type>::getTypeInfo()->convert(args[3]) );
00270                 if ( !d )
00271                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 4, DataSource<arg4_type>::GetType(), args[3]->getType() ), ResultT());
00272 
00273                 typename DataSource<arg5_type>::shared_ptr e =
00274                     AdaptDataSource<arg5_type>()( DataSourceTypeInfo<arg5_type>::getTypeInfo()->convert(args[4]) );
00275                 if ( !e )
00276                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 5, DataSource<arg5_type>::GetType(), args[4]->getType() ), ResultT());
00277 
00278                 return fun.create(a.get(), b.get(), c.get(), d.get(), e.get() );
00279             }
00280         };
00281 
00282         template<typename ResultT, typename FunctorT>
00283         class FunctorFactoryPart6
00284             : public FunctorFactoryPart<ResultT>
00285         {
00286             typedef FunctorT fun_t;
00287             typedef typename FunctorT::traits::arg1_type arg1_type;
00288             typedef typename FunctorT::traits::arg2_type arg2_type;
00289             typedef typename FunctorT::traits::arg3_type arg3_type;
00290             typedef typename FunctorT::traits::arg4_type arg4_type;
00291             typedef typename FunctorT::traits::arg5_type arg5_type;
00292             typedef typename FunctorT::traits::arg6_type arg6_type;
00293 
00294             fun_t fun;
00295         public:
00296             template<class FInit>
00297             FunctorFactoryPart6( FInit f)
00298                 : fun( f )
00299             {
00300             }
00301 
00302             ResultT produce(const std::vector<DataSourceBase::shared_ptr>& args) const
00303             {
00304                 if ( args.size() != 6 )
00305                     ORO_THROW_OR_RETURN(wrong_number_of_args_exception( 6, args.size() ), ResultT());
00306 
00307                 typename DataSource<arg1_type>::shared_ptr a =
00308                     AdaptDataSource<arg1_type>()( DataSourceTypeInfo<arg1_type>::getTypeInfo()->convert(args[0]) );
00309                 if ( !a )
00310                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 1, DataSource<arg1_type>::GetType(), args[0]->getType() ), ResultT());
00311                 typename DataSource<arg2_type>::shared_ptr b =
00312                     AdaptDataSource<arg2_type>()( DataSourceTypeInfo<arg2_type>::getTypeInfo()->convert(args[1]) );
00313                 if ( !b )
00314                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 2, DataSource<arg2_type>::GetType(), args[1]->getType() ), ResultT());
00315                 typename DataSource<arg3_type>::shared_ptr c =
00316                     AdaptDataSource<arg3_type>()( DataSourceTypeInfo<arg3_type>::getTypeInfo()->convert(args[2]) );
00317                 if ( !c )
00318                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 3, DataSource<arg3_type>::GetType(), args[2]->getType() ), ResultT());
00319                 typename DataSource<arg4_type>::shared_ptr d =
00320                     AdaptDataSource<arg4_type>()( DataSourceTypeInfo<arg4_type>::getTypeInfo()->convert(args[3]) );
00321                 if ( !d )
00322                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 4, DataSource<arg4_type>::GetType(), args[3]->getType() ), ResultT());
00323 
00324                 typename DataSource<arg5_type>::shared_ptr e =
00325                     AdaptDataSource<arg5_type>()( DataSourceTypeInfo<arg5_type>::getTypeInfo()->convert(args[4]) );
00326                 if ( !e )
00327                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 5, DataSource<arg5_type>::GetType(), args[4]->getType() ), ResultT());
00328 
00329                 typename DataSource<arg6_type>::shared_ptr f =
00330                     AdaptDataSource<arg6_type>()( DataSourceTypeInfo<arg6_type>::getTypeInfo()->convert(args[5]) );
00331                 if ( !f )
00332                     ORO_THROW_OR_RETURN(wrong_types_of_args_exception( 6, DataSource<arg6_type>::GetType(), args[5]->getType() ), ResultT());
00333 
00334                 return fun.create(a.get(), b.get(), c.get(), d.get(), e.get(), f.get() );
00335             }
00336         };
00337 
00338     }
00339 }
00340 
00341 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3