YARP
Yet Another Robot Platform
yarp::os::PortReaderBuffer< T > Class Template Reference

Buffer incoming data to a port. More...

#include <yarp/os/PortReaderBuffer.h>

+ Inheritance diagram for yarp::os::PortReaderBuffer< T >:

Detailed Description

template<typename T>
class yarp::os::PortReaderBuffer< T >

Buffer incoming data to a port.

An instance of this class can be associated with a Port by calling attach(). From then on data that arrives to the Port will be passed to this buffer, to be picked up at the user's leisure by calling check() and read(). "T" should be a PortReader class, such as Bottle.

Definition at line 39 of file PortReaderBuffer.h.

Public Member Functions

 PortReaderBuffer (unsigned int maxBuffer=0)
 Constructor. More...
 
virtual ~PortReaderBuffer ()
 Destructor. More...
 
void detach ()
 
void setStrict (bool strict=true) override
 Call this to strictly keep all messages, or allow old ones to be quietly dropped. More...
 
bool check ()
 Check if data is available. More...
 
int getPendingReads () override
 Check how many messages are waiting to be read. More...
 
T * read (bool shouldWait=true) override
 Read an available object from the port. More...
 
void interrupt () override
 Abort any read operation currently in progress. More...
 
T * lastRead () override
 Get the last data returned by read() More...
 
void attach (Port &port)
 Attach this buffer to a particular port. More...
 
void useCallback (TypedReaderCallback< T > &callback) override
 Set an object whose onRead method will be called when data is available. More...
 
void disableCallback () override
 Remove a callback set up with useCallback() More...
 
virtual bool read (ConnectionReader &connection)
 Reads objects from a network connection. More...
 
PortReadercreate () const override
 Factory method. More...
 
void setReplier (PortReader &reader) override
 If a message is received that requires a reply, use this handler. More...
 
virtual bool getEnvelope (PortReader &envelope)
 
bool isClosed () override
 Returns whether the port associated with this reader has been closed. More...
 
std::string getName () const override
 Get name of port being read from. More...
 
bool acceptObject (T *obj, PortWriter *wrapper) override
 Send object obj; expect callback to wrapper when obj is no longer in use. More...
 
bool forgetObject (T *obj, yarp::os::PortWriter *wrapper) override
 Sender is no longer interested in getting callbacks. More...
 
void * acquire () override
 Take control of the last object read. More...
 
void release (void *handle) override
 Return control to YARP of an object previously taken control of with the acquire() method. More...
 
void setTargetPeriod (double period) override
 Try to provide data periodically. More...
 
- Public Member Functions inherited from yarp::os::TypedReader< T >
virtual ~TypedReader ()
 Destructor. More...
 
- Public Member Functions inherited from yarp::os::LocalReader< T >
virtual ~LocalReader ()
 
- Public Member Functions inherited from yarp::os::PortReaderBufferBaseCreator
virtual ~PortReaderBufferBaseCreator ()
 

Private Attributes

yarp::os::PortReaderBufferBase implementation
 
bool autoDiscard
 
T * last
 
T * default_value
 
TypedReaderThread< T > * reader
 

Constructor & Destructor Documentation

◆ PortReaderBuffer()

template<typename T >
yarp::os::PortReaderBuffer< T >::PortReaderBuffer ( unsigned int  maxBuffer = 0)

Constructor.

Parameters
maxBufferMaximum number of buffers permitted (0 = no limit)

Definition at line 15 of file PortReaderBuffer-inl.h.

◆ ~PortReaderBuffer()

template<typename T >
yarp::os::PortReaderBuffer< T >::~PortReaderBuffer ( )
virtual

Destructor.

Definition at line 27 of file PortReaderBuffer-inl.h.

Member Function Documentation

◆ acceptObject()

template<typename T>
bool yarp::os::PortReaderBuffer< T >::acceptObject ( T *  obj,
PortWriter wrapper 
)
overridevirtual

Send object obj; expect callback to wrapper when obj is no longer in use.

Implements yarp::os::LocalReader< T >.

Definition at line 183 of file PortReaderBuffer-inl.h.

