Orocos Real-Time Toolkit  2.8.3
CArrayTypeInfo.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 CArrayTypeInfo.hpp
3 
4  CArrayTypeInfo.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_TEMPLATE_CARRAY_INFO_HPP
40 #define ORO_TEMPLATE_CARRAY_INFO_HPP
41 
42 #include "PrimitiveTypeInfo.hpp"
43 #include "../internal/ArrayPartDataSource.hpp"
44 #include <boost/lexical_cast.hpp>
45 #include "carray.hpp"
46 #include "../internal/carray.hpp"
47 #include "PropertyComposition.hpp"
49 
50 namespace RTT
51 {
52  namespace types
53  {
62  template<typename T, bool has_ostream = false>
64  public PrimitiveTypeInfo<T, has_ostream>,
65  public MemberFactory, public CompositionFactory
66  {
67  public:
68  CArrayTypeInfo(std::string name) :
69  PrimitiveTypeInfo<T, has_ostream> (name)
70  {
71  }
72 
74  // aquire a shared reference to the this object
75  boost::shared_ptr< CArrayTypeInfo<T> > mthis = boost::dynamic_pointer_cast<CArrayTypeInfo<T> >( this->getSharedPtr() );
76  // Allow base to install first
78  // Install the factories for primitive types
79  ti->setMemberFactory( mthis );
80  ti->setCompositionFactory( mthis );
81 
82  // Don't delete us, we're memory-managed.
83  return false;
84  }
85 
87  virtual base::AttributeBase* buildVariable(std::string name,int sizehint) const
88  {
89  // There were two choices: create an empty carray, ie pointer-like behavior; OR create one with storage in the DS.
90  // We need to redefine assignment in case of the latter, and make the storage dynamic, depending on sizehint.
91  // pointer-like is dangerous due to non-tracking of reference-counts, so this is left for the default buildVariable
92  // without a sizehint (using ValueDataSource), while the size hint version has storage.
94  ads->newArray( sizehint );
95  return new Attribute<T>( name, ads.get() );
96  }
97 
98  /* buildConstant() with sizehint is left out since it is identical to buildConstant() without sizehint.
99  We make a shallow copy, so the size is automatically taken from the original expression the constant
100  refers to. */
101 
102  virtual std::vector<std::string> getMemberNames() const {
103  // only discover the parts of this struct:
104  std::vector<std::string> result;
105  result.push_back("size");
106  result.push_back("capacity");
107  return result;
108  }
109 
110  virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
111  using namespace internal;
112  typename DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< DataSource<T> >( item );
113 
114  // size and capacity can not change during program execution:
115  if (name == "size" || name == "capacity") {
116  return new ConstantDataSource<int>( data->rvalue().count() );
117  }
118 
119  typename AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item );
120  if ( !adata ) {
122  }
123 
124  // contents of indx can change during program execution:
125  try {
126  unsigned int indx = boost::lexical_cast<unsigned int>(name);
127  // @todo could also return a direct reference to item indx using another DS type that respects updated().
128  return new ArrayPartDataSource<typename T::value_type>( *adata->set().address(), new ConstantDataSource<unsigned int>(indx), item, data->rvalue().count() );
129  } catch(...) {}
130  log(Error) << "CArrayTypeInfo: No such part (or invalid index): " << name << endlog();
132  }
133 
136  using namespace internal;
137  typename DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< DataSource<T> >( item );
138  if ( !data ) {
140  }
141 
142  // discover if user gave us a part name or index:
144  if ( id_name ) {
145  // size and capacity can not change during program execution:
146  if (id_name->get() == "size" || id_name->get() == "capacity") {
147  return new ConstantDataSource<int>( data->rvalue().count() );
148  } else {
149  log(Error) << "CArrayTypeInfo: No such part : " << id_name->get() << endlog();
151  }
152  }
153 
154  typename AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item );
155  if ( !adata ) {
156  log(Error) << "CArrayTypeInfo: need assignable data type for indexing " << this->getTypeName() << endlog();
158  }
159 
161  if ( id_indx ) {
162  return new ArrayPartDataSource<typename T::value_type>( *adata->set().address(), id_indx, item, data->rvalue().count() );
163  }
164  log(Error) << "CArrayTypeInfo: Invalid index) for type " << this->getTypeName() << endlog();
166  }
167 
172  {
174  }
175 
177  const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() );
178  if ( !pb )
179  return false;
180  typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult );
181  if ( !ads )
182  return false;
183 
184  PropertyBag const& source = pb->rvalue();
185  typename internal::AssignableDataSource<T>::reference_t result = ads->set();
186 
187  //result.resize( source.size() );
188  if(result.count() != source.size()) {
189  log(Error) << "Refusing to compose C Arrays from a property list of different size. Use the same number of properties as the C array size." << endlog();
190  return false;
191  }
192  // recurse into items of this sequence:
194  PropertyBag target( source.getType() );
195  PropertyBag decomp;
197  rds.ref(); // prevent dealloc.
198  // we compose each item in this sequence and then update result with target's result.
199  // 1. each child is composed into target (this is a recursive thing)
200  // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1.
201  if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( tir->type( decomp.getType() ) == tir->type( target.getType() ) ) && refreshProperties(decomp, target, true) ) {
202  assert(result.count() == source.size());
203  assert(source.size() == target.size());
204  assert(source.size() == decomp.size());
205  return true;
206  }
207  return false;
208  }
209 
210  };
211  }
212 }
213 
214 #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.
virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
Use getMember() for decomposition...
virtual std::vector< std::string > getMemberNames() const
Returns the list of struct member names of this type.
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
virtual base::AttributeBase * buildVariable(std::string name, int sizehint) const
Build a modifyable instance of this type.
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
const std::string & getType() const
void newArray(std::size_t size)
Clears the array of this data source and creates a new one.
boost::intrusive_ptr< ArrayDataSource< T > > shared_ptr
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) const
Returns a member of a given data source struct identified by its name.
virtual void set(param_t t)=0
Set this DataSource with a value.
Template for data types that are C-style arrays.
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
Uses the type composition to compose all typed properties from a property bag.
void setCompositionFactory(CompositionFactoryPtr cf)
Definition: TypeInfo.hpp:467
A DataSource which is used to manipulate a reference to an external value.
A container for holding references to properties.
Definition: PropertyBag.hpp:96
A factory for composing/decomposing and converting types to a form suitable for persistent storage...
DataSource< T >::result_t get() const
Return the data as type T.
A DataSource which is used to manipulate a reference to a part of a data source holding a C-style arr...
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
Returns a member of a given data source identified by a data source id.
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
An attribute is a minimalistic, named placeholder for data.
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
virtual const std::string & getTypeName() const
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:66
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:459
TypeInfoRepository::shared_ptr Types()
Obtain a pointer to the global type system.
Definition: Types.cpp:48
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
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...
A DataSource which holds a constant value and returns it in its get() method.
A DataSource which has set() methods.
Definition: DataSource.hpp:184
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 typeDecomposition(base::DataSourceBase::shared_ptr dsb, PropertyBag &targetbag, bool recurse)
Identical to RTT::types::propertyDecomposition(), but takes a DataSourceBase as source.
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
size_t size() const
Get the number of Properties in this bag.
void ref() const
Increase the reference count by one.
Definition: DataSource.cpp:80
bool refreshProperties(const PropertyBag &target, const PropertyBag &source, bool allprops)
This function refreshes the values of the properties in one PropertyBag with the values of the proper...
boost::shared_ptr< TypeInfoRepository > shared_ptr
CArrayTypeInfo(std::string name)
virtual bool composeType(base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const
Compose a type (target) from a DataSourceBase (source) containing its members.
This template class allows primitive types, which are not sent over ports, to be added to Orocos...
boost::shared_ptr< PrimitiveTypeInfo< T, use_ostream > > getSharedPtr()