Orocos Real-Time Toolkit  2.9.0
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  {
65  private:
68 
69  virtual bool connectionAdded( base::ChannelElementBase::shared_ptr channel_input, ConnPolicy const& policy ) { return true; }
70 
77  InputPort(InputPort const& orig);
78  InputPort& operator=(InputPort const& orig);
79 
80  public:
81  InputPort(std::string const& name = "unnamed", ConnPolicy const& default_policy = ConnPolicy())
83  , endpoint(new internal::ConnOutputEndpoint<T>(this))
84  {}
85 
86  virtual ~InputPort() { disconnect(); }
87 
91  void clear()
92  {
93  getEndpoint()->getReadEndpoint()->clear();
94  }
95 
98  { return read(source, true); }
99 
101  {
103  boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >(source);
104  if (! ds)
105  {
106  log(Error) << "trying to read to an incompatible data source" << endlog();
107  return NoData;
108  }
109  return read(ds->set(), copy_old_data);
110  }
111 
118  FlowStatus readNewest(base::DataSourceBase::shared_ptr source, bool copy_old_data = true)
119  {
121  boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >(source);
122  if (! ds)
123  {
124  log(Error) << "trying to read to an incompatible data source" << endlog();
125  return NoData;
126  }
127  return readNewest(ds->set(), copy_old_data);
128  }
129 
132  { return read(sample, true); }
133 
145  FlowStatus read(typename base::ChannelElement<T>::reference_t sample, bool copy_old_data)
146  {
147  return getEndpoint()->getReadEndpoint()->read(sample, copy_old_data);
148  }
149 
156  FlowStatus readNewest(typename base::ChannelElement<T>::reference_t sample, bool copy_old_data = true)
157  {
158  FlowStatus result = read(sample, copy_old_data);
159  if (result != RTT::NewData)
160  return result;
161 
162  while (read(sample, false) == RTT::NewData);
163  return RTT::NewData;
164  }
165 
174  void getDataSample(T& sample)
175  {
176  sample = getEndpoint()->getReadEndpoint()->data_sample();
177  }
178 
180  virtual const types::TypeInfo* getTypeInfo() const
182 
186  virtual base::PortInterface* clone() const
187  { return new InputPort<T>(this->getName()); }
188 
195  { return new OutputPort<T>(this->getName()); }
196 
201  {
202  return new internal::InputPortSource<T>(*this);
203  }
204 
205  virtual bool createStream(ConnPolicy const& policy)
206  {
207  return internal::ConnFactory::createStream(*this, policy);
208  }
209 
210 #ifndef ORO_DISABLE_PORT_DATA_SCRIPTING
211 
216  {
218  // Force resolution on the overloaded write method
219  typedef FlowStatus (InputPort<T>::*ReadSample)(typename base::ChannelElement<T>::reference_t);
220  ReadSample read_m = &InputPort<T>::read;
221  object->addSynchronousOperation("read", read_m, this).doc("Reads a sample from the port.").arg("sample", "");
222  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.");
223  return object;
224  }
225 #endif
226 
228  {
229  assert(endpoint);
230  return endpoint.get();
231  }
232 
234  {
235  return getEndpoint()->getSharedBuffer();
236  }
237  };
238 }
239 
240 #endif
241 
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:180
This is a channel element that represents the output endpoint of a connection, i.e.
The base class for all internal data representations.
FlowStatus read(typename base::ChannelElement< T >::reference_t sample, bool copy_old_data)
Reads a sample from the connection.
Definition: InputPort.hpp:145
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:194
virtual void set(param_t t)=0
Set this DataSource with a value.
FlowStatus
Returns the status of a data flow read operation.
Definition: FlowStatus.hpp:56
virtual base::ChannelElement< T >::shared_ptr getSharedBuffer() const
Definition: InputPort.hpp:233
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:118
const std::string & getName() const
Get the name of this Port.
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:100
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
FlowStatus read(base::DataSourceBase::shared_ptr source)
Definition: InputPort.hpp:97
virtual internal::ConnOutputEndpoint< T > * getEndpoint() const
Returns the input or output endpoint of this port (if any).
Definition: InputPort.hpp:227
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:156
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:186
virtual Service * createPortObject()
Create accessor Object for this Port, for addition to a TaskContext Object interface.
virtual ~InputPort()
Definition: InputPort.hpp:86
static bool createStream(OutputPort< T > &output_port, ConnPolicy const &policy)
Creates, attaches and checks an outbound stream to an Output port.
virtual void clear()=0
Clears the connection.
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:67
boost::call_traits< T >::reference reference_t
InputPort(std::string const &name="unnamed", ConnPolicy const &default_policy=ConnPolicy())
Definition: InputPort.hpp:81
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:215
A DataSource which has set() methods.
Definition: DataSource.hpp:184
A component&#39;s data output port.
Definition: OutputPort.hpp:70
boost::intrusive_ptr< ConnOutputEndpoint< T > > shared_ptr
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
PortInterface & doc(const std::string &desc)
Set the documentation of this port.
FlowStatus read(typename base::ChannelElement< T >::reference_t sample)
Definition: InputPort.hpp:131
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:205
void clear()
Clears the input buffer (or all input buffers), so that read() will return NoData before a new sample...
Definition: InputPort.hpp:91
base::DataSourceBase * getDataSource()
Returns a base::DataSourceBase interface to read this port.
Definition: InputPort.hpp:200
void getDataSample(T &sample)
Get a sample of the data on this port, without actually reading the port&#39;s data.
Definition: InputPort.hpp:174