Orocos Real-Time Toolkit  2.9.0
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 #include "../internal/SharedConnection.hpp"
46 
47 #ifndef NO_TYPE_INFO_FACTORY_FUNCTIONS
48 #include "MemberFactory.hpp"
49 #include "ValueFactory.hpp"
50 #include "CompositionFactory.hpp"
51 #ifndef NO_TYPE_INFO_STREAMS
52 #include "StreamFactory.hpp"
53 #endif
54 #endif
55 
56 
57 namespace RTT
58 { namespace types {
59  typedef boost::shared_ptr<internal::ConnFactory> ConnFactoryPtr;
60 
68  {
69  public:
70  typedef const std::type_info * TypeId;
71 
72  TypeInfo(const std::string& name) : mtypenames(1,name) {}
73 
74  ~TypeInfo();
83  const std::string& getTypeName() const { return mtypenames[0]; }
84 
88  std::vector<std::string> getTypeNames() const;
89 
95  void addAlias(const std::string& alias);
96 
103  bool isType(const std::string& name);
104 
108  TypeId getTypeId() const { return mtid; }
109 
113  const char * getTypeIdName() const { return mtid_name; }
114 
120  base::DataSourceBase::shared_ptr construct(const std::vector<base::DataSourceBase::shared_ptr>& args) const;
121 
128 
132  void addConstructor(TypeConstructor* tb);
133 
143  bool addProtocol(int protocol_id, TypeTransporter* tt);
144 
150  TypeTransporter* getProtocol(int protocol_id) const;
151 
155  bool hasProtocol(int protocol_id) const;
156 
160  std::vector<int> getTransportNames() const;
161 
162 #ifndef NO_TYPE_INFO_FACTORY_FUNCTIONS
163 
173  base::AttributeBase* buildConstant(std::string name,base::DataSourceBase::shared_ptr source, int sizehint) const
174  {
175  return mdsf ? mdsf->buildConstant(name, source, sizehint) : 0;
176  }
177 
179  {
180  return mdsf ? mdsf->buildConstant(name, source) : 0;
181  }
187  base::AttributeBase* buildVariable(std::string name,int sizehint) const;
188  base::AttributeBase* buildVariable(std::string name) const
189  {
190  return mdsf ? mdsf->buildVariable(name) : 0;
191  }
192 
197  bool resize(base::DataSourceBase::shared_ptr arg, int size) const
198  {
199  return mmembf ? mmembf->resize(arg, size) : false;
200  }
201 
207  {
208  return mdsf ? mdsf->buildAlias(name, b) : 0;
209  }
210 
214  base::PropertyBase* buildProperty(const std::string& name, const std::string& desc, base::DataSourceBase::shared_ptr source = 0) const
215  {
216  return mdsf ? mdsf->buildProperty(name, desc, source) : 0;
217  }
218 
223  {
224  return mdsf ? mdsf->buildAttribute(name, source) : 0;
225  }
226 
231  {
232  return mdsf ? mdsf->buildValue() : base::DataSourceBase::shared_ptr();
233  }
234 
239  {
240  return mdsf ? mdsf->buildReference(ptr) : base::DataSourceBase::shared_ptr();
241  }
242 
249  {
250  return mdsf ? mdsf->buildActionAlias(action,source) : base::DataSourceBase::shared_ptr();
251  }
252 
255 #ifndef NO_TYPE_INFO_STREAMS
256 
265  std::ostream& write(std::ostream& os, base::DataSourceBase::shared_ptr in ) const
266  {
267  if ( mstrf ) {
268  return mstrf->write(os,in);
269  } else {
270 #ifdef OS_HAVE_STREAMS
271  std::string output = std::string("(")+ in->getTypeName() +")";
272  os << output;
273 #endif
274  return os;
275  }
276  }
277 
282  std::istream& read(std::istream& os, base::DataSourceBase::shared_ptr out ) const
283  {
284  return mstrf ? mstrf->read(os, out) : os;
285  }
286 
287 
292  {
293  return mstrf ? mstrf->toString(in) : std::string();
294  }
295 
299  bool fromString( const std::string& value, base::DataSourceBase::shared_ptr out ) const
300  {
301  return mstrf ? mstrf->fromString(value, out) : false;
302  }
303 
308  bool isStreamable() const
309  {
310  return mstrf ? mstrf->isStreamable() : false;
311  }
313 #endif
314 
324  std::vector<std::string> getMemberNames() const
325  {
326  return mmembf ? mmembf->getMemberNames() : std::vector<std::string>();
327  }
328 
337  {
338  return mmembf ? mmembf->getMember(item,name) : base::DataSourceBase::shared_ptr();
339  }
340 
350  bool getMember(internal::Reference* ref, base::DataSourceBase::shared_ptr item, const std::string& name) const
351  {
352  return mmembf ? mmembf->getMember(ref, item,name) : false;
353  }
354 
364  {
365  return mmembf ? mmembf->getMember(item,id) : base::DataSourceBase::shared_ptr();
366  }
367 
387  {
388  return mcompf ? mcompf->composeType(source,target) : false;
389  }
390 
403  {
404  return mcompf ? mcompf->decomposeType(source) : base::DataSourceBase::shared_ptr();
405  }
406 
415  {
416  return mcompf ? mcompf->convertType(source) : base::DataSourceBase::shared_ptr();
417  }
418 
427  base::InputPortInterface* inputPort(std::string const& name) const;
428 
433  base::OutputPortInterface* outputPort(std::string const& name) const;
434 
441  base::ChannelElementBase::shared_ptr buildDataStorage(ConnPolicy const& policy) const;
442  base::ChannelElementBase::shared_ptr buildChannelOutput(base::InputPortInterface& port, ConnPolicy const& policy) const;
443  base::ChannelElementBase::shared_ptr buildChannelInput(base::OutputPortInterface& port, ConnPolicy const& policy) const;
444  internal::SharedConnectionBase::shared_ptr buildSharedConnection(base::OutputPortInterface *output_port, base::InputPortInterface *input_port, ConnPolicy const& policy) const;
445 
446 #endif // NO_TYPE_INFO_FACTORY_FUNCTIONS
447 
448  void setTypeId(TypeId tid) {
449  mtid = tid;
450  mtid_name = tid->name();
451  }
452 
457  void setPortFactory( ConnFactoryPtr cf ) { if (cf) mconnf = cf; }
458 
459  ConnFactoryPtr getPortFactory() const { return mconnf; }
460 
461  void setMemberFactory( MemberFactoryPtr mf ) { if (mf) mmembf = mf; }
462 
463  MemberFactoryPtr getMemberFactory() const { return mmembf; }
464 
465  void setValueFactory( ValueFactoryPtr dsf) { if (dsf) mdsf = dsf; }
466 
467  ValueFactoryPtr getValueFactory() const { return mdsf; }
468 
469  void setCompositionFactory( CompositionFactoryPtr cf) { if(cf) mcompf = cf; }
470 
471  CompositionFactoryPtr getCompositionFactory() const { return mcompf; }
472 
473  void setStreamFactory( StreamFactoryPtr sf ) { if (sf) mstrf = sf; }
474 
475  StreamFactoryPtr getStreamFactory() const { return mstrf; }
476  protected:
477  typedef std::vector<TypeConstructor*> Constructors;
478  typedef std::vector<TypeTransporter*> Transporters;
479  Constructors constructors;
480  Transporters transporters;
481  std::vector<std::string> mtypenames;
482  const char* mtid_name;
483  TypeId mtid;
484  boost::shared_ptr<internal::ConnFactory> mconnf;
485  boost::shared_ptr<MemberFactory> mmembf;
486  boost::shared_ptr<ValueFactory> mdsf;
487  boost::shared_ptr<CompositionFactory> mcompf;
488  boost::shared_ptr<StreamFactory> mstrf;
489  };
490 
491 }}
492 
493 #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:238
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:386
boost::shared_ptr< MemberFactory > MemberFactoryPtr
boost::intrusive_ptr< SharedConnectionBase > shared_ptr
void setStreamFactory(StreamFactoryPtr sf)
Definition: TypeInfo.hpp:473
base::AttributeBase * buildVariable(std::string name) const
Build a non modifyable instance of this type.
Definition: TypeInfo.hpp:188
base::AttributeBase * buildAttribute(std::string name, base::DataSourceBase::shared_ptr source=0) const
Build an Attribute of this type.
Definition: TypeInfo.hpp:222
This interface defines the function a transport protocol must support in order to allow Orocos compon...
const char * mtid_name
Definition: TypeInfo.hpp:482
const std::string & getTypeName() const
Return the type name which was first registered.
Definition: TypeInfo.hpp:83
boost::shared_ptr< CompositionFactory > mcompf
Definition: TypeInfo.hpp:487
TypeInfo(const std::string &name)
Definition: TypeInfo.hpp:72
bool isStreamable() const
Returns true if this type is directly streamable using read()/write() or toString()/fromString().
Definition: TypeInfo.hpp:308
void setValueFactory(ValueFactoryPtr dsf)
Definition: TypeInfo.hpp:465
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:291
boost::shared_ptr< MemberFactory > mmembf
Definition: TypeInfo.hpp:485
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:197
const std::type_info * TypeId
Definition: TypeInfo.hpp:70
void setCompositionFactory(CompositionFactoryPtr cf)
Definition: TypeInfo.hpp:469
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:248
#define RTT_API
Definition: rtt-config.h:97
TypeId getTypeId() const
Returns the compiler generated type id pointer.
Definition: TypeInfo.hpp:108
Transporters transporters
Definition: TypeInfo.hpp:480
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
The base class of each OutputPort.
CompositionFactoryPtr getCompositionFactory() const
Definition: TypeInfo.hpp:471
boost::shared_ptr< internal::ConnFactory > mconnf
Definition: TypeInfo.hpp:484
const char * getTypeIdName() const
Returns the compiler generated type name (non portable accross compilers!).
Definition: TypeInfo.hpp:113
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:350
MemberFactoryPtr getMemberFactory() const
Definition: TypeInfo.hpp:463
std::vector< std::string > getMemberNames() const
Returns the list of struct member names of this type.
Definition: TypeInfo.hpp:324
void setTypeId(TypeId tid)
Definition: TypeInfo.hpp:448
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:299
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:173
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:336
boost::shared_ptr< internal::ConnFactory > ConnFactoryPtr
Definition: TypeInfo.hpp:59
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:362
ConnFactoryPtr getPortFactory() const
Definition: TypeInfo.hpp:459
base::AttributeBase * buildConstant(std::string name, base::DataSourceBase::shared_ptr source) const
Build a non modifyable instance of this type.
Definition: TypeInfo.hpp:178
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:67
base::AttributeBase * buildAlias(std::string name, base::DataSourceBase::shared_ptr b) const
build an alias with b as the value.
Definition: TypeInfo.hpp:206
void setMemberFactory(MemberFactoryPtr mf)
Definition: TypeInfo.hpp:461
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:414
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:214
boost::shared_ptr< ValueFactory > ValueFactoryPtr
boost::intrusive_ptr< ChannelElementBase > shared_ptr
Constructors constructors
Definition: TypeInfo.hpp:479
boost::shared_ptr< CompositionFactory > CompositionFactoryPtr
ValueFactoryPtr getValueFactory() const
Definition: TypeInfo.hpp:467
std::vector< std::string > mtypenames
Definition: TypeInfo.hpp:481
base::DataSourceBase::shared_ptr buildValue() const
Build a internal::ValueDataSource of this type.
Definition: TypeInfo.hpp:230
This interface describes how constructors work.
boost::shared_ptr< ValueFactory > mdsf
Definition: TypeInfo.hpp:486
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:488
void setPortFactory(ConnFactoryPtr cf)
Installs a new port factory such that in-process data can be communicated between components...
Definition: TypeInfo.hpp:457
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
StreamFactoryPtr getStreamFactory() const
Definition: TypeInfo.hpp:475
std::vector< TypeConstructor * > Constructors
Definition: TypeInfo.hpp:477
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:402
std::vector< TypeTransporter * > Transporters
Definition: TypeInfo.hpp:478
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:282
std::ostream & write(std::ostream &os, base::DataSourceBase::shared_ptr in) const
Output this datasource as a human readable string.
Definition: TypeInfo.hpp:265