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             bool result =true;
00069             std::stringstream out;
00070             out << i+1;
00071             str = out.str();
00072 
00073             Property<PropertyBag>* el_bag = new Property<PropertyBag>("Element" + str, str +"th element of list"); 
00074             Property<T> el("Element" + str, str +"th element of list",vec[i])  ;
00075             if(    el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
00076             {
00077                 log(Debug)<<"Element type "<<el.getType()<<" is a bag"<<endlog();
00078                 targetbag.add( el_bag ); // Put variables in the bag
00079             }
00080             else
00081             {
00082                 log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
00083                 //For Property
00084                 targetbag.add( new Property<T>("Element" + str, str +"th element of list",vec[i]) ); // Put variables in the bag
00085             }
00086         }
00087     };
00088 
00093     template<class T>
00094     bool composeProperty(const PropertyBag& bag, std::vector<T>& result)
00095     {
00096         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00097         
00098         if ( bag.getType() == tname+"s" ) {
00099             int dimension = bag.size();
00100             Logger::log() << Logger::Info << "bag size " << dimension <<Logger::endl;
00101             result.resize( dimension );
00102 
00103             // Get values
00104             for (int i = 0; i < dimension ; i++) {
00105                 std::stringstream out;
00106                 out << "Element";
00107                 out << i + 1;
00108                 Property<PropertyBag>* el_bag =  bag.getProperty<PropertyBag>(out.str());
00109 
00110                 if(el_bag==NULL){
00111                     // Works for properties in vector
00112                     PropertyBase* element = bag.getItem( i );
00113                     log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
00114                     Property<T> my_property_t (element->getName(),element->getDescription());
00115                     if(my_property_t.getType()!=element->getType())
00116                     {
00117                         log(Error)<< "Type of "<< element->getName() << " does not match type of "<<tname+"s"<< "OR "<<"Could not read element "<<i<<endlog();
00118                         return false;
00119                     }
00120                     else{
00121                         my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00122                         result[ i ] = my_property_t.get();
00123                     }
00124                 }
00125                 else{
00126                     // Works for propertybags in vector
00127                     const std::string el_bagType = el_bag->getType();
00128                     Property<T > el_p(el_bag->getName(),el_bag->getDescription());
00129                     if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
00130                         log(Error)<<"Could not compose element "<<i<<endlog();
00131                         return false;
00132                     }
00133                     if(el_p.ready()){
00134                         result[ i ] = el_p.get();
00135                     }else{
00136                         log(Error)<<"Property of Element"<<i<<"was not ready for use"<<endlog();
00137                         return false;
00138                     }
00139                 }
00140             }
00141         }
00142         else {
00143             Logger::log() << Logger::Error << "Composing Property< std::vector<T> > :"
00144                           << " type mismatch, got type '"<< bag.getType()
00145                           << "', expected type "<<tname<<"s."<<Logger::endl;
00146             return false;
00147         }
00148         return true;
00149     };
00150 
00151     template <typename T, bool has_ostream>
00152     struct StdVectorTemplateTypeInfo
00153         : public TemplateContainerTypeInfo<std::vector<T>, int, T, ArrayIndexChecker<std::vector<T> >, SizeAssignChecker<std::vector<T> >, has_ostream >
00154     {
00155         StdVectorTemplateTypeInfo<T,has_ostream>( std::string name )
00156             : TemplateContainerTypeInfo<std::vector<T>, int, T, ArrayIndexChecker<std::vector<T> >, SizeAssignChecker<std::vector<T> >, has_ostream >(name)
00157         {
00158         };
00159 
00160         bool decomposeTypeImpl(const std::vector<T>& vec, PropertyBag& targetbag) const
00161         {
00162             decomposeProperty<T>( vec, targetbag );
00163             return true;
00164         };
00165 
00166         bool composeTypeImpl(const PropertyBag& bag, std::vector<T>& result) const
00167         {
00168             return composeProperty<T>( bag, result );
00169         }
00170 
00171     };
00172 
00173     template<typename T>
00174     std::ostream& operator << (std::ostream& os, const std::vector<T>& vec)
00175     {
00176         os<<'[';
00177         for(unsigned int i=0;i<vec.size();i++){
00178             if(i>0)
00179                 os<<',';
00180             os<<vec[i]<<' ';
00181         }
00182         
00183         return os << ']';
00184     };
00185     
00186     template<typename T>
00187     std::istream& operator >> (std::istream& is,std::vector<T>& vec)
00188     {
00189         return is;
00190     };
00191 
00192     template<typename T>
00193     struct stdvector_ctor
00194         : public std::unary_function<int, const std::vector<T>&>
00195     {
00196         typedef const std::vector<T>& (Signature)( int );
00197         mutable boost::shared_ptr< std::vector<T> > ptr;
00198         stdvector_ctor()
00199             : ptr( new std::vector<T>() ) {}
00200         const std::vector<T>& operator()( int size ) const
00201         {
00202             ptr->resize( size );
00203             return *(ptr);
00204         }
00205     };
00206     
00211     template<typename T>
00212     struct stdvector_varargs_ctor
00213     {
00214         typedef const std::vector<T>& result_type;
00215         typedef T argument_type;
00216         result_type operator()( const std::vector<T>& args ) const
00217         {
00218             return args;
00219         }
00220     };
00221     
00226      template<typename T>
00227      struct StdVectorBuilder
00228          : public TypeBuilder
00229      {
00230          virtual DataSourceBase::shared_ptr build(const std::vector<DataSourceBase::shared_ptr>& args) const {
00231              if (args.size() == 0 )
00232                  return DataSourceBase::shared_ptr();
00233              typename NArityDataSource<stdvector_varargs_ctor<T> >::shared_ptr vds = new NArityDataSource<stdvector_varargs_ctor<T> >();
00234              for(unsigned int i=0; i != args.size(); ++i) {
00235                  typename DataSource<T>::shared_ptr dsd = AdaptDataSource<T>()( args[i] );
00236                  if (dsd)
00237                      vds->add( dsd );
00238                  else
00239                      return DataSourceBase::shared_ptr();
00240              }
00241              return vds;
00242          }
00243      };
00244 
00245     template<typename T>
00246     struct stdvector_ctor2
00247         : public std::binary_function<int, T, const std::vector<T>&>
00248     {
00249         typedef const std::vector<T>& (Signature)( int, T );
00250         mutable boost::shared_ptr< std::vector<T> > ptr;
00251         stdvector_ctor2()
00252             : ptr( new std::vector<T>() ) {}
00253         const std::vector<T>& operator()( int size, T value ) const
00254         {
00255             ptr->resize( size );
00256             ptr->assign( size, value );
00257             return *(ptr);
00258         }
00259     };
00260 
00261     template<typename T>
00262     struct stdvector_index
00263         : public std::binary_function<const std::vector<T>&, int, T>
00264     {
00265         T operator()(const std::vector<T>& v, int index) const
00266         {
00267             if ( index >= (int)(v.size()) || index < 0)
00268                 return T();
00269             return v[index];
00270         }
00271     };    
00272 
00273     template<class T>
00274     struct get_size
00275         : public std::unary_function<T, int>
00276     {
00277         int operator()(T cont ) const
00278         {
00279             return cont.size();
00280         }
00281     };
00282 
00283 };
00284 #endif
00285 

Generated on Tue Aug 25 14:17:23 2009 for Orocos Real-Time Toolkit by  doxygen 1.5.8