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
virtual result_t get() const =0
virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
const std::string & getType() const
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) const
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
bool refreshProperties(const PropertyBag &target, const PropertyBag &source, bool allprops)
virtual void set(param_t t)=0
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
void setCompositionFactory(CompositionFactoryPtr cf)
Definition: TypeInfo.hpp:469
A container for holding references to properties.
Definition: PropertyBag.hpp:96
bool installTypeInfoObject(TypeInfo *ti)
virtual const_reference_t rvalue() const =0
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:461
bool installTypeInfoObject(TypeInfo *ti)
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
bool typeDecomposition(base::DataSourceBase::shared_ptr dsb, PropertyBag &targetbag, bool recurse)
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
static DataSource< T > * narrow(base::DataSourceBase *db)
virtual bool composeType(base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const
virtual std::vector< std::string > getMemberNames() const
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
size_t size() const
boost::shared_ptr< TypeInfoRepository > shared_ptr
static Logger & log()
Definition: Logger.hpp:350
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
boost::shared_ptr< PrimitiveTypeInfo< T, use_ostream > > getSharedPtr()


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:30