Orocos Real-Time Toolkit  2.8.3
TypeInfo.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:26 CET 2004 TypeInfo.hpp
3 
4  TypeInfo.hpp - description
5  -------------------
6  begin : Mon January 19 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
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 #ifndef ORO_CORELIB_TYPEINFO_HPP
39 #define ORO_CORELIB_TYPEINFO_HPP
40 
41 #include <vector>
42 #include <string>
43 #include "../base/DataSourceBase.hpp"
44 #include "../base/ChannelElementBase.hpp"
45 
46 #ifndef NO_TYPE_INFO_FACTORY_FUNCTIONS
47 #include "MemberFactory.hpp"
48 #include "ValueFactory.hpp"
49 #include "CompositionFactory.hpp"
50 #ifndef NO_TYPE_INFO_STREAMS
51 #include "StreamFactory.hpp"
52 #endif
53 #endif
54 
55 
56 namespace RTT
57 { namespace types {
58  typedef boost::shared_ptr<internal::ConnFactory> ConnFactoryPtr;
59 
67  {
68  public:
69  typedef const std::type_info * TypeId;
70 
71  TypeInfo(const std::string& name) : mtypenames(1,name) {}
72 
73  ~TypeInfo();
82  const std::string& getTypeName() const { return mtypenames[0]; }
83 
87  std::vector<std::string> getTypeNames() const;
88 
94  void addAlias(const std::string& alias);
95 
102  bool isType(const std::string& name);
103 
107  TypeId getTypeId() const { return mtid; }
108 
112  const char * getTypeIdName() const { return mtid_name; }
113 
119  base::DataSourceBase::shared_ptr construct(const std::vector<base::DataSourceBase::shared_ptr>& args) const;
120 
127 
131  void addConstructor(TypeConstructor* tb);
132 
142  bool addProtocol(int protocol_id, TypeTransporter* tt);
143 
149  TypeTransporter* getProtocol(int protocol_id) const;
150 
154  bool hasProtocol(int protocol_id) const;
155 
159  std::vector<int> getTransportNames() const;
160 
161 #ifndef NO_TYPE_INFO_FACTORY_FUNCTIONS
162 
172  base::AttributeBase* buildConstant(std::string name,base::DataSourceBase::shared_ptr source, int sizehint) const
173  {
174  return mdsf ? mdsf->buildConstant(name, source, sizehint) : 0;
175  }
176 
178  {
179  return mdsf ? mdsf->buildConstant(name, source) : 0;
180  }
186  base::AttributeBase* buildVariable(std::string name,int sizehint) const;
187  base::AttributeBase* buildVariable(std::string name) const
188  {
189  return mdsf ? mdsf->buildVariable(name) : 0;
190  }
191 
196  bool resize(base::DataSourceBase::shared_ptr arg, int size) const
197  {
198  return mmembf ? mmembf->resize(arg, size) : false;
199  }
200 
206  {
207  return mdsf ? mdsf->buildAlias(name, b) : 0;
208  }
209 
213  base::PropertyBase* buildProperty(const std::string& name, const std::string& desc, base::DataSourceBase::shared_ptr source = 0) const
214  {
215  return mdsf ? mdsf->buildProperty(name, desc, source) : 0;
216  }
217 
222  {
223  return mdsf ? mdsf->buildAttribute(name, source) : 0;
224  }
225 
230  {
231  return mdsf ? mdsf->buildValue() : base::DataSourceBase::shared_ptr();
232  }
233 
238  {
239  return mdsf ? mdsf->buildReference(ptr) : base::DataSourceBase::shared_ptr();
240  }
241 
248  {
249  return mdsf ? mdsf->buildActionAlias(action,source) : base::DataSourceBase::shared_ptr();
250  }
251 
254 #ifndef NO_TYPE_INFO_STREAMS
255 
264  std::ostream& write(std::ostream& os, base::DataSourceBase::shared_ptr in ) const
265  {
266  if ( mstrf ) {
267  return mstrf->write(os,in);
268  } else {
269 #ifdef OS_HAVE_STREAMS
270  std::string output = std::string("(")+ in->getTypeName() +")";
271  os << output;
272 #endif
273  return os;
274  }
275  }
276 
281  std::istream& read(std::istream& os, base::DataSourceBase::shared_ptr out ) const
282  {
283  return mstrf ? mstrf->read(os, out) : os;
284  }
285 
286 
291  {
292  return mstrf ? mstrf->toString(in) : std::string();
293  }
294 
298  bool fromString( const std::string& value, base::DataSourceBase::shared_ptr out ) const
299  {
300  return mstrf ? mstrf->fromString(value, out) : false;
301  }
302 
307  bool isStreamable() const
308  {
309  return mstrf ? mstrf->isStreamable() : false;
310  }
312 #endif
313 
323  std::vector<std::string> getMemberNames() const
324  {
325  return mmembf ? mmembf->getMemberNames() : std::vector<std::string>();
326  }
327 
336  {
337  return mmembf ? mmembf->getMember(item,name) : base::DataSourceBase::shared_ptr();
338  }
339 
349  bool getMember(internal::Reference* ref, base::DataSourceBase::shared_ptr item, const std::string& name) const
350  {
351  return mmembf ? mmembf->getMember(ref, item,name) : false;
352  }
353 
363  {
364  return mmembf ? mmembf->getMember(item,id) : base::DataSourceBase::shared_ptr();
365  }
366 
386  {
387  return mcompf ? mcompf->composeType(source,target) : false;
388  }
389 
402  {
403  return mcompf ? mcompf->decomposeType(source) : base::DataSourceBase::shared_ptr();
404  }
405 
414  {
415  return mcompf ? mcompf->convertType(source) : base::DataSourceBase::shared_ptr();
416  }
417 
426  base::InputPortInterface* inputPort(std::string const& name) const;
427 
432  base::OutputPortInterface* outputPort(std::string const& name) const;
433 
440  base::ChannelElementBase::shared_ptr buildDataStorage(ConnPolicy const& policy) const;
441  base::ChannelElementBase::shared_ptr buildChannelOutput(base::InputPortInterface& port) const;
443 
444 #endif // NO_TYPE_INFO_FACTORY_FUNCTIONS
445 
446  void setTypeId(TypeId tid) {
447  mtid = tid;
448  mtid_name = tid->name();
449  }
450 
455  void setPortFactory( ConnFactoryPtr cf ) { if (cf) mconnf = cf; }
456 
457  ConnFactoryPtr getPortFactory() const { return mconnf; }
458 
459  void setMemberFactory( MemberFactoryPtr mf ) { if (mf) mmembf = mf; }
460 
461  MemberFactoryPtr getMemberFactory() const { return mmembf; }
462 
463  void setValueFactory( ValueFactoryPtr dsf) { if (dsf) mdsf = dsf; }
464 
465  ValueFactoryPtr getValueFactory() const { return mdsf; }
466 
467  void setCompositionFactory( CompositionFactoryPtr cf) { if(cf) mcompf = cf; }
468 
469  CompositionFactoryPtr getCompositionFactory() const { return mcompf; }
470 
471  void setStreamFactory( StreamFactoryPtr sf ) { if (sf) mstrf = sf; }
472 
473  StreamFactoryPtr getStreamFactory() const { return mstrf; }
474  protected:
475  typedef std::vector<TypeConstructor*> Constructors;
476  typedef std::vector<TypeTransporter*> Transporters;
477  Constructors constructors;
478  Transporters transporters;
479  std::vector<std::string> mtypenames;
480  const char* mtid_name;
481  TypeId mtid;
482  boost::shared_ptr<internal::ConnFactory> mconnf;
483  boost::shared_ptr<MemberFactory> mmembf;
484  boost::shared_ptr<ValueFactory> mdsf;
485  boost::shared_ptr<CompositionFactory> mcompf;
486  boost::shared_ptr<StreamFactory> mstrf;
487  };
488 
489 }}
490 
491 #endif
The base class of the InputPort.
base::DataSourceBase::shared_ptr buildReference(void *ptr) const
Build a internal::ReferenceDataSource of this type, pointing to the given pointer.
Definition: TypeInfo.hpp:237
bool composeType(base::DataSourceBase::shared_ptr source, base::DataSourceBase::shared_ptr target) const
Compose a type (target) from a DataSourceBase (source) containing its members.
Definition: TypeInfo.hpp:385
boost::shared_ptr< MemberFactory > MemberFactoryPtr
void setStreamFactory(StreamFactoryPtr sf)
Definition: TypeInfo.hpp:471
base::AttributeBase * buildVariable(std::string name) const
Build a non modifyable instance of this type.
Definition: TypeInfo.hpp:187
base::AttributeBase * buildAttribute(std::string name, base::DataSourceBase::shared_ptr source=0) const
Build an Attribute of this type.
Definition: TypeInfo.hpp:221
This interface defines the function a transport protocol must support in order to allow Orocos compon...
const char * mtid_name
Definition: TypeInfo.hpp:480
const std::string & getTypeName() const
Return the type name which was first registered.
Definition: TypeInfo.hpp:82
boost::shared_ptr< CompositionFactory > mcompf
Definition: TypeInfo.hpp:485
TypeInfo(const std::string &name)
Definition: TypeInfo.hpp:71
bool isStreamable() const
Returns true if this type is directly streamable using read()/write() or toString()/fromString().
Definition: TypeInfo.hpp:307
void setValueFactory(ValueFactoryPtr dsf)
Definition: TypeInfo.hpp:463
Base class for all properties.
std::string toString(base::DataSourceBase::shared_ptr in) const
Usability function which converts data to a string.
Definition: TypeInfo.hpp:290
boost::shared_ptr< MemberFactory > mmembf
Definition: TypeInfo.hpp:483
bool resize(base::DataSourceBase::shared_ptr arg, int size) const
Tries to resize a data source in case it&#39;s a resizable sequence.
Definition: TypeInfo.hpp:196
const std::type_info * TypeId
Definition: TypeInfo.hpp:69
void setCompositionFactory(CompositionFactoryPtr cf)
Definition: TypeInfo.hpp:467
base::DataSourceBase::shared_ptr buildActionAlias(base::ActionInterface *action, base::DataSourceBase::shared_ptr source) const
Returns a DataSource that first executes an action and returns the result of another data source...
Definition: TypeInfo.hpp:247
#define RTT_API
Definition: rtt-config.h:97
TypeId getTypeId() const
Returns the compiler generated type id pointer.
Definition: TypeInfo.hpp:107
Transporters transporters
Definition: TypeInfo.hpp:478
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:92
The base class of each OutputPort.
CompositionFactoryPtr getCompositionFactory() const
Definition: TypeInfo.hpp:469
boost::shared_ptr< internal::ConnFactory > mconnf
Definition: TypeInfo.hpp:482
const char * getTypeIdName() const
Returns the compiler generated type name (non portable accross compilers!).
Definition: TypeInfo.hpp:112
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.
Definition: TypeInfo.hpp:349
MemberFactoryPtr getMemberFactory() const
Definition: TypeInfo.hpp:461
std::vector< std::string > getMemberNames() const
Returns the list of struct member names of this type.
Definition: TypeInfo.hpp:323
void setTypeId(TypeId tid)
Definition: TypeInfo.hpp:446
An attribute is a minimalistic, named placeholder for data.
boost::shared_ptr< StreamFactory > StreamFactoryPtr
bool fromString(const std::string &value, base::DataSourceBase::shared_ptr out) const
Usability function which converts a string to data.
Definition: TypeInfo.hpp:298
base::AttributeBase * buildConstant(std::string name, base::DataSourceBase::shared_ptr source, int sizehint) const
Build a non modifyable instance of this type.
Definition: TypeInfo.hpp:172
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.
Definition: TypeInfo.hpp:335
boost::shared_ptr< internal::ConnFactory > ConnFactoryPtr
Definition: TypeInfo.hpp:58
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
Returns a reference to a member of a given data source struct identified by its name.
Definition: TypeInfo.hpp:361
ConnFactoryPtr getPortFactory() const
Definition: TypeInfo.hpp:457
base::AttributeBase * buildConstant(std::string name, base::DataSourceBase::shared_ptr source) const
Build a non modifyable instance of this type.
Definition: TypeInfo.hpp:177
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:66
base::AttributeBase * buildAlias(std::string name, base::DataSourceBase::shared_ptr b) const
build an alias with b as the value.
Definition: TypeInfo.hpp:205
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:459
base::DataSourceBase::shared_ptr convertType(base::DataSourceBase::shared_ptr source) const
Specialize this function to return an alternate type which represents this one in a compatible way...
Definition: TypeInfo.hpp:413
base::PropertyBase * buildProperty(const std::string &name, const std::string &desc, base::DataSourceBase::shared_ptr source=0) const
Build a Property of this type.
Definition: TypeInfo.hpp:213
boost::shared_ptr< ValueFactory > ValueFactoryPtr
boost::intrusive_ptr< ChannelElementBase > shared_ptr
Constructors constructors
Definition: TypeInfo.hpp:477
boost::shared_ptr< CompositionFactory > CompositionFactoryPtr
ValueFactoryPtr getValueFactory() const
Definition: TypeInfo.hpp:465
std::vector< std::string > mtypenames
Definition: TypeInfo.hpp:479
base::DataSourceBase::shared_ptr buildValue() const
Build a internal::ValueDataSource of this type.
Definition: TypeInfo.hpp:229
This interface describes how constructors work.
boost::shared_ptr< ValueFactory > mdsf
Definition: TypeInfo.hpp:484
Object that may receive a reference to some data by means of a pointer or data source.
Definition: Reference.hpp:15
boost::shared_ptr< StreamFactory > mstrf
Definition: TypeInfo.hpp:486
void setPortFactory(ConnFactoryPtr cf)
Installs a new port factory such that in-process data can be communicated between components...
Definition: TypeInfo.hpp:455
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
StreamFactoryPtr getStreamFactory() const
Definition: TypeInfo.hpp:473
std::vector< TypeConstructor * > Constructors
Definition: TypeInfo.hpp:475
base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
Specialize this function to return an alternate type which represents this one in a compatible way...
Definition: TypeInfo.hpp:401
std::vector< TypeTransporter * > Transporters
Definition: TypeInfo.hpp:476
std::istream & read(std::istream &os, base::DataSourceBase::shared_ptr out) const
Read a new value for this datasource from a human readable string.
Definition: TypeInfo.hpp:281
std::ostream & write(std::ostream &os, base::DataSourceBase::shared_ptr in) const
Output this datasource as a human readable string.
Definition: TypeInfo.hpp:264