MultiVectorComposition.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Sat May 21 20:15:51 CEST 2005 MultiVectorComposition.hpp
3 
4  MultiVectorComposition.hpp - description
5  -------------------
6  begin : Sat May 21 2005
7  copyright : (C) 2005 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 #ifndef MULTIVECTOR_COMPOSITION_HPP
41 #define MULTIVECTOR_COMPOSITION_HPP
42 
43 #include "MultiVector.hpp"
44 #include "../Property.hpp"
45 #include "../PropertyBag.hpp"
46 #include "../Logger.hpp"
47 
48 namespace RTT
49 { namespace extras {
50 
55  template<class T, int S>
57  {
58  Property<PropertyBag> result(c.getName(),c.getDescription(), PropertyBag("MultiVector") );
59 
60  MultiVector<S,T> vec = c;
61  Property<int>* dimension = new Property<int>("Size","Size of the MultiVector", vec.size() );
62 
63  result.value().add( dimension );
64 
65  std::stringstream data_name;
66 
67  for ( int i=0; i < dimension->get() ; i++)
68  {
69  data_name << i;
70  result.value().add( new Property<T>(data_name.str(),"",vec[i]) ); // Put variables in the bag
71  data_name.str("");
72  }
73 
74  pi->introspect(result); // introspect the bag.
75  deleteProperties( result.value() );
76 
77  }
78 
79  template<class T, int S>
81  {
82  Property<PropertyBag> result(c.getName(),c.getDescription(), PropertyBag("MultiVector") );
83 
84  MultiVector<S,T> vec = c;
85  Property<int>* dimension = new Property<int>("Size","Size of the MultiVector", vec.size() );
86 
87  result.value().add( dimension );
88 
89  std::stringstream data_name;
90 
91  for ( int i=0; i < dimension->get() ; i++)
92  {
93  data_name << i;
94  result.value().add( new Property<T>(data_name.str(),"",vec[i]) ); // Put variables in the bag
95  data_name.str("");
96  }
97 
98  pi->introspect(result); // introspect the bag.
99  deleteProperties( result.value() );
100 
101  }
102 
106  template<class T, int S>
108  {
109  base::PropertyBase* v_base = bag.find( result.getName() );
110  if ( v_base == 0 )
111  return false;
112 
113  Property<PropertyBag>* v_bag = dynamic_cast< Property<PropertyBag>* >( v_base );
114 
115  TypeInfoRepository* tir = Types();
116  if (v_bag != 0 && tir->type(v_bag->get().getType()) == tir->getTypeInfo<MultiVector<S,T> >() )
117  {
118  Property<T>* comp;
119 
120  Property<int>* dim;
121  v_base = v_bag->get().find("Size");
122  if ( v_base == 0 ) {
123  Logger::log() << Logger::Error << "In PropertyBag for Property< MultiVector<S,T> > :"
124  << result.getName() << " : could not find property \"Size\"."<<Logger::endl;
125  return false;
126  }
127  dim = dynamic_cast< Property<int>* >(v_base);
128  if ( dim == 0) {
129  Logger::log() << Logger::Error << "In PropertyBag for Property< MultiVector<S,T> > :"
130  << result.getName() << " : Expected \"Size\" to be of type short."<<Logger::endl;
131  return false;
132  }
133  int dimension = dim->get();
134 
135  std::stringstream data_name;
136 
137  // Get values
138  for (int i = 0; i < dimension ; i++)
139  {
140  data_name << i;
141  base::PropertyBase* element = v_bag->get().find( data_name.str() );
142  if ( element == 0 ) {
143  Logger::log() << Logger::Error << "Aborting composition of Property< MultiVector<S,T> > "<<result.getName()
144  << ": Data element "<< data_name.str() <<" not found !"
145  <<Logger::endl;
146  return false;
147  }
148  comp = dynamic_cast< Property<T>* >( element );
149  if ( comp == 0 ) {
151  Logger::log() << Logger::Error << "Aborting composition of Property< MultiVector<S,T> > "<<result.getName()
152  << ": Exptected data element "<< data_name.str() << " to be of type "<<internal::DataSource<T>::GetType()
153  <<" got type " << element->getType()
154  <<Logger::endl;
155  return false;
156  }
157  result.value()[i] = comp->get();
158 
159  data_name.str("");
160  }
161  }
162  else
163  {
164  if ( v_bag != 0 ) {
165  Logger::log() << Logger::Error << "Composing Property< MultiVector<S,T> > :"
166  << result.getName() << " : type mismatch, got type '"<< v_bag->get().getType() <<"'"<<Logger::endl;
167  } else {
168  Logger::log() << Logger::Error << "Composing Property< MultiVector<S,T> > :"
169  << result.getName() << " : not a PropertyBag."<<Logger::endl;
170  }
171  // cerr << "\033[1;33mWarning: Bag was empty! \033[0m" << endl;
172  Logger::log() << Logger::Debug << "Could not update Property< MultiVector<S,T> > : "<<result.getName()<<Logger::endl;
173  return false;
174  }
175  return true;
176 
177  }
178 
179 }}; // namespace RTT
180 
181 
182 #endif
DataSourceType get() const
Definition: Property.hpp:246
void decomposeProperty(base::PropertyIntrospection *pi, const Property< MultiVector< S, T > > &c)
virtual void introspect(PropertyBase *p)
Definition: Property.cpp:58
A container for holding references to properties.
Definition: PropertyBag.hpp:96
bool composeProperty(const PropertyBag &bag, Property< MultiVector< S, T > > &result)
reference_t value()
Definition: Property.hpp:277
base::PropertyBase * find(const std::string &name) const
static std::string GetType()
A property represents a named value of any type with a description.
Definition: Property.hpp:76
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
virtual std::string getType() const =0
virtual const types::TypeInfo * getTypeInfo() const
Definition: Property.hpp:421
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
void deleteProperties(PropertyBag &target)
static Logger & log()
Definition: Logger.cpp:117
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
A static allocated Vector.
Definition: MultiVector.hpp:72
virtual DataSourceBase::shared_ptr getDataSource() const =0


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:34