/*************************************************************************** tag: Tinne De Laet Soetens 2007 VectorTemplateComposition.hpp 2007 Ruben Smits VectorTemplateComposition.hpp - description *************************************************************************** * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public * * License as published by the Free Software Foundation; * * version 2 of the License. * * * * As a special exception, you may use this file as part of a free * * software library without restriction. Specifically, if other files * * instantiate templates or use macros or inline functions from this * * file, or you compile this file and link it with other files to * * produce an executable, this file does not by itself cause the * * resulting executable to be covered by the GNU General Public * * License. This exception does not however invalidate any other * * reasons why the executable file might be covered by the GNU General * * Public License. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, * * Suite 330, Boston, MA 02111-1307 USA * * * ***************************************************************************/ #ifndef VECTOR_TEMPLATE_COMPOSITION_HPP #define VECTOR_TEMPLATE_COMPOSITION_HPP #include #include #include #include #include #include #include #include #include namespace RTT { class PropertyIntrospection; /** * A decomposeProperty method for decomposing a vector * into a PropertyBag with Property's. * The dimension of the vector must be less than 100 if you want the * Property's to have a different name. */ template void decomposeProperty(const std::vector& vec, PropertyBag& targetbag) { std::string tname = detail::DataSourceTypeInfo::getType(); targetbag.setType(tname+"s"); int dimension = vec.size(); std::string str; assert( targetbag.empty() ); for ( int i=0; i < dimension ; i++){ std::stringstream out; out << i+1; str = out.str(); targetbag.add( new Property("Element " + str, str +"th element of list",vec[i]) ); // Put variables in the bag } }; /** * A composeProperty method for composing a property of a vector * The dimension of the vector must be less than 100. */ template bool composeProperty(const PropertyBag& bag, std::vector& result) { std::string tname = detail::DataSourceTypeInfo::getType(); if ( bag.getType() == tname+"s" ) { int dimension = bag.size(); Logger::log() << Logger::Info << "bag size " << dimension <* el_bag = bag.getProperty(out.str()); if(el_bag==NULL){ // Works for properties in vector PropertyBase* element = bag.getItem( i ); //Property* t_bag= dynamic_cast< Property* >( element ); log(Debug)<getName()<<", "<< element->getDescription()< my_property_t (t_abag->getName(),t_bag->getDescription()); Property my_property_t (element->getName(),element->getDescription()); log(Debug) << "element type" << element->getType() << endlog(); log(Debug) << "property type" << my_property_t.getType() << endlog(); if(my_property_t.getType()!=element->getType()) { log(Error)<< "Type of "<< element->getName() << " does not match type of "<composeType(element->getDataSource(),my_property_t.getDataSource()); result[ i ] = my_property_t.get(); } } else{ // Works for propertybags in vector const std::string el_bagType = el_bag->getType(); log(Debug) << "el_bagType " << el_bagType << endlog(); //Property el_p(el_bag->getName(),el_bag->getDescription()); Property el_p(el_bag->getName(),el_bag->getDescription()); log(Debug) << "el_p type" << el_p.getType() << endlog(); if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){ log(Error)<<"Could not compose element "<* t_bag= dynamic_cast< Property* >( element ); log(Debug)<getName()<<", "<< element->getDescription()< my_property_t (t_abag->getName(),t_bag->getDescription()); Property my_property_t (element->getName(),element->getDescription()); log(Debug) << "element type" << element->getType() << endlog(); log(Debug) << "property type" << my_property_t.getType() << endlog(); if(my_property_t.getType()!=element->getType()) log(Error)<< "Type of "<< element->getName() << " does not match type of "<composeType(element->getDataSource(),my_property_t.getDataSource()); result[ i ] = my_property_t.get(); } */ } } else { Logger::log() << Logger::Error << "Composing Property< std::vector > :" << " type mismatch, got type '"<< bag.getType() << "', expected type "< struct StdVectorTemplateTypeInfo : public TemplateContainerTypeInfo, int, T, ArrayIndexChecker >, SizeAssignChecker >, has_ostream > { StdVectorTemplateTypeInfo( std::string name ) : TemplateContainerTypeInfo, int, T, ArrayIndexChecker >, SizeAssignChecker >, has_ostream >(name) { }; bool decomposeTypeImpl(const std::vector& vec, PropertyBag& targetbag) const { decomposeProperty( vec, targetbag ); return true; }; bool composeTypeImpl(const PropertyBag& bag, std::vector& result) const { return composeProperty( bag, result ); } }; template std::ostream& operator << (std::ostream& os, const std::vector& vec) { os<<'['; for(unsigned int i=0;i0) os<<','; os< std::istream& operator >> (std::istream& is,std::vector& vec) { return is; }; template struct stdvector_ctor : public std::unary_function&> { typedef const std::vector& (Signature)( int ); mutable boost::shared_ptr< std::vector > ptr; stdvector_ctor() : ptr( new std::vector() ) {} const std::vector& operator()( int size ) const { ptr->resize( size ); return *(ptr); } }; /** * See NArityDataSource which requires a function object like * this one. */ template struct stdvector_varargs_ctor { typedef const std::vector& result_type; typedef T argument_type; result_type operator()( const std::vector& args ) const { return args; } }; /** * Constructs an array with \a n elements, which are given upon * construction time. */ template struct StdVectorBuilder : public TypeBuilder { virtual DataSourceBase::shared_ptr build(const std::vector& args) const { if (args.size() == 0 ) return DataSourceBase::shared_ptr(); typename NArityDataSource >::shared_ptr vds = new NArityDataSource >(); for(unsigned int i=0; i != args.size(); ++i) { typename DataSource::shared_ptr dsd = AdaptDataSource()( args[i] ); if (dsd) vds->add( dsd ); else return DataSourceBase::shared_ptr(); } return vds; } }; template struct stdvector_ctor2 : public std::binary_function&> { typedef const std::vector& (Signature)( int, T ); mutable boost::shared_ptr< std::vector > ptr; stdvector_ctor2() : ptr( new std::vector() ) {} const std::vector& operator()( int size, T value ) const { ptr->resize( size ); ptr->assign( size, value ); return *(ptr); } }; template struct stdvector_index : public std::binary_function&, int, T> { T operator()(const std::vector& v, int index) const { if ( index >= (int)(v.size()) || index < 0) return T(); return v[index]; } }; template struct get_size : public std::unary_function { int operator()(T cont ) const { return cont.size(); } }; }; #endif