Orocos Real-Time Toolkit  2.5.0
SequenceConstructor.hpp
00001 #ifndef SEQUENCECONSTRUCTOR_HPP_
00002 #define SEQUENCECONSTRUCTOR_HPP_
00003 
00004 #include "TypeBuilder.hpp"
00005 #include "../internal/DataSources.hpp"
00006 #include <vector>
00007 
00008 namespace RTT
00009 {
00010     namespace types
00011     {
00015         template<class T>
00016         struct sequence_ctor: public std::unary_function<int, const T& >
00017         {
00018             typedef const T& ( Signature)(int);
00019             mutable boost::shared_ptr<T> ptr;
00020             sequence_ctor() :
00021                 ptr(new T())
00022             {
00023             }
00024             const T& operator()(int size) const
00025             {
00026                 ptr->resize(size);
00027                 return *(ptr);
00028             }
00029         };
00030 
00035         template<class T>
00036         struct sequence_varargs_ctor
00037         {
00038             typedef const std::vector<T>& result_type;
00039             typedef T argument_type;
00040             result_type operator()(const std::vector<T>& args) const
00041             {
00042                 return args;
00043             }
00044         };
00045 
00050         template<class T>
00051         struct sequence_constructor_datasource {
00052             typedef internal::NArityDataSource<sequence_varargs_ctor<T> > type;
00053         };
00054 
00059         template<class T>
00060         struct SequenceBuilder: public TypeBuilder
00061         {
00062             typedef typename T::value_type value_type;
00063             virtual base::DataSourceBase::shared_ptr build(const std::vector<
00064                     base::DataSourceBase::shared_ptr>& args) const
00065             {
00066                 if (args.size() == 0)
00067                     return base::DataSourceBase::shared_ptr();
00068                 typename sequence_constructor_datasource<value_type>::type::shared_ptr vds = new typename sequence_constructor_datasource<value_type>::type();
00069                 for (unsigned int i = 0; i != args.size(); ++i)
00070                 {
00071                     typename internal::DataSource<value_type>::shared_ptr dsd =
00072                             boost::dynamic_pointer_cast<internal::DataSource<value_type> >(
00073                                     args[i]);
00074                     if (dsd)
00075                         vds->add(dsd);
00076                     else
00077                         return base::DataSourceBase::shared_ptr();
00078                 }
00079                 return vds;
00080             }
00081 
00082         };
00083 
00089         template<class T>
00090         struct sequence_ctor2: public std::binary_function<int, typename T::value_type,
00091                 const T&>
00092         {
00093             typedef const T& ( Signature)(int, typename T::value_type);
00094             mutable boost::shared_ptr<T> ptr;
00095             sequence_ctor2() :
00096                 ptr(new T() )
00097             {
00098             }
00099             const T& operator()(int size, typename T::value_type value) const
00100             {
00101                 ptr->resize(size);
00102                 ptr->assign(size, value);
00103                 return *(ptr);
00104             }
00105         };
00106 
00107     }
00108 }
00109 
00110 #endif /* SEQUENCECONSTRUCTOR_HPP_ */