MultiVectorComposition.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Sat May 21 20:15:51 CEST 2005  MultiVectorComposition.hpp
00003 
00004                         MultiVectorComposition.hpp -  description
00005                            -------------------
00006     begin                : Sat May 21 2005
00007     copyright            : (C) 2005 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 
00040 #ifndef MULTIVECTOR_COMPOSITION_HPP
00041 #define MULTIVECTOR_COMPOSITION_HPP
00042 
00043 #include "MultiVector.hpp"
00044 #include "../Property.hpp"
00045 #include "../PropertyBag.hpp"
00046 #include "../Logger.hpp"
00047 
00048 namespace RTT
00049 { namespace extras {
00050 
00055     template<class T, int S>
00056     void decomposeProperty(base::PropertyIntrospection *pi, const Property< MultiVector<S, T> >& c)
00057     {
00058         Property<PropertyBag> result(c.getName(),c.getDescription(), PropertyBag("MultiVector") );
00059 
00060         MultiVector<S,T> vec = c;
00061         Property<int>* dimension = new Property<int>("Size","Size of the MultiVector", vec.size() );
00062 
00063         result.value().add( dimension );
00064 
00065         std::stringstream data_name;
00066 
00067         for ( int i=0; i < dimension->get() ; i++)
00068             {
00069                 data_name  << i;
00070                 result.value().add( new Property<T>(data_name.str(),"",vec[i]) ); // Put variables in the bag
00071                 data_name.str("");
00072             }
00073 
00074         pi->introspect(result); // introspect the bag.
00075         deleteProperties( result.value() );
00076 
00077     }
00078 
00079     template<class T, int S>
00080     void decomposeProperty(base::PropertyIntrospection *pi, const Property< const MultiVector<S, T>& >& c)
00081     {
00082         Property<PropertyBag> result(c.getName(),c.getDescription(), PropertyBag("MultiVector") );
00083 
00084         MultiVector<S,T> vec = c;
00085         Property<int>* dimension = new Property<int>("Size","Size of the MultiVector", vec.size() );
00086 
00087         result.value().add( dimension );
00088 
00089         std::stringstream data_name;
00090 
00091         for ( int i=0; i < dimension->get() ; i++)
00092             {
00093                 data_name  << i;
00094                 result.value().add( new Property<T>(data_name.str(),"",vec[i]) ); // Put variables in the bag
00095                 data_name.str("");
00096             }
00097 
00098         pi->introspect(result); // introspect the bag.
00099         deleteProperties( result.value() );
00100 
00101     }
00102 
00106     template<class T, int S>
00107     bool composeProperty(const PropertyBag& bag, Property<MultiVector<S,T> >& result)
00108     {
00109         base::PropertyBase* v_base = bag.find( result.getName() );
00110         if ( v_base == 0 )
00111             return false;
00112 
00113         Property<PropertyBag>* v_bag = dynamic_cast< Property<PropertyBag>* >( v_base );
00114 
00115         TypeInfoRepository* tir = Types();
00116         if (v_bag != 0 && tir->type(v_bag->get().getType()) == tir->getTypeInfo<MultiVector<S,T> >() )
00117             {
00118                 Property<T>* comp;
00119 
00120                 Property<int>* dim;
00121                 v_base = v_bag->get().find("Size");
00122                 if ( v_base == 0 ) {
00123                     Logger::log() << Logger::Error << "In PropertyBag for Property< MultiVector<S,T> > :"
00124                                   << result.getName() << " : could not find property \"Size\"."<<Logger::endl;
00125                     return false;
00126                 }
00127                 dim = dynamic_cast< Property<int>* >(v_base);
00128                 if ( dim == 0) {
00129                     Logger::log() << Logger::Error << "In PropertyBag for Property< MultiVector<S,T> > :"
00130                                   << result.getName() << " : Expected \"Size\" to be of type short."<<Logger::endl;
00131                     return false;
00132                 }
00133                 int dimension = dim->get();
00134 
00135                 std::stringstream data_name;
00136 
00137                 // Get values
00138                 for (int i = 0; i < dimension ; i++)
00139                     {
00140                         data_name  << i;
00141                         base::PropertyBase* element = v_bag->get().find( data_name.str() );
00142                         if ( element == 0 ) {
00143                             Logger::log() << Logger::Error << "Aborting composition of Property< MultiVector<S,T> > "<<result.getName()
00144                                           << ": Data element "<< data_name.str() <<" not found !"
00145                                           <<Logger::endl;
00146                             return false;
00147                         }
00148                         comp = dynamic_cast< Property<T>* >( element );
00149                         if ( comp == 0 ) {
00150                             base::DataSourceBase::shared_ptr ds = element->getDataSource();
00151                             Logger::log() << Logger::Error << "Aborting composition of Property< MultiVector<S,T> > "<<result.getName()
00152                                           << ": Exptected data element "<< data_name.str() << " to be of type "<<internal::DataSource<T>::GetType()
00153                                           <<" got type " << element->getType()
00154                                           <<Logger::endl;
00155                             return false;
00156                         }
00157                         result.value()[i] = comp->get();
00158 
00159                         data_name.str("");
00160                     }
00161             }
00162         else
00163             {
00164                 if ( v_bag != 0 ) {
00165                     Logger::log() << Logger::Error << "Composing Property< MultiVector<S,T> > :"
00166                                   << result.getName() << " : type mismatch, got type '"<< v_bag->get().getType()  <<"'"<<Logger::endl;
00167                 } else {
00168                     Logger::log() << Logger::Error << "Composing Property< MultiVector<S,T> > :"
00169                                   << result.getName() << " : not a PropertyBag."<<Logger::endl;
00170                 }
00171                 // cerr << "\033[1;33mWarning: Bag was empty! \033[0m" << endl;
00172                 Logger::log() << Logger::Debug << "Could not update Property< MultiVector<S,T> > : "<<result.getName()<<Logger::endl;
00173                 return false;
00174             }
00175         return true;
00176 
00177     }
00178 
00179 }}; // namespace RTT
00180 
00181 
00182 #endif


rtt
Author(s): RTT Developers
autogenerated on Thu Jan 2 2014 11:35:22