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