Orocos Real-Time Toolkit  2.9.0
TypeInfo.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 TypeInfo.cpp
3 
4  TypeInfo.cpp - 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 #include "TypeInfo.hpp"
40 #include "TypeConstructor.hpp"
41 #include "../internal/DataSourceTypeInfo.hpp"
42 #include "../internal/ConnFactory.hpp"
43 #include "TypeTransporter.hpp"
44 
45 #include "rtt-config.h"
46 
47 #include "../Logger.hpp"
48 #include "../base/AttributeBase.hpp"
49 
50 namespace RTT
51 {
52  using namespace std;
53  using namespace detail;
54  using namespace internal;
55 
57  {
58  // cleanup transporters
59  for (Transporters::iterator i = transporters.begin(); i != transporters.end(); ++i)
60  delete *i;
61 
62  // cleanup constructors
63  for (Constructors::iterator i= constructors.begin(); i != constructors.end(); ++i)
64  delete (*i);
65  }
66 
67  std::vector<std::string> TypeInfo::getTypeNames() const
68  {
69  return mtypenames;
70  }
71 
72  void TypeInfo::addAlias(const std::string& alias) {
73  // only alias new names:
74  if ( !alias.empty() && find(mtypenames.begin(), mtypenames.end(), alias) == mtypenames.end() )
75  mtypenames.push_back(alias);
76  }
77 
78  bool TypeInfo::isType(const std::string& name) {
79  return (find(mtypenames.begin(), mtypenames.end(), name) != mtypenames.end() );
80  }
81 
82  base::AttributeBase* TypeInfo::buildVariable(std::string name, int hint) const
83  {
84  return mdsf ? mdsf->buildVariable(name, hint) : 0;
85  }
86 
87  DataSourceBase::shared_ptr TypeInfo::construct(const std::vector<DataSourceBase::shared_ptr>& args) const
88  {
89 
91  // build default constructible:
92  if ( args.empty() ) {
93  AttributeBase* ab = this->buildVariable("constructor");
94  ds = ab->getDataSource();
95  delete ab;
96  return ds;
97  }
98 
99  // return same type if equal:
100  if ( args.size() == 1 && args.front()->getTypeInfo() == this )
101  return args.front();
102 
103  Constructors::const_iterator i= constructors.begin();
104  while (i != constructors.end() ) {
105  ds = (*i)->build( args );
106  if ( ds )
107  return ds;
108  ++i;
109  }
110  // returns empty data source to indicate not constructible
111  return ds;
112  }
113 
115  constructors.push_back(tb);
116  }
117 
119  {
120  if ( arg->getTypeInfo() == this )
121  return arg;
122 
123  //log(Info) << getTypeName() << ": trying to convert from " << arg->getTypeName()<<endlog();
125  Constructors::const_iterator i= constructors.begin();
126  while (i != constructors.end() ) {
127  ds = (*i)->convert( arg );
128  if ( ds ) {
129  return ds;
130  }
131  ++i;
132  }
133  // if no conversion happend, return arg again.
134  return arg;
135  }
136 
137  bool TypeInfo::addProtocol(int protocol_id, TypeTransporter* tt)
138  {
139  if (transporters.size() < static_cast<size_t>(protocol_id + 1))
140  transporters.resize(protocol_id + 1);
141  if ( transporters[protocol_id] ) {
142  log(Debug) << "A protocol with id "<<protocol_id<<" was already added for type "<< getTypeName()<<endlog();
143  delete tt;
144  return false;
145  }
146  transporters[protocol_id] = tt;
147  return true;
148  }
149 
150  TypeTransporter* TypeInfo::getProtocol(int protocol_id) const
151  {
152  // if the protocol is unknown to this type, return the protocol of the 'unknown type'
153  // type, which is a fallback such that we won't have to return zero, but can
154  // gracefully fall-back.
155  // In order to not endlessly recurse, we check if we aren't the UnknownType !
156  if ( protocol_id + 1 > int(transporters.size()) || transporters[protocol_id] == 0) {
158  return DataSourceTypeInfo<UnknownType>::getTypeInfo()->getProtocol( protocol_id );
159  else {
160  log(Warning) << "The protocol with id "<<protocol_id<<" did not register a fall-back handler for unknown types!"<<endlog();
161  log(Warning) << " triggered by: "<< getTypeName() << " which does not have a transport."<<endlog();
162  return 0; // That transport did not register a fall-back !
163  }
164  }
165  return transporters[protocol_id];
166  }
167 
168  bool TypeInfo::hasProtocol(int protocol_id) const
169  {
170  // if the protocol is unknown to this type, return the protocol of the 'unknown type'
171  // type, which is a fallback such that we won't have to return zero, but can
172  // gracefully fall-back.
173  // In order to not endlessly recurse, we check if we aren't the UnknownType !
174  if ( protocol_id + 1 > int(transporters.size()) || transporters[protocol_id] == 0) {
175  return false;
176  }
177  return true;
178  }
179 
180  std::vector<int> TypeInfo::getTransportNames() const
181  {
182  std::vector<int> ret;
183  for (size_t i=0; i<transporters.size(); ++i)
184  {
185  // dump only protocols with an actual transporter
186  // NB the transporter does not have a name, so you have to manually
187  // match the protocol number to an actual transport
188  if (0 != transporters[i])
189  {
190  ret.push_back(i);
191  }
192  }
193  return ret;
194  }
195 
196 
197  base::InputPortInterface* TypeInfo::inputPort(std::string const& name) const
198  {
199  return mconnf ? mconnf->inputPort(name) : 0;
200  }
201 
202  base::OutputPortInterface* TypeInfo::outputPort(std::string const& name) const
203  {
204  return mconnf ? mconnf->outputPort(name) : 0;
205  }
206 
208  {
209  return mconnf ? mconnf->buildDataStorage(policy) : base::ChannelElementBase::shared_ptr();
210  }
211 
213  {
214  return mconnf ? mconnf->buildChannelOutput(port, policy) : base::ChannelElementBase::shared_ptr();
215  }
216 
218  {
219  return mconnf ? mconnf->buildChannelInput(port, policy) : base::ChannelElementBase::shared_ptr();
220  }
221 
223  {
224  return mconnf ? mconnf->buildSharedConnection(output_port, input_port, policy) : internal::SharedConnectionBase::shared_ptr();
225  }
226 }
The base class of the InputPort.
std::vector< int > getTransportNames() const
Return a list of protocols that have transporters.
Definition: TypeInfo.cpp:180
base::DataSourceBase::shared_ptr convert(base::DataSourceBase::shared_ptr arg) const
Automatic conversion: convert a internal::DataSource to this type.
Definition: TypeInfo.cpp:118
void addAlias(const std::string &alias)
Adds an alias to the this type.
Definition: TypeInfo.cpp:72
void addConstructor(TypeConstructor *tb)
Add a constructor/convertor object.
Definition: TypeInfo.cpp:114
boost::intrusive_ptr< SharedConnectionBase > shared_ptr
base::ChannelElementBase::shared_ptr buildChannelOutput(base::InputPortInterface &port, ConnPolicy const &policy) const
Definition: TypeInfo.cpp:212
This interface defines the function a transport protocol must support in order to allow Orocos compon...
base::ChannelElementBase::shared_ptr buildDataStorage(ConnPolicy const &policy) const
Creates single data or buffered storage for this type.
Definition: TypeInfo.cpp:207
STL namespace.
base::ChannelElementBase::shared_ptr buildChannelInput(base::OutputPortInterface &port, ConnPolicy const &policy) const
Definition: TypeInfo.cpp:217
virtual DataSourceBase::shared_ptr getDataSource() const =0
Return a internal::DataSource which contains the same contents.
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
The base class of each OutputPort.
std::vector< std::string > getTypeNames() const
Returns all registered names and aliases of this type.
Definition: TypeInfo.cpp:67
An attribute is a minimalistic, named placeholder for data.
bool hasProtocol(int protocol_id) const
Check if this type is transporable over a given protocol.
Definition: TypeInfo.cpp:168
bool isType(const std::string &name)
Returns true if this type is known by the type system under the given name.
Definition: TypeInfo.cpp:78
bool addProtocol(int protocol_id, TypeTransporter *tt)
Register a protocol for data transport over a network.
Definition: TypeInfo.cpp:137
boost::intrusive_ptr< ChannelElementBase > shared_ptr
Every DataSource of type T has a type info class which it can ask type information.
base::AttributeBase * buildVariable(std::string name, int sizehint) const
Build a modifyable instance of this type.
Definition: TypeInfo.cpp:82
base::DataSourceBase::shared_ptr construct(const std::vector< base::DataSourceBase::shared_ptr > &args) const
Constructor syntax: construct a internal::DataSource which returns an instance of data depending on t...
Definition: TypeInfo.cpp:87
TypeTransporter * getProtocol(int protocol_id) const
Returns this type&#39;s transport for a given protocol.
Definition: TypeInfo.cpp:150
base::OutputPortInterface * outputPort(std::string const &name) const
Returns a new OutputPort<T> object where T is the type represented by this TypeInfo object...
Definition: TypeInfo.cpp:202
This interface describes how constructors work.
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
internal::SharedConnectionBase::shared_ptr buildSharedConnection(base::OutputPortInterface *output_port, base::InputPortInterface *input_port, ConnPolicy const &policy) const
Definition: TypeInfo.cpp:222
base::InputPortInterface * inputPort(std::string const &name) const
Returns a new InputPort<T> object where T is the type represented by this TypeInfo object...
Definition: TypeInfo.cpp:197