00001 /*************************************************************************** 00002 tag: Tinne De Laet 2007 VectorTemplateComposition.hpp 00003 2007 Ruben Smits 00004 VectorTemplateComposition.hpp - description 00005 00006 *************************************************************************** 00007 * This library is free software; you can redistribute it and/or * 00008 * modify it under the terms of the GNU General Public * 00009 * License as published by the Free Software Foundation; * 00010 * version 2 of the License. * 00011 * * 00012 * As a special exception, you may use this file as part of a free * 00013 * software library without restriction. Specifically, if other files * 00014 * instantiate templates or use macros or inline functions from this * 00015 * file, or you compile this file and link it with other files to * 00016 * produce an executable, this file does not by itself cause the * 00017 * resulting executable to be covered by the GNU General Public * 00018 * License. This exception does not however invalidate any other * 00019 * reasons why the executable file might be covered by the GNU General * 00020 * Public License. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU General Public * 00028 * License along with this library; if not, write to the Free Software * 00029 * Foundation, Inc., 59 Temple Place, * 00030 * Suite 330, Boston, MA 02111-1307 USA * 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 "Types.hpp" 00040 #include "../Logger.hpp" 00041 #include "../internal/DataSources.hpp" 00042 #include <vector> 00043 00044 namespace RTT 00045 { 00046 namespace types { 00047 00048 template<class T> 00049 bool composeTemplateProperty(const PropertyBag& bag, T& result) 00050 { 00051 TypeInfoRepository::shared_ptr tir = Types(); 00052 if ( tir->type( bag.getType()) == tir->getTypeInfo< T >() ) { 00053 Property< typename T::value_type>* comp; 00054 int dimension = bag.size(); 00055 result.resize( dimension ); 00056 00057 // Get values 00058 int size_correction = 0; 00059 for (int i = 0; i < dimension ; i++) { 00060 base::PropertyBase* element = bag.getItem( i ); 00061 comp = dynamic_cast< Property< typename T::value_type>* >( element ); 00062 if ( comp == 0 ) { 00063 // detect LEGACY element: 00064 if ( element->getName() == "Size" ) { 00065 // oops, our result vector will be one smaller. 00066 size_correction += 1; 00067 continue; 00068 } 00069 Logger::log() << Logger::Error << "Aborting composition of Property< T > " 00070 << ": Exptected data element "<< i << " to be of type " << internal::DataSourceTypeInfo< typename T::value_type>::getTypeName() 00071 <<" got type " << element->getType() 00072 <<Logger::endl; 00073 return false; 00074 } 00075 result[ i - size_correction ] = comp->get(); 00076 } 00077 result.resize( dimension - size_correction ); 00078 } 00079 else { 00080 Logger::log() << Logger::Error << "Composing Property< T > :" 00081 << " type mismatch, got type '"<< bag.getType() 00082 << "', expected 'vector<" << internal::DataSourceTypeInfo< typename T::value_type>::getTypeName() <<">'."<<Logger::endl; 00083 return false; 00084 } 00085 return true; 00086 } 00087 } 00088 } 00089 00090 #endif 00091