Orocos Real-Time Toolkit  2.6.0
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 "SequenceTypeInfo.hpp"
00040 #include "Types.hpp"
00041 #include "../Logger.hpp"
00042 #include "../internal/DataSources.hpp"
00043 #include <ostream>
00044 #include <sstream>
00045 #include <vector>
00046 
00047 namespace RTT
00048 { namespace types {
00049     template <typename T, bool has_ostream>
00050     struct StdVectorTemplateTypeInfo
00051         : public SequenceTypeInfo<std::vector<T>, has_ostream >
00052     {
00053         StdVectorTemplateTypeInfo<T,has_ostream>( std::string name )
00054             : SequenceTypeInfo<std::vector<T>, has_ostream >(name)
00055         {
00056         }
00057     };
00058 
00059     template<typename T>
00060     std::ostream& operator << (std::ostream& os, const std::vector<T>& vec)
00061     {
00062         os<<'[';
00063         for(unsigned int i=0;i<vec.size();i++){
00064             if(i>0)
00065                 os<<',';
00066             os<<vec[i]<<' ';
00067         }
00068 
00069         return os << ']';
00070     }
00071 
00072     template<typename T>
00073     std::istream& operator >> (std::istream& is,std::vector<T>& vec)
00074     {
00075         return is;
00076     }
00077 
00078     template<typename T>
00079     struct stdvector_ctor
00080         : public std::unary_function<int, const std::vector<T>&>
00081     {
00082         typedef const std::vector<T>& (Signature)( int );
00083         mutable boost::shared_ptr< std::vector<T> > ptr;
00084         stdvector_ctor()
00085             : ptr( new std::vector<T>() ) {}
00086         const std::vector<T>& operator()( int size ) const
00087         {
00088             ptr->resize( size );
00089             return *(ptr);
00090         }
00091     };
00092 
00097     template<typename T>
00098     struct stdvector_varargs_ctor
00099     {
00100         typedef const std::vector<T>& result_type;
00101         typedef T argument_type;
00102         result_type operator()( const std::vector<T>& args ) const
00103         {
00104             return args;
00105         }
00106     };
00107 
00112      template<typename T>
00113      struct StdVectorBuilder
00114          : public TypeConstructor
00115      {
00116          virtual base::DataSourceBase::shared_ptr build(const std::vector<base::DataSourceBase::shared_ptr>& args) const {
00117              if (args.size() == 0 )
00118                  return base::DataSourceBase::shared_ptr();
00119              typename internal::NArityDataSource<stdvector_varargs_ctor<T> >::shared_ptr vds = new internal::NArityDataSource<stdvector_varargs_ctor<T> >();
00120              for(unsigned int i=0; i != args.size(); ++i) {
00121                  typename internal::DataSource<T>::shared_ptr dsd = boost::dynamic_pointer_cast< internal::DataSource<T> >( args[i] );
00122                  if (dsd)
00123                      vds->add( dsd );
00124                  else
00125                      return base::DataSourceBase::shared_ptr();
00126              }
00127              return vds;
00128          }
00129      };
00130 
00131     template<typename T>
00132     struct stdvector_ctor2
00133         : public std::binary_function<int, T, const std::vector<T>&>
00134     {
00135         typedef const std::vector<T>& (Signature)( int, T );
00136         mutable boost::shared_ptr< std::vector<T> > ptr;
00137         stdvector_ctor2()
00138             : ptr( new std::vector<T>() ) {}
00139         const std::vector<T>& operator()( int size, T value ) const
00140         {
00141             ptr->resize( size );
00142             ptr->assign( size, value );
00143             return *(ptr);
00144         }
00145     };
00146 }}
00147 
00148 #ifdef __clang__
00149 namespace std {
00150     using RTT::types::operator<<;
00151     using RTT::types::operator>>;
00152 }
00153 #endif
00154 
00155 #endif
00156