00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef VECTOR_TEMPLATE_COMPOSITION_HPP
00035 #define VECTOR_TEMPLATE_COMPOSITION_HPP
00036
00037 #include <rtt/Property.hpp>
00038 #include <rtt/PropertyBag.hpp>
00039 #include <rtt/TemplateTypeInfo.hpp>
00040 #include <rtt/Types.hpp>
00041 #include <rtt/Logger.hpp>
00042 #include <rtt/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 targetbag.add( new Property<T>("Element " + str, str +"th element of list",vec[i]) );
00072 }
00073 };
00078 template<class T>
00079 bool composeProperty(const PropertyBag& bag, std::vector<T>& result)
00080 {
00081 std::string tname = detail::DataSourceTypeInfo<T>::getType();
00082
00083 if ( bag.getType() == tname+"s" ) {
00084 int dimension = bag.size();
00085 Logger::log() << Logger::Info << "bag size " << dimension <<Logger::endl;
00086 result.resize( dimension );
00087
00088
00089 for (int i = 0; i < dimension ; i++) {
00090 PropertyBase* element = bag.getItem( i );
00091
00092 log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
00093
00094 Property<T> my_property_t (element->getName(),element->getDescription());
00095 if(my_property_t.getType()!=element->getType())
00096 log(Error)<< "Type of "<< element->getName() << " does not match type of "<<tname+"s"<<endlog();
00097 else{
00098 my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00099 result[ i ] = my_property_t.get();
00100 }
00101 }
00102 }
00103 else {
00104 Logger::log() << Logger::Error << "Composing Property< std::vector<T> > :"
00105 << " type mismatch, got type '"<< bag.getType()
00106 << "', expected type "<<tname<<"s."<<Logger::endl;
00107 return false;
00108 }
00109 return true;
00110 };
00111
00112 template <typename T, bool has_ostream>
00113 struct StdVectorTemplateTypeInfo
00114 : public TemplateContainerTypeInfo<std::vector<T>, int, T, ArrayIndexChecker<std::vector<T> >, SizeAssignChecker<std::vector<T> >, has_ostream >
00115 {
00116 StdVectorTemplateTypeInfo<T,has_ostream>( std::string name )
00117 : TemplateContainerTypeInfo<std::vector<T>, int, T, ArrayIndexChecker<std::vector<T> >, SizeAssignChecker<std::vector<T> >, has_ostream >(name)
00118 {
00119 };
00120
00121 bool decomposeTypeImpl(const std::vector<T>& vec, PropertyBag& targetbag) const
00122 {
00123 decomposeProperty<T>( vec, targetbag );
00124 return true;
00125 };
00126
00127 bool composeTypeImpl(const PropertyBag& bag, std::vector<T>& result) const
00128 {
00129 return composeProperty<T>( bag, result );
00130 }
00131
00132 };
00133
00134 template<typename T>
00135 std::ostream& operator << (std::ostream& os, const std::vector<T>& vec)
00136 {
00137 os<<'[';
00138 for(unsigned int i=0;i<vec.size();i++){
00139 if(i>0)
00140 os<<',';
00141 os<<vec[i]<<' ';
00142 }
00143
00144 return os << ']';
00145 };
00146
00147 template<typename T>
00148 std::istream& operator >> (std::istream& is,std::vector<T>& vec)
00149 {
00150 return is;
00151 };
00152
00153 template<typename T>
00154 struct stdvector_ctor
00155 : public std::unary_function<int, const std::vector<T>&>
00156 {
00157 typedef const std::vector<T>& (Signature)( int );
00158 mutable boost::shared_ptr< std::vector<T> > ptr;
00159 stdvector_ctor()
00160 : ptr( new std::vector<T>() ) {}
00161 const std::vector<T>& operator()( int size ) const
00162 {
00163 ptr->resize( size );
00164 return *(ptr);
00165 }
00166 };
00167
00172 template<typename T>
00173 struct stdvector_varargs_ctor
00174 {
00175 typedef const std::vector<T>& result_type;
00176 typedef T argument_type;
00177 result_type operator()( const std::vector<T>& args ) const
00178 {
00179 return args;
00180 }
00181 };
00182
00187 template<typename T>
00188 struct StdVectorBuilder
00189 : public TypeBuilder
00190 {
00191 virtual DataSourceBase::shared_ptr build(const std::vector<DataSourceBase::shared_ptr>& args) const {
00192 if (args.size() == 0 )
00193 return DataSourceBase::shared_ptr();
00194 typename NArityDataSource<stdvector_varargs_ctor<T> >::shared_ptr vds = new NArityDataSource<stdvector_varargs_ctor<T> >();
00195 for(unsigned int i=0; i != args.size(); ++i) {
00196 typename DataSource<T>::shared_ptr dsd = AdaptDataSource<T>()( args[i] );
00197 if (dsd)
00198 vds->add( dsd );
00199 else
00200 return DataSourceBase::shared_ptr();
00201 }
00202 return vds;
00203 }
00204 };
00205
00206 template<typename T>
00207 struct stdvector_ctor2
00208 : public std::binary_function<int, T, const std::vector<T>&>
00209 {
00210 typedef const std::vector<T>& (Signature)( int, T );
00211 mutable boost::shared_ptr< std::vector<T> > ptr;
00212 stdvector_ctor2()
00213 : ptr( new std::vector<T>() ) {}
00214 const std::vector<T>& operator()( int size, T value ) const
00215 {
00216 ptr->resize( size );
00217 ptr->assign( size, value );
00218 return *(ptr);
00219 }
00220 };
00221
00222 template<typename T>
00223 struct stdvector_index
00224 : public std::binary_function<const std::vector<T>&, int, T>
00225 {
00226 T operator()(const std::vector<T>& v, int index) const
00227 {
00228 if ( index >= (int)(v.size()) || index < 0)
00229 return T();
00230 return v[index];
00231 }
00232 };
00233
00234 template<class T>
00235 struct get_size
00236 : public std::unary_function<T, int>
00237 {
00238 int operator()(T cont ) const
00239 {
00240 return cont.size();
00241 }
00242 };
00243
00244 };
00245 #endif
00246