Orocos Real-Time Toolkit  2.9.0
StructTypeInfo.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 StructTypeInfo.hpp
3 
4  StructTypeInfo.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_STRUCT_TYPE_INFO_HPP
40 #define ORO_STRUCT_TYPE_INFO_HPP
41 
42 #include "TemplateTypeInfo.hpp"
44 #include "type_discovery.hpp"
45 #include "MemberFactory.hpp"
46 
47 namespace RTT
48 {
49  namespace types
50  {
61  template<typename T, bool has_ostream = false>
62  class StructTypeInfo: public TemplateTypeInfo<T, has_ostream>, public MemberFactory
63  {
64  public:
65  StructTypeInfo(std::string name) :
66  TemplateTypeInfo<T, has_ostream> (name)
67  {
68  }
69 
71  // aquire a shared reference to the this object
72  boost::shared_ptr< StructTypeInfo<T,has_ostream> > mthis = boost::dynamic_pointer_cast<StructTypeInfo<T,has_ostream> >( this->getSharedPtr() );
73  assert(mthis);
74  // Allow base to install first
76  // Install the factories for primitive types
77  ti->setMemberFactory( mthis );
78 
79  // Don't delete us, we're memory-managed.
80  return false;
81  }
82 
83  virtual std::vector<std::string> getMemberNames() const {
84  // only discover the part names of this struct:
85  type_discovery in;
86  T t; // boost can't work without a value.
87  in.discover( t );
88  return in.mnames;
89  }
90 
93  // user tried to pass the member name by data source, but we can't read out this datasource after getMember() returns.
94  // ie, we could only read out id as a string and then call the getMember below.
95  // type_discovery requires the name right now and does not allow to delay the name, unless we discover the whole type,
96  // keep all datasources and then use getMember using some functor data source.... Not going to do that !
97  assert(false && "You're doing something new and exotic. Contact the Orocos-dev mailing list.");
99  }
100 
101  virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
102  typename internal::AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( item );
103  // Use a copy in case our parent is not assignable:
104  if ( !adata ) {
105  // is it non-assignable ?
106  typename internal::DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< internal::DataSource<T> >( item );
107  if ( data ) {
108  // create a copy
109  adata = new internal::ValueDataSource<T>( data->get() );
110  }
111  }
112  if (adata) {
113  type_discovery in( adata );
114  return in.discoverMember( adata->set(), name );
115  }
116  log(Error) << "Wrong call to type info function " + this->getTypeName() << "'s getMember() can not process "<< item->getTypeName() <<endlog();
118  }
119 
120  virtual bool getMember(internal::Reference* ref, base::DataSourceBase::shared_ptr item, const std::string& name) const {
121  typename internal::AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( item );
122  // Use a copy in case our parent is not assignable:
123  if ( !adata ) {
124  // is it non-assignable ?
125  typename internal::DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< internal::DataSource<T> >( item );
126  if ( data ) {
127  // create a copy -> this is the only place & case where we allocate -> how to fix ?
128  adata = new internal::ValueDataSource<T>( data->get() );
129  }
130  }
131  if (adata) {
132  type_discovery in( adata );
133  return in.referenceMember( ref, adata->set(), name );
134  }
135  log(Error) << "Wrong call to type info function " + this->getTypeName() << "'s getMember() can not process "<< item->getTypeName() <<endlog();
136  return false;
137  }
138 
139  virtual bool resize(base::DataSourceBase::shared_ptr arg, int size) const
140  {
141  return false;
142  }
143 
144 
150  virtual bool composeTypeImpl(const PropertyBag& source, typename internal::AssignableDataSource<T>::reference_t result) const {
151  // The default implementation decomposes result and refreshes it with source.
154  rds.ref(); // prevent dealloc.
155  PropertyBag decomp;
156  // only try refreshProperties if decomp's type is equal to source type.
157  // update vs refresh: since it is intentional that the decomposition leads to references to parts of result,
158  // only refreshProperties() is meaningful (ie we have a one-to-one mapping). In case of sequences, this would
159  // of course not match, so this is struct specific.
160  return typeDecomposition( &rds, decomp, false) && ( tir->type(decomp.getType()) == tir->type(source.getType()) ) && refreshProperties(decomp, source);
161  }
162 
163 
164  };
165  }
166 }
167 
168 #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.
This template class allows user types to be used in all Orocos primitives.
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
const std::string & getType() const
virtual void set(param_t t)=0
Set this DataSource with a value.
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
virtual bool resize(base::DataSourceBase::shared_ptr arg, int size) const
Tries to resize a data source in case it&#39;s a resizable sequence.
A DataSource which is used to manipulate a reference to an external value.
A container for holding references to properties.
Definition: PropertyBag.hpp:96
base::DataSourceBase::shared_ptr discoverMember(T &t, const std::string name)
This function discovers a single part of a serializable struct and returns an assignable datasource t...
PartNames mnames
The names of the parts of the parent struct.
StructTypeInfo(std::string name)
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.
This file implements a &#39;level 2&#39; type archiver of serializable objects and uses the standard BOOST se...
virtual const std::string & getTypeName() const
Return the type name for which this generator generates type info features.
virtual std::vector< std::string > getMemberNames() const
Returns the list of struct member names of this type.
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:67
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:461
virtual bool composeTypeImpl(const PropertyBag &source, typename internal::AssignableDataSource< T >::reference_t result) const
Implementation that updates result with the matching parts in source Relies on the fact that getMembe...
TypeInfoRepository::shared_ptr Types()
Obtain a pointer to the global type system.
Definition: Types.cpp:48
Type Information for data types that are structs, ie all user data types fall in this category...
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 has set() methods.
Definition: DataSource.hpp:184
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
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.
This archive is capable of decomposing objects of serialization level 1 and 2 into part data sources...
bool typeDecomposition(base::DataSourceBase::shared_ptr dsb, PropertyBag &targetbag, bool recurse)
Identical to RTT::types::propertyDecomposition(), but takes a DataSourceBase as source.
Object that may receive a reference to some data by means of a pointer or data source.
Definition: Reference.hpp:15
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:52
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...
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
boost::shared_ptr< TypeInfoRepository > shared_ptr
bool referenceMember(internal::Reference *ref, T &t, const std::string name)
This function discovers a single part of a serializable struct and sets a reference to that member of...
virtual bool getMember(internal::Reference *ref, base::DataSourceBase::shared_ptr item, const std::string &name) const
Stores a reference to a member of a struct identified by its name.
void discover(T &t)
This function discovers all parts of a serializable struct and creates a DataSource object for each m...
boost::shared_ptr< PrimitiveTypeInfo< T, use_ostream > > getSharedPtr()