◆ acquire()

template<typename T >
void * yarp::os::PortReaderBuffer< T >::acquire ( )
overridevirtual

Take control of the last object read.

YARP will not reuse that object until it is explicitly released by the user. Be careful - if you acquire objects without releasing, YARP will keep making new ones to store incoming messages. So you need to release all objects you acquire eventually to avoid running out of memory.

Returns
the handle to call release() with in order to give YARP back control of the last object read.

Implements yarp::os::TypedReader< T >.

Definition at line 195 of file PortReaderBuffer-inl.h.

◆ attach()

template<typename T >
void yarp::os::PortReaderBuffer< T >::attach ( Port port)

Attach this buffer to a particular port.

Data arriving to that port will from now on be placed in this buffer.

Parameters
portthe port to attach to

Definition at line 119 of file PortReaderBuffer-inl.h.

◆ check()

template<typename T >
bool yarp::os::PortReaderBuffer< T >::check ( )

Check if data is available.

Returns
true iff data is available (i.e. a call to read() will return immediately and successfully)

Definition at line 59 of file PortReaderBuffer-inl.h.

◆ create()

template<typename T >
yarp::os::PortReader * yarp::os::PortReaderBuffer< T >::create ( ) const
overridevirtual

Factory method.

New instances are created as needed to store incoming data. By default, this just uses the default constructor - override this if you need to do something fancier (such as allocating a shared memory space).

Returns
new instance of the templated type.

Implements yarp::os::PortReaderBufferBaseCreator.

Definition at line 153 of file PortReaderBuffer-inl.h.

◆ detach()

template<typename T >
void yarp::os::PortReaderBuffer< T >::detach ( )

Definition at line 33 of file PortReaderBuffer-inl.h.

◆ disableCallback()

template<typename T >
void yarp::os::PortReaderBuffer< T >::disableCallback ( )
overridevirtual

Remove a callback set up with useCallback()

Implements yarp::os::TypedReader< T >.

Definition at line 137 of file PortReaderBuffer-inl.h.

◆ forgetObject()

template<typename T>
bool yarp::os::PortReaderBuffer< T >::forgetObject ( T *  obj,
yarp::os::PortWriter wrapper 
)
overridevirtual

Sender is no longer interested in getting callbacks.

Implements yarp::os::LocalReader< T >.

Definition at line 189 of file PortReaderBuffer-inl.h.

◆ getEnvelope()

template<typename T >
bool yarp::os::PortReaderBuffer< T >::getEnvelope ( PortReader envelope)
virtual

Definition at line 165 of file PortReaderBuffer-inl.h.

◆ getName()

template<typename T >
std::string yarp::os::PortReaderBuffer< T >::getName ( ) const
overridevirtual

Get name of port being read from.

Returns
name of port

Implements yarp::os::TypedReader< T >.

Definition at line 177 of file PortReaderBuffer-inl.h.

◆ getPendingReads()

template<typename T >
int yarp::os::PortReaderBuffer< T >::getPendingReads ( )
overridevirtual

Check how many messages are waiting to be read.

Returns
number of pending messages

Implements yarp::os::TypedReader< T >.

Definition at line 65 of file PortReaderBuffer-inl.h.

◆ interrupt()

template<typename T >
void yarp::os::PortReaderBuffer< T >::interrupt ( )
overridevirtual

Abort any read operation currently in progress.

Implements yarp::os::TypedReader< T >.

Definition at line 107 of file PortReaderBuffer-inl.h.

◆ isClosed()

template<typename T >
bool yarp::os::PortReaderBuffer< T >::isClosed ( )
overridevirtual

Returns whether the port associated with this reader has been closed.

Returns
true if Port associated with this reader has been closed

Implements yarp::os::TypedReader< T >.

Definition at line 171 of file PortReaderBuffer-inl.h.

◆ lastRead()

template<typename T >
T * yarp::os::PortReaderBuffer< T >::lastRead ( )
overridevirtual

Get the last data returned by read()

Returns
pointer to last data returned by read(), or nullptr on failure.

Implements yarp::os::TypedReader< T >.

