Orocos Real-Time Toolkit  2.8.3
BoostArrayTypeInfo.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 BoostArrayTypeInfo.hpp
3 
4  BoostArrayTypeInfo.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_BOOSTARRAY_INFO_HPP
40 #define ORO_TEMPLATE_BOOSTARRAY_INFO_HPP
41 
42 #include "PrimitiveTypeInfo.hpp"
43 #include "../internal/ArrayPartDataSource.hpp"
44 #include "type_discovery.hpp"
45 #include <boost/lexical_cast.hpp>
46 #include <boost/array.hpp>
47 #include "PropertyComposition.hpp"
49 #include "CompositionFactory.hpp"
50 #include "MemberFactory.hpp"
51 
52 namespace RTT
53 {
54  namespace types
55  {
67  template<typename T, bool has_ostream = false>
69  public PrimitiveTypeInfo<T, has_ostream>,
70  public MemberFactory, public CompositionFactory
71  {
72  public:
73  BoostArrayTypeInfo(std::string name) :
74  PrimitiveTypeInfo<T, has_ostream> (name)
75  {
76  }
77 
79  // aquire a shared reference to the this object
80  boost::shared_ptr< BoostArrayTypeInfo<T> > mthis = boost::dynamic_pointer_cast<BoostArrayTypeInfo<T> >( this->getSharedPtr() );
81  // Allow base to install first
83  // Install the factories for primitive types
84  ti->setMemberFactory( mthis );
85  ti->setCompositionFactory( mthis );
86 
87  // Don't delete us, we're memory-managed.
88  return false;
89  }
90 
91  virtual std::vector<std::string> getMemberNames() const {
92  // only discover the parts of this struct:
93  std::vector<std::string> result;
94  result.push_back("size");
95  result.push_back("capacity");
96  return result;
97  }
98 
99  virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
100  using namespace internal;
101  typename AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item );
102  if ( !data ) {
104  }
105 
106  // size and capacity can not change during program execution:
107  if (name == "size" || name == "capacity") {
108  return new ConstantDataSource<int>( T::static_size );
109  }
110 
111  // contents of indx can change during program execution:
112  try {
113  unsigned int indx = boost::lexical_cast<unsigned int>(name);
114  // @todo could also return a direct reference to item indx using another DS type that respects updated().
115  return new ArrayPartDataSource<typename T::value_type>( *data->set().c_array(), new ConstantDataSource<unsigned int>(indx), item, T::static_size);
116  } catch(...) {}
117  log(Error) << "BoostArrayTypeInfo: No such part (or invalid index): " << name << endlog();
119  }
120 
123  using namespace internal;
124  typename AssignableDataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< AssignableDataSource<T> >( item );
125  if ( !data ) {
127  }
128 
129  // discover if user gave us a part name or index:
132  if ( id_name ) {
133  // size and capacity can not change during program execution:
134  if (id_name->get() == "size" || id_name->get() == "capacity") {
135  return new ConstantDataSource<int>( T::static_size );
136  }
137  }
138 
139  if ( id_indx ) {
140  return new ArrayPartDataSource<typename T::value_type>( *data->set().c_array(), id_indx, item, T::static_size );
141  }
142  log(Error) << "BoostArrayTypeInfo: No such part (or invalid index): " << id_name->get() << id_indx->get() << endlog();
144  }
145 
150  {
152  }
153 
155  const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (dssource.get() );
156  if ( !pb )
157  return false;
158  typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( dsresult );
159  if ( !ads )
160  return false;
161 
162  PropertyBag const& source = pb->rvalue();
163  typename internal::AssignableDataSource<T>::reference_t result = ads->set();
164 
165  //result.resize( source.size() );
166  if(result.size() != source.size()) {
167  log(Error) << "Refusing to compose Boost Arrays from a property list of different size. Use the same number of properties as the C++ boost array size." << endlog();
168  return false;
169  }
170  // recurse into items of this sequence:
172  PropertyBag target( source.getType() );
173  PropertyBag decomp;
175  rds.ref(); // prevent dealloc.
176  // we compose each item in this sequence and then update result with target's result.
177  // 1. each child is composed into target (this is a recursive thing)
178  // 2. we decompose result one-level deep and 'refresh' it with the composed children of step 1.
179  if ( composePropertyBag(source, target) && typeDecomposition( &rds, decomp, false) && ( tir->type(decomp.getType()) == tir->type(target.getType()) ) && refreshProperties(decomp, target, true) ) {
180  assert(result.size() == source.size());
181  assert(source.size() == target.size());
182  assert(source.size() == decomp.size());
183  return true;
184  }
185  return false;
186  }
187 
188  };
189  }
190 }
191 
192 #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...
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
const std::string & getType() const
Template for data types that are of type boost::array<U,int>.
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 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.
virtual void set(param_t t)=0
Set this DataSource with a value.
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...
A DataSource which is used to manipulate a reference to a part of a data source holding a C-style arr...
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
This file implements a &#39;level 2&#39; type archiver of serializable objects and uses the standard BOOST se...
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
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
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
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
A DataSource which holds a constant value and returns it in its get() method.
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.
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
virtual std::vector< std::string > getMemberNames() const
Returns the list of struct member names of this type.
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.
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
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()