VectorTemplateComposition.hpp
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 "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 );
00078 }
00079 else
00080 {
00081 log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
00082
00083 targetbag.add( new Property<T>("Element" + str, str +"th element of list",vec[i]) );
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
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
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
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