Definition at line 113 of file PortReaderBuffer-inl.h.

◆ read() [1/2]

template<typename T >
T * yarp::os::PortReaderBuffer< T >::read ( bool  shouldWait = true)
overridevirtual

Read an available object from the port.

Parameters
shouldWaittrue if the method should wait until an object is available, false if the call should return immediately if no message is available
Returns
A pointer to an object read from the port, or nullptr if none is available and waiting was not requested. This object is owned by the communication system and should not be deleted by the user. The object is available to the user until the next call to one of the read methods, after which it should not be accessed again.

Implements yarp::os::TypedReader< T >.

Definition at line 72 of file PortReaderBuffer-inl.h.

◆ read() [2/2]

template<typename T >
bool yarp::os::PortReaderBuffer< T >::read ( ConnectionReader connection)
virtual

Reads objects from a network connection.

This method is called by a port when data is received.

Parameters
connectionan interface to the network connection for reading
Returns
true iff the object is successfully read

Definition at line 147 of file PortReaderBuffer-inl.h.

◆ release()

template<typename T >
void yarp::os::PortReaderBuffer< T >::release ( void *  handle)
overridevirtual

Return control to YARP of an object previously taken control of with the acquire() method.

Parameters
handlethe pointer returned by acquire() when control of the object was taken by the user.

Implements yarp::os::TypedReader< T >.

Definition at line 201 of file PortReaderBuffer-inl.h.

◆ setReplier()

template<typename T >
void yarp::os::PortReaderBuffer< T >::setReplier ( PortReader reader)
overridevirtual

If a message is received that requires a reply, use this handler.

No buffering happens.

Parameters
readerthe handler to use

Implements yarp::os::TypedReader< T >.

Definition at line 159 of file PortReaderBuffer-inl.h.

◆ setStrict()

template<typename T >
void yarp::os::PortReaderBuffer< T >::setStrict ( bool  strict = true)
overridevirtual

Call this to strictly keep all messages, or allow old ones to be quietly dropped.

If you don't call this, old messages will be quietly dropped.

Parameters
strictTrue to keep all messages until they are read, false to drop old messages when a new one comes in.
Warning
If you can't read them as fast as the come in, watch out.

Implements yarp::os::TypedReader< T >.

Definition at line 51 of file PortReaderBuffer-inl.h.

◆ setTargetPeriod()

template<typename T >
void yarp::os::PortReaderBuffer< T >::setTargetPeriod ( double  period)
overridevirtual

Try to provide data periodically.

If no new data arrives in a given period, repeat the last data received (if any). Similarly, the port should not pass on data more frequently than the given period.

Parameters
periodtarget period in (fractional) seconds.

Implements yarp::os::TypedReader< T >.

Definition at line 207 of file PortReaderBuffer-inl.h.

◆ useCallback()

template<typename T>
void yarp::os::PortReaderBuffer< T >::useCallback ( TypedReaderCallback< T > &  callback)
overridevirtual

Set an object whose onRead method will be called when data is available.

Parameters
callbackthe object whose onRead method will be called with data

Implements yarp::os::TypedReader< T >.

Definition at line 126 of file PortReaderBuffer-inl.h.

Member Data Documentation

◆ autoDiscard

template<typename T>
bool yarp::os::PortReaderBuffer< T >::autoDiscard
private

Definition at line 144 of file PortReaderBuffer.h.

◆ default_value

template<typename T>
T* yarp::os::PortReaderBuffer< T >::default_value
private

Definition at line 146 of file PortReaderBuffer.h.

◆ implementation

template<typename T>
yarp::os::PortReaderBufferBase yarp::os::PortReaderBuffer< T >::implementation
private

Definition at line 143 of file PortReaderBuffer.h.

◆ last

template<typename T>
T* yarp::os::PortReaderBuffer< T >::last
private

Definition at line 145 of file PortReaderBuffer.h.

◆ reader

template<typename T>
TypedReaderThread<T>* yarp::os::PortReaderBuffer< T >::reader
private

Definition at line 147 of file PortReaderBuffer.h.


The documentation for this class was generated from the following files: