DataSourceStorage.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:13 CET 2006  DataSourceStorage.hpp
00003 
00004                         DataSourceStorage.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_TASK_DATASOURCE_STORAGE_HPP
00040 #define ORO_TASK_DATASOURCE_STORAGE_HPP
00041 
00042 #include <boost/function.hpp>
00043 #include <boost/bind.hpp>
00044 #include <boost/mem_fn.hpp>
00045 #include "boost/function_types/function_type.hpp"
00046 #include "boost/function_types/function_type_arity.hpp"
00047 #include "CommandC.hpp"
00048 
00049 namespace RTT
00050 {
00051     namespace detail
00052     {
00053         // Partial specialisations for storing a void, not a void or reference
00054         template<class R>
00055         struct DataSourceResultStorage
00056         {
00057             typename ValueDataSource<R>::shared_ptr result;
00058 
00059             DataSourceResultStorage()
00060                 : result(new ValueDataSource<R>() )
00061             {
00062             }
00063 
00064             template<class ContainerT>
00065             void initRet(ContainerT& cc) {
00066                 cc.ret(DataSourceBase::shared_ptr(result));
00067             }
00068 
00069             R getResult() {
00070                 return result->get();
00071             }
00072         };
00073 
00074         template<>
00075         struct DataSourceResultStorage<void>
00076         {
00077             DataSourceResultStorage()
00078             {
00079             }
00080 
00081             template<class ContainerT>
00082             void initRet(ContainerT& ) {}
00083 
00084             void getResult() {}
00085         };
00086 
00089         template<class R>
00090         struct DataSourceResultStorage<R&>
00091         {
00092             typename ValueDataSource<R>::shared_ptr result;
00093 
00094             DataSourceResultStorage()
00095                 : result(new ValueDataSource<R>() )
00096             {
00097             }
00098 
00099             template<class ContainerT>
00100             void initRet(ContainerT& cc) {
00101                 cc.ret(DataSourceBase::shared_ptr(result));
00102             }
00103 
00104             R getResult() {
00105                 return result->get();
00106             }
00107         };
00108 
00109         // Partial specialisations for storing a reference or not reference
00110         template<class A>
00111         struct DataSourceArgStorage
00112         {
00113             typename ValueDataSource<A>::shared_ptr value;
00114             DataSourceArgStorage()
00115                 : value(new ValueDataSource<A>() )
00116             {}
00117         };
00118 
00121         template<class A>
00122         struct DataSourceArgStorage<A&>
00123         {
00124             // strips the reference !
00125             typename ValueDataSource<A>::shared_ptr value;
00126             DataSourceArgStorage()
00127                 : value(new ValueDataSource<A>() )
00128             {}
00129         };
00130 
00131         template<int, class T>
00132         struct DataSourceStorageImpl;
00133 
00137         template<class DataType>
00138         struct DataSourceStorageImpl<0, DataType>
00139             : public DataSourceResultStorage<typename boost::function_traits<DataType>::result_type>
00140         {
00141             template<class ContainerT>
00142             void initArgs(ContainerT& ) {}
00143         };
00144 
00148         template<class DataType>
00149         struct DataSourceStorageImpl<1, DataType>
00150             : public DataSourceResultStorage<typename boost::function_traits<DataType>::result_type>
00151         {
00152             typedef typename boost::function_traits<DataType>::arg1_type   arg1_type;
00153             DataSourceArgStorage<arg1_type> ma1;
00154 
00155             template<class ContainerT>
00156             void initArgs(ContainerT& cc) {
00157                 cc.arg( DataSourceBase::shared_ptr(ma1.value.get()) );
00158             }
00159 
00160             void store(arg1_type a1) {
00161                 ma1.value->set(a1);
00162             }
00163         };
00164 
00165         template<class DataType>
00166         struct DataSourceStorageImpl<2, DataType>
00167             : public DataSourceResultStorage<typename boost::function_traits<DataType>::result_type>
00168         {
00169             typedef typename boost::function_traits<DataType>::arg1_type   arg1_type;
00170             typedef typename boost::function_traits<DataType>::arg2_type   arg2_type;
00171             DataSourceArgStorage<arg1_type> ma1;
00172             DataSourceArgStorage<arg2_type> ma2;
00173 
00174             template<class ContainerT>
00175             void initArgs(ContainerT& cc) {
00176                 cc.arg( DataSourceBase::shared_ptr(ma1.value) );
00177                 cc.arg( DataSourceBase::shared_ptr(ma2.value) );
00178             }
00179             void store(arg1_type a1, arg2_type a2) {
00180                 ma1.value->set(a1);
00181                 ma2.value->set(a2);
00182             }
00183         };
00184 
00185         template<class DataType>
00186         struct DataSourceStorageImpl<3, DataType>
00187             : public DataSourceResultStorage<typename boost::function_traits<DataType>::result_type>
00188         {
00189             typedef typename boost::function_traits<DataType>::arg1_type   arg1_type;
00190             typedef typename boost::function_traits<DataType>::arg2_type   arg2_type;
00191             typedef typename boost::function_traits<DataType>::arg3_type   arg3_type;
00192             DataSourceArgStorage<arg1_type> ma1;
00193             DataSourceArgStorage<arg2_type> ma2;
00194             DataSourceArgStorage<arg3_type> ma3;
00195 
00196             template<class ContainerT>
00197             void initArgs(ContainerT& cc) {
00198                 cc.arg( DataSourceBase::shared_ptr(ma1.value) );
00199                 cc.arg( DataSourceBase::shared_ptr(ma2.value) );
00200                 cc.arg( DataSourceBase::shared_ptr(ma3.value) );
00201             }
00202             void store(arg1_type a1, arg2_type a2, arg3_type a3) {
00203                 ma1.value->set(a1);
00204                 ma2.value->set(a2);
00205                 ma3.value->set(a3);
00206             }
00207         };
00208 
00209         template<class DataType>
00210         struct DataSourceStorageImpl<4, DataType>
00211             : public DataSourceResultStorage<typename boost::function_traits<DataType>::result_type>
00212         {
00213             typedef typename boost::function_traits<DataType>::arg1_type   arg1_type;
00214             typedef typename boost::function_traits<DataType>::arg2_type   arg2_type;
00215             typedef typename boost::function_traits<DataType>::arg3_type   arg3_type;
00216             typedef typename boost::function_traits<DataType>::arg4_type   arg4_type;
00217             DataSourceArgStorage<arg1_type> ma1;
00218             DataSourceArgStorage<arg2_type> ma2;
00219             DataSourceArgStorage<arg3_type> ma3;
00220             DataSourceArgStorage<arg4_type> ma4;
00221 
00222             template<class ContainerT>
00223             void initArgs(ContainerT& cc) {
00224                 cc.arg( DataSourceBase::shared_ptr(ma1.value) );
00225                 cc.arg( DataSourceBase::shared_ptr(ma2.value) );
00226                 cc.arg( DataSourceBase::shared_ptr(ma3.value) );
00227                 cc.arg( DataSourceBase::shared_ptr(ma4.value) );
00228             }
00229             void store(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4) {
00230                 ma1.value->set(a1);
00231                 ma2.value->set(a2);
00232                 ma3.value->set(a3);
00233                 ma4.value->set(a4);
00234             }
00235         };
00236 
00237 
00244         template<class DataType>
00245         struct DataSourceStorage
00246             : public DataSourceStorageImpl<boost::function_traits<DataType>::arity, DataType>
00247         {
00248         };
00249     }
00250 }
00251 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3