MatrixTypeInfo.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 MatrixTypeInfo.hpp
3 
4  MatrixTypeInfo.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
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 #ifndef ORO_MATRIX_TYPE_INFO_HPP
40 #define ORO_MATRIX_TYPE_INFO_HPP
41 
43 #include "types/MemberFactory.hpp"
44 #include "types/type_discovery.hpp"
46 #include <boost/lexical_cast.hpp>
47 
48 namespace RTT
49 {
50  namespace types
51  {
57  template<class T>
58  int get_rows(T const& cont)
59  {
60  return cont.rows();
61  }
62 
68  template<class T>
69  int get_columns(T const& cont)
70  {
71  return cont.columns();
72  }
73 
81  template<class T>
82  typename T::reference get_container_item(T & cont, int index_row, int index_column)
83  {
84  if (index_row >= (int) (cont.rows()) || index_row < 0 || index_column >= (int) (cont.columns()) || index_column < 0)
86  return cont[index_row,index_column];
87  }
88 
89 
99  template<typename T, bool has_ostream = false>
100  class MatrixTypeInfo: public TemplateTypeInfo<T, has_ostream>, public MemberFactory
101  {
102  public:
103  MatrixTypeInfo(std::string name) :
104  TemplateTypeInfo<T, has_ostream> (name)
105  {
106  }
107 
109  // aquire a shared reference to the this object
110  boost::shared_ptr< MatrixTypeInfo<T> > mthis = boost::dynamic_pointer_cast<MatrixTypeInfo<T> >( this->getSharedPtr() );
111  // Allow base to install first
113  // Install the factories for primitive types
114  ti->setMemberFactory( mthis );
115 
116  // Don't delete us, we're memory-managed.
117  return false;
118  }
119 
120  base::AttributeBase* buildVariable(std::string name,int size) const
121  {
122  // if a sizehint is given
123  T t_init(size_rows, size_columns, typename T::value_type() );
124 
125  log(Debug) << "Building variable '"<<name <<"' of type " << this->getTypeName() <<" and number of rows "<< size << " and number of columns " << size <<Logger::endl;
127  }
128 
129  bool resize(base::DataSourceBase::shared_ptr arg, int size_rows, int size_columns) const
130  {
131  if (arg->isAssignable()) {
133  asarg->set().resize( size_rows, size_columns );
134  asarg->updated();
135  return true;
136  }
137  return false;
138  }
139 
143  virtual bool composeTypeImpl(const PropertyBag& bag, typename internal::AssignableDataSource<T>::reference_t result) const
144  {
145  if ( bag.getType() == "Matrix" ) {
146  unsigned int rows = bag.size();
147  unsigned int cols = 0;
148  // Get values
149  for (unsigned int i = 1; i <= rows ; i++) {
150  std::stringstream out;
151  out << i;
152  Property<PropertyBag>* row_bag = bag.getProperty<PropertyBag>(out.str());
153  if(row_bag==NULL){
154  log(Error)<<"Could not read row "<<i<<endlog();
155  return false;
156  }
157  Property<RowVector> row_p(row_bag->getName(),row_bag->getDescription());
158  if(!(row_p.getDataSource()->composeType(row_bag->getDataSource()))){
159  log(Error)<<"Could not decompose row "<<i<<endlog();
160  return false;
161  }
162  if(row_p.ready()){
163  if(i==1){
164  cols = row_p.get().size();
165  result.resize(rows,cols);
166  } else
167  if(row_p.get().size()!=cols){
168  log(Error)<<"Row "<<i+1<<" size does not match matrix columns"<<endlog();
169  return false;
170  }
171  for ( unsigned int j=1; j <= row_p.get().size() ; j++){
172  result(i,j)=row_p.get()(j);
173  }
174  }else{
175  log(Error)<<"Property of Row "<<i<<"was not ready for use"<<endlog();
176  return false;
177  }
178  }
179  }else {
180  log(Error) << "Composing Property< Matrix > :"
181  << " type mismatch, got type '"<< bag.getType()
182  << "', expected type "<<"Matrix."<<endlog();
183  return false;
184  }
185  return true;
186  }
187 /*
188  // take into account sequences:
189  base::PropertyBase* sz = source.find("Rows");
190  if (!sz)
191  sz = source.find("rows");
192  if (sz)
193  {
194  internal::DataSource<int>::shared_ptr sz_ds = internal::DataSource<int>::narrow(sz->getDataSource().get());
195  if (sz_ds)
196  result.resize( sz_ds->get(), sz_ds->get() );
197  }
198  else
199  {
200  // no size found, inform parent of number of elements to come:
201  result.resize( source.size() );
202  }
203  return TemplateTypeInfo<T,has_ostream>::composeTypeImpl(source,result);
204  }
205 */
206 
207 /*
208  virtual std::vector<std::string> getMemberNames() const {
209  // only discover the parts of this struct:
210  std::vector<std::string> result;
211  result.push_back("size");
212  result.push_back("capacity");
213  return result;
214  }
215 */
216 
217  virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
218  // the only thing we do is to check for an integer in name, otherwise, assume a part (size/capacity) is accessed:
219  try {
220  unsigned int indx = boost::lexical_cast<unsigned int>(name);
221  // @todo could also return a direct reference to item indx using another DS type that respects updated().
222  return getMember( item, new internal::ConstantDataSource<int>(indx));
223  } catch(...) {}
224 
225  return getMember( item, new internal::ConstantDataSource<std::string>(name) );
226  }
227 
230  typename internal::AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( item );
231  if ( !data ) {
232  if ( !item->isAssignable() )
233  log(Error) << "Can't return reference to members of type "<< this->getTypeName() <<" since given object is not assignable." <<endlog();
234  else
235  log(Error) << "Consistency error: TypeInfo of type "<< this->getTypeName() <<" can't handle types of type "<< item->getType() <<endlog();
237  }
238 
239  // discover if user gave us a part name or index:
242  if ( id_name ) {
243  if ( id_name->get() == "size" ) {
244  try {
246  } catch(...) {}
247  }
248  if ( id_name->get() == "capacity" ) {
249  try {
251  } catch(...) {}
252  }
253  }
254 
255  if ( id_indx ) {
256  try {
257  return internal::newFunctorDataSource(&get_container_item<T>, internal::GenerateDataSource()(item.get(), id_indx.get() ) );
258  } catch(...) {}
259  }
260  if (id_name) {
261  log(Error) << "MatrixTypeInfo: No such part : " << id_name->get() << endlog();
262  }
263  if (id_indx) {
264  log(Error) << "MatrixTypeInfo: Invalid index : " << id_indx->get() <<":"<< id_indx->getTypeName() << endlog();
265  }
267  }
268  };
269  }
270 }
271 
272 #endif
virtual base::DataSourceBase::shared_ptr getDataSource() const
Definition: Property.hpp:393
virtual result_t get() const =0
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
static T na()
Definition: NA.hpp:57
const std::string & getType() const
bool resize(base::DataSourceBase::shared_ptr arg, int size_rows, int size_columns) const
virtual void set(param_t t)=0
base::DataSourceBase * newFunctorDataSource(Function f, const std::vector< base::DataSourceBase::shared_ptr > &args)
A container for holding references to properties.
Definition: PropertyBag.hpp:96
int get_columns(T const &cont)
virtual bool composeTypeImpl(const PropertyBag &bag, typename internal::AssignableDataSource< T >::reference_t result) const
MatrixTypeInfo(std::string name)
virtual const std::string & getTypeName() const
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
const std::string & getDescription() const
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:461
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
base::PropertyBase * getProperty(const std::string &name) const
int get_rows(T const &cont)
static DataSource< T > * narrow(base::DataSourceBase *db)
bool installTypeInfoObject(TypeInfo *ti)
virtual std::string getTypeName() const
const std::string & getName() const
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
base::AttributeBase * buildVariable(std::string name, int size) const
bool get_container_item(std::vector< bool > &cont, int index)
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
size_t size() const
static Logger & log()
Definition: Logger.hpp:350
bool installTypeInfoObject(TypeInfo *ti)
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
boost::shared_ptr< PrimitiveTypeInfo< T, use_ostream > > getSharedPtr()
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) const


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:25