Orocos Real-Time Toolkit  2.8.3
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
DataSource is a base class representing a generic way to read data of type T.
Definition: DataSource.hpp:94
virtual result_t get() const =0
Return the data as type T.
void addConstructor(TypeConstructor *tb)
Add a constructor/convertor object.
Definition: TypeInfo.cpp:114
Sequence constructor which takes the number of elements in the sequence.
A function object which returns a vector of DataSources.
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
Constructs a sequence from the number of elements and a prototype element for these elements...
std::vector< std::string > getMemberNames() const
virtual void set(param_t t)=0
Set this DataSource with a value.
int get_capacity(T const &cont)
Returns the capacity of an STL container which has the capacity() member function.
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
Uses the type composition to compose all typed properties from a property bag.
base::DataSourceBase * newFunctorDataSource(Function f, const std::vector< base::DataSourceBase::shared_ptr > &args)
Creates a data source that returns the result of a given function.
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
An attribute is a minimalistic, named placeholder for data.
base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
Use getMember() for decomposition...
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
Create a new Constructor.
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:66
base::AttributeBase * buildVariable(std::string name, int size) const
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
bool composeType(base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const
Specialize to resize result given the size of source.
Every DataSource of type T has a type info class which it can ask type information.
A special DataSource only to be used for if you understand the copy()/clone() semantics very well...
Template for data types that are C++ STL Sequences with operator[], size() and capacity() methods...
A DataSource which holds a constant value and returns it in its get() method.
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
This method narrows a base::DataSourceBase to a typeded AssignableDataSource, possibly returning a ne...
Definition: DataSource.inl:72
A DataSource which has set() methods.
Definition: DataSource.hpp:184
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
static DataSource< T > * narrow(base::DataSourceBase *db)
This method narrows a base::DataSourceBase to a typeded DataSource, possibly returning a new object...
Definition: DataSource.inl:66
bool get_container_item(std::vector< bool > &cont, int index)
Specialisation for vector<bool>, we don&#39;t return references to bits aka std::_Bit_reference.
boost::intrusive_ptr< DataSourceBase > shared_ptr
Use this type to store a pointer to a DataSourceBase.
An Attribute has a name and contains data which can be set and get.
Definition: Attribute.hpp:56
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
int get_size(T const &cont)
Returns the size of an STL container which has the size() member function.
virtual void updated()
In case the internal::DataSource returns a &#39;reference&#39; type, call this method to notify it that the d...
Definition: DataSource.cpp:112
virtual std::string getTypeName() const
Return the Orocos type name, without const, pointer or reference qualifiers.
Definition: DataSource.inl:26
Constructs an sequence with n elements, which are given upon construction time.
bool composeTemplateProperty(const PropertyBag &bag, T &result)