SampleComposition.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Tinne De Laet 2009 SampleComposition.hpp
3 
4  ***************************************************************************
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public *
7  * License as published by the Free Software Foundation; *
8  * version 2 of the License. *
9  * *
10  * As a special exception, you may use this file as part of a free *
11  * software library without restriction. Specifically, if other files *
12  * instantiate templates or use macros or inline functions from this *
13  * file, or you compile this file and link it with other files to *
14  * produce an executable, this file does not by itself cause the *
15  * resulting executable to be covered by the GNU General Public *
16  * License. This exception does not however invalidate any other *
17  * reasons why the executable file might be covered by the GNU General *
18  * Public License. *
19  * *
20  * This library is distributed in the hope that it will be useful, *
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
23  * Lesser General Public License for more details. *
24  * *
25  * You should have received a copy of the GNU General Public *
26  * License along with this library; if not, write to the Free Software *
27  * Foundation, Inc., 59 Temple Place, *
28  * Suite 330, Boston, MA 02111-1307 USA *
29  * *
30  ***************************************************************************/
31 
32 #ifndef SAMPLE_COMPOSITION_HPP
33 #define SAMPLE_COMPOSITION_HPP
34 
35 #include <rtt/Property.hpp>
36 #include <rtt/PropertyBag.hpp>
37 #include <rtt/TemplateTypeInfo.hpp>
38 #include <rtt/Types.hpp>
39 #include <rtt/Logger.hpp>
40 #include <rtt/DataSources.hpp>
41 #include <ostream>
42 #include <sstream>
43 #include <vector>
44 
45 #include "../../sample/sample.h"
46 #include "../../sample/weightedsample.h"
47 namespace BFL
48 {
49  using namespace RTT;
50  using namespace RTT::detail;
51  using namespace MatrixWrapper;
52  class PropertyIntrospection;
53 /*****************************************************************************
54  * SAMPLE
55  * **************************************************************************/
56 
61  template<class T>
62  void decomposeProperty(const Sample<T>& sample, PropertyBag& targetbag)
63  {
64  std::string tname = detail::DataSourceTypeInfo<T>::getType();
65  targetbag.setType("Sample");
66  //std::string str;
67 
68  assert( targetbag.empty() );
69 
70  bool result =true;
71  //std::stringstream out;
72  //out << i+1;
73  //str = out.str();
74 
75  Property<PropertyBag>* el_bag = new Property<PropertyBag>("SampleValue", "Sample Value");
76  Property<T> el("SampleValue" , "Sample value ",sample.ValueGet()) ;
77  if( el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
78  {
79  //log(Debug)<<"Element type "<<el.getType()<<" is a bag"<<endlog();
80  targetbag.add( el_bag ); // Put variables in the bag
81  }
82  else
83  {
84  //log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
85  //For Property
86  targetbag.add( new Property<T>("SampleValue" ,"Sample Value",sample.ValueGet() )); // Put variables in the bag
87  }
88  };
89 
94  template<class T>
95  bool composeProperty(const PropertyBag& bag, Sample<T>& sample)
96  {
97  //log(Debug) << "composeProperty of sample " << endlog();
98  std::string tname = detail::DataSourceTypeInfo<T>::getType();
99  if ( bag.getType() == std::string("Sample") ) {
100  // Get values
101  Property<PropertyBag>* el_bag = bag.getProperty<PropertyBag>("SampleValue");
102 
103  if(el_bag==NULL){
104  // Works for properties in Sample
105  PropertyBase* element = bag.getItem( 0 );
106  //log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
107  Property<T> my_property_t (element->getName(),element->getDescription());
108  if(my_property_t.getType()!=element->getType())
109  {
110  log(Error)<< "Type of "<< element->getName() << " does not match type of Sample"<< "OR "<<"Could not read Sample Value "<<endlog();
111  return false;
112  }
113  else{
114  my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
115  sample.ValueSet( my_property_t.get());
116  }
117  }
118  else{
119  // Works for propertybags in Sample
120  const std::string el_bagType = el_bag->getType();
121  Property<T > el_p(el_bag->getName(),el_bag->getDescription());
122  if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
123  log(Error)<<"Could not compose SampleValue "<<endlog();
124  return false;
125  }
126  if(el_p.ready()){
127  sample.ValueSet( el_p.get());
128  }else{
129  log(Error)<<"Property of SampleValue was not ready for use"<<endlog();
130  return false;
131  }
132  }
133  }
134  else {
135  Logger::log() << Logger::Error << "Composing Property< Sample<T> > :"
136  << " type mismatch, got type '"<< bag.getType()
137  << "', expected type "<<tname<<"."<<Logger::endl;
138  return false;
139  }
140  return true;
141  };
142 
143  template <typename T>
145  : public TemplateTypeInfo<Sample<T>, true>
146  {
147  SampleTypeInfo<T>(std::string name)
148  : TemplateTypeInfo<Sample<T>, true >(name)
149  {
150  };
151 
152  bool decomposeTypeImpl(const Sample<T>& sample, PropertyBag& targetbag) const
153  {
154  decomposeProperty<T>( sample, targetbag );
155  return true;
156  };
157 
158  bool composeTypeImpl(const PropertyBag& bag, Sample<T>& result) const
159  {
160  return composeProperty<T>( bag, result );
161  }
162 
163  };
164 
165  template<typename T>
166  struct Sample_ctor
167  : public std::unary_function<T, const Sample<T>&>
168  {
169  typedef const Sample<T>& (Signature)( T );
172  : ptr( new Sample<T>() ) {}
173  const Sample<T>& operator()( T value ) const
174  {
175  ptr->ValueSet( value );
176  return *(ptr);
177  }
178  };
179 
180 /*****************************************************************************
181  * WEIGHTED SAMPLE
182  * **************************************************************************/
187  template<class T>
188  void decomposeProperty(const WeightedSample<T>& weightedSample, PropertyBag& targetbag)
189  {
190  std::string tname = detail::DataSourceTypeInfo<T>::getType();
191  targetbag.setType("WeightedSample");
192  //std::string str;
193 
194  assert( targetbag.empty() );
195 
196  bool result =true;
197  //std::stringstream out;
198  //out << i+1;
199  //str = out.str();
200 
201  // add value
202  Property<PropertyBag>* el_bag = new Property<PropertyBag>("WeightedSampleValue", "WeightedSample Value");
203  Property<T> el("WeightedSampleValue" , "WeightedSample value ",weightedSample.ValueGet()) ;
204  if( el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
205  {
206  //log(Debug)<<"Element type "<<el.getType()<<" is a bag"<<endlog();
207  targetbag.add( el_bag ); // Put variables in the bag
208  }
209  else
210  {
211  //log(Debug)<<"Element type "<<el.getType()<<" is not a bag"<<endlog();
212  //For Property
213  targetbag.add( new Property<T>("WeightedSampleValue" ,"WeightedSample Value",weightedSample.ValueGet() )); // Put variables in the bag
214  }
215  // add weight
216  targetbag.add( new Property<double>("WeightedSampleWeight" ,"WeightedSample Weight",weightedSample.WeightGet() )); // Put variables in the bag
217  };
218 
223  template<class T>
224  bool composeProperty(const PropertyBag& bag, WeightedSample<T>& weightedSample)
225  {
226  //log(Debug) << "composeProperty of WeightedSample " << endlog();
227  std::string tname = detail::DataSourceTypeInfo<T>::getType();
228  if ( bag.getType() == std::string("WeightedSample") ) {
229  // Get values of sample
230  Property<PropertyBag>* el_bag = bag.getProperty<PropertyBag>("WeightedSampleValue");
231 
232  if(el_bag==NULL){
233  // Works for properties in WeightedSample
234  PropertyBase* element = bag.getItem( 0 );
235  //log(Debug)<<element->getName()<<", "<< element->getDescription()<<endlog();
236  Property<T> my_property_t (element->getName(),element->getDescription());
237  if(my_property_t.getType()!=element->getType())
238  {
239  log(Error)<< "Type of "<< element->getName() << " does not match type of WeightedSample"<< "OR "<<"Could not read WeightedSample Value "<<endlog();
240  return false;
241  }
242  else{
243  my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
244  weightedSample.ValueSet( my_property_t.get());
245  }
246  }
247  else{
248  // Works for propertybags in WeightedSample
249  const std::string el_bagType = el_bag->getType();
250  Property<T > el_p(el_bag->getName(),el_bag->getDescription());
251  if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
252  log(Error)<<"Could not compose WeightedSampleValue "<<endlog();
253  return false;
254  }
255  if(el_p.ready()){
256  weightedSample.ValueSet( el_p.get());
257  }else{
258  log(Error)<<"Property of WeightedSampleValue was not ready for use"<<endlog();
259  return false;
260  }
261  }
262  // Get weight of sample
263  Property<double>* weightProp = bag.getProperty<double>("WeightedSampleWeight");
264 
265  if(!weightProp)
266  {
267  log(Error)<< "Error reading weight of WeightedSample"<<endlog();
268  return false;
269  }
270  else{
271  weightedSample.WeightSet( weightProp->get());
272  }
273  }
274  else {
275  Logger::log() << Logger::Error << "Composing Property< WeightedSample<T> > :"
276  << " type mismatch, got type '"<< bag.getType()
277  << "', expected type "<<tname<<"."<<Logger::endl;
278  return false;
279  }
280  return true;
281  };
282 
283  template <typename T>
285  : public TemplateTypeInfo<WeightedSample<T>, true>
286  {
287  WeightedSampleTypeInfo<T>(std::string name)
288  : TemplateTypeInfo<WeightedSample<T>, true >(name)
289  {
290  };
291 
292  bool decomposeTypeImpl(const WeightedSample<T>& weightedSample, PropertyBag& targetbag) const
293  {
294  decomposeProperty<T>( weightedSample, targetbag );
295  return true;
296  };
297 
298  bool composeTypeImpl(const PropertyBag& bag, WeightedSample<T>& result) const
299  {
300  return composeProperty<T>( bag, result );
301  }
302 
303  };
304 
305  template<typename T>
307  : public std::binary_function<T ,double , const WeightedSample<T>&>
308  {
309  typedef const WeightedSample<T>& (Signature)( T, double );
312  : ptr( new WeightedSample<T>() ) {}
313  const WeightedSample<T>& operator()( T value , double weight) const
314  {
315  ptr->ValueSet( value );
316  ptr->WeightSet( weight );
317  return *(ptr);
318  }
319  };
320 
325 /*
326  template<typename T>
327  struct Sample_varargs_ctor
328  {
329  typedef const Sample<T>& result_type;
330  typedef T argument_type;
331  result_type operator()( const Sample<T>& args ) const
332  {
333  return args;
334  }
335  };
336 */
337 
338 
339 };
340 #endif
341 
boost::shared_ptr< WeightedSample< T > > ptr
bool decomposeTypeImpl(const Sample< T > &sample, PropertyBag &targetbag) const
void WeightSet(double weight)
Set the weight.
const WeightedSample< T > &( Signature)(T, double)
void decomposeProperty(const Sample< T > &sample, PropertyBag &targetbag)
const Sample< T > & operator()(T value) const
void ValueSet(const T &value)
Set the value of the Sample.
void ValueSet(const T &value)
Set the value of the Sample.
Definition: asirfilter.h:125
bool composeTypeImpl(const PropertyBag &bag, WeightedSample< T > &result) const
boost::shared_ptr< Sample< T > > ptr
const Sample< T > &( Signature)(T)
bool decomposeTypeImpl(const WeightedSample< T > &weightedSample, PropertyBag &targetbag) const
const WeightedSample< T > & operator()(T value, double weight) const
T & ValueGet()
Get the value of the Sample.
bool composeTypeImpl(const PropertyBag &bag, Sample< T > &result) const
double WeightGet() const
Get the weight.
T & ValueGet()
Get the value of the Sample.
Definition: asirfilter.h:132
bool composeProperty(const PropertyBag &bag, Sample< T > &sample)


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 Mon Jun 10 2019 12:47:59