SampleComposition.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Tinne De Laet 2009  SampleComposition.hpp
00003 
00004  ***************************************************************************
00005  *   This library is free software; you can redistribute it and/or         *
00006  *   modify it under the terms of the GNU General Public                   *
00007  *   License as published by the Free Software Foundation;                 *
00008  *   version 2 of the License.                                             *
00009  *                                                                         *
00010  *   As a special exception, you may use this file as part of a free       *
00011  *   software library without restriction.  Specifically, if other files   *
00012  *   instantiate templates or use macros or inline functions from this     *
00013  *   file, or you compile this file and link it with other files to        *
00014  *   produce an executable, this file does not by itself cause the         *
00015  *   resulting executable to be covered by the GNU General Public          *
00016  *   License.  This exception does not however invalidate any other        *
00017  *   reasons why the executable file might be covered by the GNU General   *
00018  *   Public License.                                                       *
00019  *                                                                         *
00020  *   This library is distributed in the hope that it will be useful,       *
00021  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00022  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00023  *   Lesser General Public License for more details.                       *
00024  *                                                                         *
00025  *   You should have received a copy of the GNU General Public             *
00026  *   License along with this library; if not, write to the Free Software   *
00027  *   Foundation, Inc., 59 Temple Place,                                    *
00028  *   Suite 330, Boston, MA  02111-1307  USA                                *
00029  *                                                                         *
00030  ***************************************************************************/
00031 
00032 #ifndef SAMPLE_COMPOSITION_HPP
00033 #define SAMPLE_COMPOSITION_HPP
00034 
00035 #include <rtt/Property.hpp>
00036 #include <rtt/PropertyBag.hpp>
00037 #include <rtt/TemplateTypeInfo.hpp>
00038 #include <rtt/Types.hpp>
00039 #include <rtt/Logger.hpp>
00040 #include <rtt/DataSources.hpp>
00041 #include <ostream>
00042 #include <sstream>
00043 #include <vector>
00044 
00045 #include "../../sample/sample.h"
00046 #include "../../sample/weightedsample.h"
00047 namespace BFL
00048 {
00049     using namespace RTT;
00050     using namespace RTT::detail;
00051     using namespace MatrixWrapper;
00052     class PropertyIntrospection;
00053 /*****************************************************************************
00054  * SAMPLE
00055  * **************************************************************************/
00056 
00061     template<class T>
00062     void decomposeProperty(const Sample<T>& sample, PropertyBag& targetbag)
00063     {
00064         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00065         targetbag.setType("Sample");
00066         //std::string str;
00067 
00068         assert( targetbag.empty() );
00069 
00070         bool result =true;
00071         //std::stringstream out;
00072         //out << i+1;
00073         //str = out.str();
00074 
00075         Property<PropertyBag>* el_bag = new Property<PropertyBag>("SampleValue", "Sample Value"); 
00076         Property<T> el("SampleValue" , "Sample value ",sample.ValueGet())  ;
00077         if(    el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
00078         {
00079             //log(Debug)<<"Element type "<<el.getType()<<" is a bag"<<endlog();
00080             targetbag.add( el_bag ); // Put variables in the bag
00081         }
00082         else
00083         {
00084             //log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
00085             //For Property
00086             targetbag.add( new Property<T>("SampleValue" ,"Sample Value",sample.ValueGet() )); // Put variables in the bag
00087         }
00088     };
00089 
00094     template<class T>
00095     bool composeProperty(const PropertyBag& bag, Sample<T>& sample)
00096     {
00097         //log(Debug) << "composeProperty of sample " << endlog();
00098         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00099         if ( bag.getType() == std::string("Sample") ) {
00100             // Get values
00101             Property<PropertyBag>* el_bag =  bag.getProperty<PropertyBag>("SampleValue");
00102 
00103             if(el_bag==NULL){
00104                 // Works for properties in Sample
00105                 PropertyBase* element = bag.getItem( 0 );
00106                 //log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
00107                 Property<T> my_property_t (element->getName(),element->getDescription());
00108                 if(my_property_t.getType()!=element->getType())
00109                 {
00110                     log(Error)<< "Type of "<< element->getName() << " does not match type of Sample"<< "OR "<<"Could not read Sample Value "<<endlog();
00111                     return false;
00112                 }
00113                 else{
00114                     my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00115                     sample.ValueSet( my_property_t.get());
00116                 }
00117             }
00118             else{
00119                 // Works for propertybags in Sample
00120                 const std::string el_bagType = el_bag->getType();
00121                 Property<T > el_p(el_bag->getName(),el_bag->getDescription());
00122                 if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
00123                     log(Error)<<"Could not compose SampleValue "<<endlog();
00124                     return false;
00125                 }
00126                 if(el_p.ready()){
00127                     sample.ValueSet( el_p.get());
00128                 }else{
00129                     log(Error)<<"Property of SampleValue was not ready for use"<<endlog();
00130                     return false;
00131                 }
00132             }
00133         }
00134         else {
00135             Logger::log() << Logger::Error << "Composing Property< Sample<T> > :"
00136                           << " type mismatch, got type '"<< bag.getType()
00137                           << "', expected type "<<tname<<"."<<Logger::endl;
00138             return false;
00139         }
00140         return true;
00141     };
00142 
00143     template <typename T>
00144     struct SampleTypeInfo
00145         : public TemplateTypeInfo<Sample<T>, true>
00146     {
00147         SampleTypeInfo<T>(std::string name)
00148             : TemplateTypeInfo<Sample<T>, true >(name)
00149         {
00150         };
00151 
00152         bool decomposeTypeImpl(const Sample<T>& sample, PropertyBag& targetbag) const
00153         {
00154             decomposeProperty<T>( sample, targetbag );
00155             return true;
00156         };
00157 
00158         bool composeTypeImpl(const PropertyBag& bag, Sample<T>& result) const
00159         {
00160             return composeProperty<T>( bag, result );
00161         }
00162 
00163     };
00164 
00165     template<typename T>
00166     struct Sample_ctor
00167         : public std::unary_function<T, const Sample<T>&>
00168     {
00169         typedef const Sample<T>& (Signature)( T );
00170         mutable boost::shared_ptr< Sample<T> > ptr;
00171         Sample_ctor()
00172             : ptr( new Sample<T>() ) {}
00173         const Sample<T>& operator()( T value ) const
00174         {
00175             ptr->ValueSet( value );
00176             return *(ptr);
00177         }
00178     };
00179 
00180 /*****************************************************************************
00181  * WEIGHTED SAMPLE
00182  * **************************************************************************/
00187     template<class T>
00188     void decomposeProperty(const WeightedSample<T>& weightedSample, PropertyBag& targetbag)
00189     {
00190         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00191         targetbag.setType("WeightedSample");
00192         //std::string str;
00193 
00194         assert( targetbag.empty() );
00195 
00196         bool result =true;
00197         //std::stringstream out;
00198         //out << i+1;
00199         //str = out.str();
00200 
00201         // add value
00202         Property<PropertyBag>* el_bag = new Property<PropertyBag>("WeightedSampleValue", "WeightedSample Value"); 
00203         Property<T> el("WeightedSampleValue" , "WeightedSample value ",weightedSample.ValueGet())  ;
00204         if(    el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
00205         {
00206             //log(Debug)<<"Element type "<<el.getType()<<" is a bag"<<endlog();
00207             targetbag.add( el_bag ); // Put variables in the bag
00208         }
00209         else
00210         {
00211             //log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
00212             //For Property
00213             targetbag.add( new Property<T>("WeightedSampleValue" ,"WeightedSample Value",weightedSample.ValueGet() )); // Put variables in the bag
00214         }
00215         // add weight
00216         targetbag.add( new Property<double>("WeightedSampleWeight" ,"WeightedSample Weight",weightedSample.WeightGet() )); // Put variables in the bag
00217     };
00218 
00223     template<class T>
00224     bool composeProperty(const PropertyBag& bag, WeightedSample<T>& weightedSample)
00225     {
00226         //log(Debug) << "composeProperty of WeightedSample " << endlog();
00227         std::string tname = detail::DataSourceTypeInfo<T>::getType();
00228         if ( bag.getType() == std::string("WeightedSample") ) {
00229             // Get values of sample
00230             Property<PropertyBag>* el_bag =  bag.getProperty<PropertyBag>("WeightedSampleValue");
00231 
00232             if(el_bag==NULL){
00233                 // Works for properties in WeightedSample
00234                 PropertyBase* element = bag.getItem( 0 );
00235                 //log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
00236                 Property<T> my_property_t (element->getName(),element->getDescription());
00237                 if(my_property_t.getType()!=element->getType())
00238                 {
00239                     log(Error)<< "Type of "<< element->getName() << " does not match type of WeightedSample"<< "OR "<<"Could not read WeightedSample Value "<<endlog();
00240                     return false;
00241                 }
00242                 else{
00243                     my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00244                     weightedSample.ValueSet( my_property_t.get());
00245                 }
00246             }
00247             else{
00248                 // Works for propertybags in WeightedSample
00249                 const std::string el_bagType = el_bag->getType();
00250                 Property<T > el_p(el_bag->getName(),el_bag->getDescription());
00251                 if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
00252                     log(Error)<<"Could not compose WeightedSampleValue "<<endlog();
00253                     return false;
00254                 }
00255                 if(el_p.ready()){
00256                     weightedSample.ValueSet( el_p.get());
00257                 }else{
00258                     log(Error)<<"Property of WeightedSampleValue was not ready for use"<<endlog();
00259                     return false;
00260                 }
00261             }
00262             // Get weight of sample
00263             Property<double>* weightProp =  bag.getProperty<double>("WeightedSampleWeight");
00264 
00265             if(!weightProp)
00266             {
00267                 log(Error)<< "Error reading weight of WeightedSample"<<endlog();
00268                 return false;
00269             }
00270             else{
00271                 weightedSample.WeightSet( weightProp->get());
00272             }
00273         }
00274         else {
00275             Logger::log() << Logger::Error << "Composing Property< WeightedSample<T> > :"
00276                           << " type mismatch, got type '"<< bag.getType()
00277                           << "', expected type "<<tname<<"."<<Logger::endl;
00278             return false;
00279         }
00280         return true;
00281     };
00282 
00283     template <typename T>
00284     struct WeightedSampleTypeInfo
00285         : public TemplateTypeInfo<WeightedSample<T>, true>
00286     {
00287         WeightedSampleTypeInfo<T>(std::string name)
00288             : TemplateTypeInfo<WeightedSample<T>, true >(name)
00289         {
00290         };
00291 
00292         bool decomposeTypeImpl(const WeightedSample<T>& weightedSample, PropertyBag& targetbag) const
00293         {
00294             decomposeProperty<T>( weightedSample, targetbag );
00295             return true;
00296         };
00297 
00298         bool composeTypeImpl(const PropertyBag& bag, WeightedSample<T>& result) const
00299         {
00300             return composeProperty<T>( bag, result );
00301         }
00302 
00303     };
00304 
00305     template<typename T>
00306     struct WeightedSample_ctor
00307         : public std::binary_function<T ,double , const WeightedSample<T>&>
00308     {
00309         typedef const WeightedSample<T>& (Signature)( T, double );
00310         mutable boost::shared_ptr< WeightedSample<T> > ptr;
00311         WeightedSample_ctor()
00312             : ptr( new WeightedSample<T>() ) {}
00313         const WeightedSample<T>& operator()( T value , double weight) const
00314         {
00315             ptr->ValueSet( value );
00316             ptr->WeightSet( weight );
00317             return *(ptr);
00318         }
00319     };
00320     
00325 /*
00326     template<typename T>
00327     struct Sample_varargs_ctor
00328     {
00329         typedef const Sample<T>& result_type;
00330         typedef T argument_type;
00331         result_type operator()( const Sample<T>& args ) const
00332         {
00333             return args;
00334         }
00335     };
00336 */
00337     
00338 
00339 };
00340 #endif
00341 


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Fri Aug 28 2015 10:10:21