VectorTemplateComposition.hpp

00001 /***************************************************************************
00002   tag: Tinne De Laet 2007  VectorTemplateComposition.hpp
00003        2007 Ruben Smits
00004                         VectorTemplateComposition.hpp -  description
00005 
00006  ***************************************************************************
00007  *   This library is free software; you can redistribute it and/or         *
00008  *   modify it under the terms of the GNU General Public                   *
00009  *   License as published by the Free Software Foundation;                 *
00010  *   version 2 of the License.                                             *
00011  *                                                                         *
00012  *   As a special exception, you may use this file as part of a free       *
00013  *   software library without restriction.  Specifically, if other files   *
00014  *   instantiate templates or use macros or inline functions from this     *
00015  *   file, or you compile this file and link it with other files to        *
00016  *   produce an executable, this file does not by itself cause the         *
00017  *   resulting executable to be covered by the GNU General Public          *
00018  *   License.  This exception does not however invalidate any other        *
00019  *   reasons why the executable file might be covered by the GNU General   *
00020  *   Public License.                                                       *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU General Public             *
00028  *   License along with this library; if not, write to the Free Software   *
00029  *   Foundation, Inc., 59 Temple Place,                                    *
00030  *   Suite 330, Boston, MA  02111-1307  USA                                *
00031  *                                                                         *
00032  ***************************************************************************/
00033 
00034 #ifndef VECTOR_TEMPLATE_COMPOSITION_HPP
00035 #define VECTOR_TEMPLATE_COMPOSITION_HPP
00036 
00037 #include "Property.hpp"
00038 #include "PropertyBag.hpp"
00039 #include "TemplateTypeInfo.hpp"
00040 #include "Types.hpp"
00041 #include "Logger.hpp"
00042 #include "DataSources.hpp"
00043 #include <ostream>
00044 #include <sstream>
00045 #include <vector>
00046 
00047 namespace RTT
00048 {
00049     class PropertyIntrospection;
00050 
00057     template<class T>
00058     void decomposeProperty(const std::vector<T>& vec, PropertyBag& targetbag)
00059     {
00060         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00061         targetbag.setType(tname+"s");
00062         int dimension = vec.size();
00063         std::string str;
00064 
00065         assert( targetbag.empty() );
00066 
00067         for ( int i=0; i < dimension ; i++){
00068             std::stringstream out;
00069             out << i+1;
00070             str = out.str();
00071 
00072             Property<PropertyBag>* el_bag = new Property<PropertyBag>("Element" + str, str +"th element of list"); 
00073             Property<T> el("Element" + str, str +"th element of list",vec[i])  ;
00074             if(    el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
00075             {
00076                 log(Debug)<<"Element type "<<el.getType()<<" is a bag"<<endlog();
00077                 targetbag.add( el_bag ); // Put variables in the bag
00078             }
00079             else
00080             {
00081                 log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
00082                 //For Property
00083                 targetbag.add( new Property<T>("Element" + str, str +"th element of list",vec[i]) ); // Put variables in the bag
00084             }
00085         }
00086     };
00087 
00092     template<class T>
00093     bool composeProperty(const PropertyBag& bag, std::vector<T>& result)
00094     {
00095         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00096         
00097         if ( bag.getType() == tname+"s" ) {
00098             int dimension = bag.size();
00099             Logger::log() << Logger::Info << "bag size " << dimension <<Logger::endl;
00100             result.resize( dimension );
00101 
00102             // Get values
00103             for (int i = 0; i < dimension ; i++) {
00104                 std::stringstream out;
00105                 out << "Element";
00106                 out << i + 1;
00107                 Property<PropertyBag>* el_bag =  bag.getProperty<PropertyBag>(out.str());
00108 
00109                 if(el_bag==NULL){
00110                     // Works for properties in vector
00111                     PropertyBase* element = bag.getItem( i );
00112                     log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
00113                     Property<T> my_property_t (element->getName(),element->getDescription());
00114                     if(my_property_t.getType()!=element->getType())
00115                     {
00116                         log(Error)<< "Type of "<< element->getName() << " does not match type of "<<tname+"s"<< "OR "<<"Could not read element "<<i<<endlog();
00117                         return false;
00118                     }
00119                     else{
00120                         my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00121                         result[ i ] = my_property_t.get();
00122                     }
00123                 }
00124                 else{
00125                     // Works for propertybags in vector
00126                     const std::string el_bagType = el_bag->getType();
00127                     Property<T > el_p(el_bag->getName(),el_bag->getDescription());
00128                     if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
00129                         log(Error)<<"Could not compose element "<<i<<endlog();
00130                         return false;
00131                     }
00132                     if(el_p.ready()){
00133                         result[ i ] = el_p.get();
00134                     }else{
00135                         log(Error)<<"Property of Element"<<i<<"was not ready for use"<<endlog();
00136                         return false;
00137                     }
00138                 }
00139             }
00140         }
00141         else {
00142             Logger::log() << Logger::Error << "Composing Property< std::vector<T> > :"
00143                           << " type mismatch, got type '"<< bag.getType()
00144                           << "', expected type "<<tname<<"s."<<Logger::endl;
00145             return false;
00146         }
00147         return true;
00148     };
00149 
00150     template <typename T, bool has_ostream>
00151     struct StdVectorTemplateTypeInfo
00152         : public TemplateContainerTypeInfo<std::vector<T>, int, T, ArrayIndexChecker<std::vector<T> >, SizeAssignChecker<std::vector<T> >, has_ostream >
00153     {
00154         StdVectorTemplateTypeInfo<T,has_ostream>( std::string name )
00155             : TemplateContainerTypeInfo<std::vector<T>, int, T, ArrayIndexChecker<std::vector<T> >, SizeAssignChecker<std::vector<T> >, has_ostream >(name)
00156         {
00157         };
00158 
00159         bool decomposeTypeImpl(const std::vector<T>& vec, PropertyBag& targetbag) const
00160         {
00161             decomposeProperty<T>( vec, targetbag );
00162             return true;
00163         };
00164 
00165         bool composeTypeImpl(const PropertyBag& bag, std::vector<T>& result) const
00166         {
00167             return composeProperty<T>( bag, result );
00168         }
00169 
00170     };
00171 
00172     template<typename T>
00173     std::ostream& operator << (std::ostream& os, const std::vector<T>& vec)
00174     {
00175         os<<'[';
00176         for(unsigned int i=0;i<vec.size();i++){
00177             if(i>0)
00178                 os<<',';
00179             os<<vec[i]<<' ';
00180         }
00181         
00182         return os << ']';
00183     };
00184     
00185     template<typename T>
00186     std::istream& operator >> (std::istream& is,std::vector<T>& vec)
00187     {
00188         return is;
00189     };
00190 
00191     template<typename T>
00192     struct stdvector_ctor
00193         : public std::unary_function<int, const std::vector<T>&>
00194     {
00195         typedef const std::vector<T>& (Signature)( int );
00196         mutable boost::shared_ptr< std::vector<T> > ptr;
00197         stdvector_ctor()
00198             : ptr( new std::vector<T>() ) {}
00199         const std::vector<T>& operator()( int size ) const
00200         {
00201             ptr->resize( size );
00202             return *(ptr);
00203         }
00204     };
00205     
00210     template<typename T>
00211     struct stdvector_varargs_ctor
00212     {
00213         typedef const std::vector<T>& result_type;
00214         typedef T argument_type;
00215         result_type operator()( const std::vector<T>& args ) const
00216         {
00217             return args;
00218         }
00219     };
00220     
00225      template<typename T>
00226      struct StdVectorBuilder
00227          : public TypeBuilder
00228      {
00229          virtual DataSourceBase::shared_ptr build(const std::vector<DataSourceBase::shared_ptr>& args) const {
00230              if (args.size() == 0 )
00231                  return DataSourceBase::shared_ptr();
00232              typename NArityDataSource<stdvector_varargs_ctor<T> >::shared_ptr vds = new NArityDataSource<stdvector_varargs_ctor<T> >();
00233              for(unsigned int i=0; i != args.size(); ++i) {
00234                  typename DataSource<T>::shared_ptr dsd = AdaptDataSource<T>()( args[i] );
00235                  if (dsd)
00236                      vds->add( dsd );
00237                  else
00238                      return DataSourceBase::shared_ptr();
00239              }
00240              return vds;
00241          }
00242      };
00243 
00244     template<typename T>
00245     struct stdvector_ctor2
00246         : public std::binary_function<int, T, const std::vector<T>&>
00247     {
00248         typedef const std::vector<T>& (Signature)( int, T );
00249         mutable boost::shared_ptr< std::vector<T> > ptr;
00250         stdvector_ctor2()
00251             : ptr( new std::vector<T>() ) {}
00252         const std::vector<T>& operator()( int size, T value ) const
00253         {
00254             ptr->resize( size );
00255             ptr->assign( size, value );
00256             return *(ptr);
00257         }
00258     };
00259 
00260     template<typename T>
00261     struct stdvector_index
00262         : public std::binary_function<const std::vector<T>&, int, T>
00263     {
00264         T operator()(const std::vector<T>& v, int index) const
00265         {
00266             if ( index >= (int)(v.size()) || index < 0)
00267                 return T();
00268             return v[index];
00269         }
00270     };    
00271 
00272     template<class T>
00273     struct get_size
00274         : public std::unary_function<T, int>
00275     {
00276         int operator()(T cont ) const
00277         {
00278             return cont.size();
00279         }
00280     };
00281 
00282 }
00283 #endif
00284 
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3