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
virtual result_t get() const =0
virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
virtual std::vector< std::string > getMemberNames() const
bool installTypeInfoObject(TypeInfo *ti)
virtual base::AttributeBase * buildVariable(std::string name, int sizehint) const
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
const std::string & getType() const
void newArray(std::size_t size)
boost::intrusive_ptr< ArrayDataSource< T > > shared_ptr
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) 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
DataSource< T >::result_t get() const
virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
bool installTypeInfoObject(TypeInfo *ti)
virtual const_reference_t rvalue() const =0
virtual const std::string & getTypeName() const
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:461
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)
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
CArrayTypeInfo(std::string name)
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
virtual bool composeType(base::DataSourceBase::shared_ptr dssource, base::DataSourceBase::shared_ptr dsresult) const
boost::shared_ptr< PrimitiveTypeInfo< T, use_ostream > > getSharedPtr()


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