SequenceTypeInfoBase.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 SequenceTypeInfo.hpp
3 
4  SequenceTypeInfo.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_SEQUENCE_TYPE_INFO_BASE_HPP
40 #define ORO_SEQUENCE_TYPE_INFO_BASE_HPP
41 
42 #include "SequenceConstructor.hpp"
43 #include "TemplateConstructor.hpp"
44 #include "PropertyComposition.hpp"
47 #include "../internal/FusedFunctorDataSource.hpp"
48 #include "../internal/DataSourceGenerator.hpp"
49 #include <boost/lexical_cast.hpp>
50 
51 namespace RTT
52 {
53  namespace types
54  {
60  template<class T>
61  int get_capacity(T const& cont)
62  {
63  return cont.capacity();
64  }
65 
71  template<class T>
72  int get_size(T const& cont)
73  {
74  return cont.size();
75  }
76 
84  template<class T>
85  typename T::reference get_container_item(T & cont, int index)
86  {
87  if (index >= (int) (cont.size()) || index < 0)
89  return cont[index];
90  }
91 
99  template<class T>
100  typename T::value_type get_container_item_copy(const T & cont, int index)
101  {
102  if (index >= (int) (cont.size()) || index < 0)
104  return cont[index];
105  }
106 
115  bool get_container_item(std::vector<bool> & cont, int index);
116  bool get_container_item_copy(const std::vector<bool> & cont, int index);
117 
125  template<typename T>
127  {
128  public:
130  {
131  }
132 
134  ti->addConstructor( new SequenceBuilder<T>() );
137  // Don't delete us, we're memory-managed.
138  return false;
139  }
140 
142 
143  base::AttributeBase* buildVariable(std::string name,int size) const
144  {
145  // if a sizehint is given
146  T t_init(size, typename T::value_type() );
147 
149  }
150 
151  bool resize(base::DataSourceBase::shared_ptr arg, int size) const
152  {
153  if (arg->isAssignable()) {
155  asarg->set().resize( size );
156  asarg->updated();
157  return true;
158  }
159  return false;
160  }
161 
166  const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() );
167  if ( !pb )
168  return false;
169  typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult );
170  if ( !ads )
171  return false;
172 
173  PropertyBag const& source = pb->rvalue();
174  typename internal::AssignableDataSource<T>::reference_t result = ads->set();
175 
176  // recurse into items of this sequence:
177  PropertyBag target( source.getType() );
178  // we compose each item in this sequence and then update result with target's result.
179  // 1. each child is composed into target (this is a recursive thing using composeType() on each child)
180  // 2. we decompose result one-level deep and
181  // 3. 'refresh' it with the composed children of step 1.
182  if ( composePropertyBag(source, target) && composeTemplateProperty(target, result ) ){
183  ads->updated();
184  Logger::log() <<Logger::Debug<<"Successfuly composed Sequence from "<< source.getType() <<Logger::endl;
185  return true;
186  } else
187  Logger::log() <<Logger::Debug<<"Failed to composed Sequence from "<< source.getType() <<Logger::endl;
188 
189  return false;
190  }
191 
196  {
198  }
199 
200 
201  std::vector<std::string> getMemberNames() const {
202  // only discover the parts of this struct:
203  std::vector<std::string> result;
204  result.push_back("size");
205  result.push_back("capacity");
206  return result;
207  }
208 
210  // the only thing we do is to check for an integer in name, otherwise, assume a part (size/capacity) is accessed:
211  try {
212  unsigned int indx = boost::lexical_cast<unsigned int>(name);
213  // @todo could also return a direct reference to item indx using another DS type that respects updated().
214  return getMember( item, new internal::ConstantDataSource<int>(indx));
215  } catch(...) {}
216 
217  return getMember( item, new internal::ConstantDataSource<std::string>(name) );
218  }
219 
222  // discover if user gave us a part name or index:
225  if ( id_name ) {
226  if ( id_name->get() == "size" ) {
227  try {
229  } catch(...) {}
230  }
231  if ( id_name->get() == "capacity" ) {
232  try {
234  } catch(...) {}
235  }
236  }
237 
238  if ( id_indx ) {
239  try {
240  if ( item->isAssignable() )
241  return internal::newFunctorDataSource(&get_container_item<T>, internal::GenerateDataSource()(item.get(), id_indx.get() ) );
242  else
243  return internal::newFunctorDataSource(&get_container_item_copy<T>, internal::GenerateDataSource()(item.get(), id_indx.get() ) );
244  } catch(...) {}
245  }
246  if (id_name) {
247  log(Error) << "SequenceTypeInfo: No such member : " << id_name->get() << endlog();
248  }
249  if (id_indx) {
250  log(Error) << "SequenceTypeInfo: Invalid index : " << id_indx->get() <<":"<< id_indx->getTypeName() << endlog();
251  }
252  if ( !id_name && ! id_indx)
253  log(Error) << "SequenceTypeInfo: Not a member or index : " << id <<":"<< id->getTypeName() << endlog();
255  }
256  };
257  }
258 }
259 
260 #endif
virtual result_t get() const =0
void addConstructor(TypeConstructor *tb)
Definition: TypeInfo.cpp:114
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) const
std::vector< std::string > getMemberNames() const
virtual void set(param_t t)=0
int get_capacity(T const &cont)
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
base::DataSourceBase * newFunctorDataSource(Function f, const std::vector< base::DataSourceBase::shared_ptr > &args)
A container for holding references to properties.
Definition: PropertyBag.hpp:96
bool get_container_item_copy(const std::vector< bool > &cont, int index)
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) const
base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
virtual const_reference_t rvalue() const =0
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
base::AttributeBase * buildVariable(std::string name, int size) const
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
bool composeType(base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
static DataSource< T > * narrow(base::DataSourceBase *db)
static Logger & log()
Definition: Logger.cpp:117
virtual std::string getTypeName() 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
static Logger & log()
Definition: Logger.hpp:350
int get_size(T const &cont)
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
bool composeTemplateProperty(const PropertyBag &bag, T &result)


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