Orocos Real-Time Toolkit  2.8.3
InputPort.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 InputPort.hpp
3 
4  InputPort.hpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Sylvain Joyeux
8  email : sylvain.joyeux@m4x.org
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_INPUT_PORT_HPP
40 #define ORO_INPUT_PORT_HPP
41 
43 #include "internal/Channels.hpp"
45 #include "Service.hpp"
46 #include "OperationCaller.hpp"
47 
48 #include "OutputPort.hpp"
49 
50 namespace RTT
51 {
62  template<typename T>
64  {
66 
67  virtual bool connectionAdded( base::ChannelElementBase::shared_ptr channel_input, ConnPolicy const& policy ) { return true; }
68 
69  bool do_read(typename base::ChannelElement<T>::reference_t sample, FlowStatus& result, bool copy_old_data, const internal::ConnectionManager::ChannelDescriptor& descriptor)
70  {
71  typename base::ChannelElement<T>::shared_ptr input = static_cast< base::ChannelElement<T>* >( descriptor.get<1>().get() );
72  assert( result != NewData );
73  if ( input ) {
74  FlowStatus tresult = input->read(sample, copy_old_data);
75  // the result trickery is for not overwriting OldData with NoData.
76  if (tresult == NewData) {
77  result = tresult;
78  return true;
79  }
80  // stores OldData result
81  if (tresult > result)
82  result = tresult;
83  }
84  return false;
85  }
86 
93  InputPort(InputPort const& orig);
94  InputPort& operator=(InputPort const& orig);
95  public:
96  InputPort(std::string const& name = "unnamed", ConnPolicy const& default_policy = ConnPolicy())
98  {}
99 
100  virtual ~InputPort() { disconnect(); }
101 
104  { return read(source, true); }
105 
107  {
109  boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >(source);
110  if (! ds)
111  {
112  log(Error) << "trying to read to an incompatible data source" << endlog();
113  return NoData;
114  }
115  return read(ds->set(), copy_old_data);
116  }
117 
124  FlowStatus readNewest(base::DataSourceBase::shared_ptr source, bool copy_old_data = true)
125  {
127  boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >(source);
128  if (! ds)
129  {
130  log(Error) << "trying to read to an incompatible data source" << endlog();
131  return NoData;
132  }
133  return readNewest(ds->set(), copy_old_data);
134  }
135 
138  { return read(sample, true); }
139 
151  FlowStatus read(typename base::ChannelElement<T>::reference_t sample, bool copy_old_data)
152  {
153  FlowStatus result = NoData;
154  // read and iterate if necessary.
155  cmanager.select_reader_channel( boost::bind( &InputPort::do_read, this, boost::ref(sample), boost::ref(result), _1, _2 ), copy_old_data );
156  return result;
157  }
158 
159 
166  FlowStatus readNewest(typename base::ChannelElement<T>::reference_t sample, bool copy_old_data = true)
167  {
168  FlowStatus result = read(sample, copy_old_data);
169  if (result != RTT::NewData)
170  return result;
171 
172  while (read(sample, false) == RTT::NewData);
173  return RTT::NewData;
174  }
175 
184  void getDataSample(T& sample)
185  {
187  if ( input ) {
188  sample = input->data_sample();
189  }
190  }
191 
193  virtual const types::TypeInfo* getTypeInfo() const
195 
199  virtual base::PortInterface* clone() const
200  { return new InputPort<T>(this->getName()); }
201 
208  { return new OutputPort<T>(this->getName()); }
209 
214  {
215  return new internal::InputPortSource<T>(*this);
216  }
217 
218  virtual bool createStream(ConnPolicy const& policy)
219  {
220  return internal::ConnFactory::createStream(*this, policy);
221  }
222 
223 #ifndef ORO_DISABLE_PORT_DATA_SCRIPTING
224 
229  {
231  // Force resolution on the overloaded write method
232  typedef FlowStatus (InputPort<T>::*ReadSample)(typename base::ChannelElement<T>::reference_t);
233  ReadSample read_m = &InputPort<T>::read;
234  object->addSynchronousOperation("read", read_m, this).doc("Reads a sample from the port.").arg("sample", "");
235  object->addSynchronousOperation("clear", &InputPortInterface::clear, this).doc("Clears any remaining data in this port. After a clear, a read() will return NoData if no writes happened in between.");
236  return object;
237  }
238 #endif
239  };
240 }
241 
242 #endif
243 
The base class of the InputPort.
boost::intrusive_ptr< ChannelElement< T > > shared_ptr
virtual const types::TypeInfo * getTypeInfo() const
Returns the types::TypeInfo object for the port&#39;s type.
Definition: InputPort.hpp:193
This is a channel element that represents the output endpoint of a connection, i.e.
The base class for all internal data representations.
base::ChannelElementBase * getCurrentChannel() const
Returns the first added channel or if select_if was called, the selected channel. ...
FlowStatus read(typename base::ChannelElement< T >::reference_t sample, bool copy_old_data)
Reads a sample from the connection.
Definition: InputPort.hpp:151
virtual void disconnect()
Removes any connection that either go to or come from this port and removes all callbacks and cleans ...
virtual base::PortInterface * antiClone() const
Create the anti-clone (inverse port) of this port with the same name A port for reading will return a...
Definition: InputPort.hpp:207
virtual void set(param_t t)=0
Set this DataSource with a value.
FlowStatus
Returns the status of a data flow read.
Definition: FlowStatus.hpp:54
FlowStatus readNewest(base::DataSourceBase::shared_ptr source, bool copy_old_data=true)
Read all new samples that are available on this port, and returns the last one.
Definition: InputPort.hpp:124
virtual FlowStatus read(reference_t sample, bool copy_old_data)
Reads a sample from the connection.
const std::string & getName() const
Get the name of this Port.
internal::ConnectionManager cmanager
A component&#39;s data input port.
Definition: InputPort.hpp:63
FlowStatus read(base::DataSourceBase::shared_ptr source, bool copy_old_data)
Reads the port and updates the value hold by the given data source.
Definition: InputPort.hpp:106
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:92
FlowStatus read(base::DataSourceBase::shared_ptr source)
Definition: InputPort.hpp:103
This class allows storage and retrieval of operations, ports, attributes and properties provided by a...
Definition: Service.hpp:93
FlowStatus readNewest(typename base::ChannelElement< T >::reference_t sample, bool copy_old_data=true)
Read all new samples that are available on this port, and returns the last one.
Definition: InputPort.hpp:166
void clear()
Clears the connection.
A typed version of ChannelElementBase.
static const types::TypeInfo * getTypeInfo()
Return the typeinfo object.
virtual base::PortInterface * clone() const
Create a clone of this port with the same name.
Definition: InputPort.hpp:199
virtual Service * createPortObject()
Create accessor Object for this Port, for addition to a TaskContext Object interface.
virtual ~InputPort()
Definition: InputPort.hpp:100
static bool createStream(OutputPort< T > &output_port, ConnPolicy const &policy)
Creates, attaches and checks an outbound stream to an Output port.
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:66
boost::tuple< boost::shared_ptr< ConnID >, base::ChannelElementBase::shared_ptr, ConnPolicy > ChannelDescriptor
A Channel (= connection) is described by an opaque ConnID object, the first element of the channel an...
boost::call_traits< T >::reference reference_t
InputPort(std::string const &name="unnamed", ConnPolicy const &default_policy=ConnPolicy())
Definition: InputPort.hpp:96
This class represents a read port using the data source interface.
boost::intrusive_ptr< ChannelElementBase > shared_ptr
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
virtual Service * createPortObject()
Create accessor Object for this Port, for addition to a TaskContext Object interface.
Definition: InputPort.hpp:228
A DataSource which has set() methods.
Definition: DataSource.hpp:184
void select_reader_channel(Pred pred, bool copy_old_data)
Selects a connection as the current channel if pred(connection) is true.
A component&#39;s data output port.
Definition: OutputPort.hpp:70
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
PortInterface & doc(const std::string &desc)
Set the documentation of this port.
FlowStatus read(typename base::ChannelElement< T >::reference_t sample)
Definition: InputPort.hpp:137
The base class of every data flow port.
virtual bool createStream(ConnPolicy const &policy)
Creates a data stream from or to this port using connection-less transports.
Definition: InputPort.hpp:218
base::DataSourceBase * getDataSource()
Returns a base::DataSourceBase interface to read this port.
Definition: InputPort.hpp:213
virtual bool data_sample(param_t sample)
Provides a data sample to initialize this connection.
void getDataSample(T &sample)
Get a sample of the data on this port, without actually reading the port&#39;s data.
Definition: InputPort.hpp:184