YARP
Yet Another Robot Platform
PortReaderBuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_PORTREADERBUFFER_H
11 #define YARP_OS_PORTREADERBUFFER_H
12 
13 #include <yarp/os/Bottle.h>
14 #include <yarp/os/LocalReader.h>
15 #include <yarp/os/Port.h>
16 #include <yarp/os/PortReader.h>
19 #include <yarp/os/Thread.h>
20 #include <yarp/os/TypedReader.h>
23 
24 #include <cstdio>
25 #include <string>
26 
27 namespace yarp {
28 namespace os {
29 
38 template <typename T>
40  public yarp::os::TypedReader<T>,
41  public yarp::os::LocalReader<T>,
43 {
44 public:
50  PortReaderBuffer(unsigned int maxBuffer = 0);
51 
55  virtual ~PortReaderBuffer();
56 
57  void detach();
58 
59  // documented in TypedReader
60  void setStrict(bool strict = true) override;
61 
68  bool check();
69 
70  // documented in TypedReader
71  int getPendingReads() override;
72 
73  // documented in TypedReader
74  T* read(bool shouldWait = true) override;
75 
76  // documented in TypedReader
77  void interrupt() override;
78 
79  // documented in TypedReader
80  T* lastRead() override;
81 
89  void attach(Port& port);
90 
91  // documented in TypedReader
92  void useCallback(TypedReaderCallback<T>& callback) override;
93 
94  // documented in TypedReader
95  void disableCallback() override;
96 
104  virtual bool read(ConnectionReader& connection);
105 
114  PortReader* create() const override;
115 
116  // documented in TypedReader
117  void setReplier(PortReader& reader) override;
118 
119  virtual bool getEnvelope(PortReader& envelope);
120 
121  // documented in TypedReader
122  bool isClosed() override;
123 
124  // documented in TypedReader
125  std::string getName() const override;
126 
127  // documented in LocalReader
128  bool acceptObject(T* obj, PortWriter* wrapper) override;
129 
130  // documented in LocalReader
131  bool forgetObject(T* obj, yarp::os::PortWriter* wrapper) override;
132 
133  // documented in TypedReader
134  void* acquire() override;
135 
136  // documented in TypedReader
137  void release(void* handle) override;
138 
139  // documented in TypedReader
140  void setTargetPeriod(double period) override;
141 
142 private:
143  yarp::os::PortReaderBufferBase implementation;
144  bool autoDiscard;
145  T* last;
146  T* default_value;
147  TypedReaderThread<T>* reader;
148 };
149 
150 } // namespace os
151 } // namespace yarp
152 
154 
155 #endif // YARP_OS_PORTREADERBUFFER_H
yarp::os::TypedReaderThread
Definition: TypedReaderThread.h:26
yarp::os::TypedReader
A base class for sources of typed data.
Definition: TypedReader.h:26
yarp::os::PortReaderBuffer::acquire
void * acquire() override
Take control of the last object read.
Definition: PortReaderBuffer-inl.h:195
TypedReaderThread.h
yarp::os::PortReaderBuffer::check
bool check()
Check if data is available.
Definition: PortReaderBuffer-inl.h:59
Port.h
yarp::os::PortReaderBuffer::acceptObject
bool acceptObject(T *obj, PortWriter *wrapper) override
Send object obj; expect callback to wrapper when obj is no longer in use.
Definition: PortReaderBuffer-inl.h:183
yarp::os::PortReaderBuffer::disableCallback
void disableCallback() override
Remove a callback set up with useCallback()
Definition: PortReaderBuffer-inl.h:137
yarp::os::PortReaderBuffer
Buffer incoming data to a port.
Definition: PortReaderBuffer.h:43
yarp::os::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:31
PortReaderBufferBaseCreator.h
yarp::os::PortReaderBuffer::attach
void attach(Port &port)
Attach this buffer to a particular port.
Definition: PortReaderBuffer-inl.h:119
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
yarp::os::PortReaderBuffer::interrupt
void interrupt() override
Abort any read operation currently in progress.
Definition: PortReaderBuffer-inl.h:107
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::PortReaderBuffer::~PortReaderBuffer
virtual ~PortReaderBuffer()
Destructor.
Definition: PortReaderBuffer-inl.h:27
PortReaderBufferBase.h
yarp::os::PortReaderBuffer::create
PortReader * create() const override
Factory method.
Definition: PortReaderBuffer-inl.h:153
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
yarp::os::PortReaderBuffer::getName
std::string getName() const override
Get name of port being read from.
Definition: PortReaderBuffer-inl.h:177
yarp::os::PortReaderBuffer::lastRead
T * lastRead() override
Get the last data returned by read()
Definition: PortReaderBuffer-inl.h:113
yarp::os::LocalReader
Under development.
Definition: LocalReader.h:22
yarp::os::PortReaderBufferBase
Definition: PortReaderBufferBase.h:26
yarp::os::PortReaderBuffer::getPendingReads
int getPendingReads() override
Check how many messages are waiting to be read.
Definition: PortReaderBuffer-inl.h:65
Thread.h
yarp::os::PortReaderBuffer::useCallback
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
Definition: PortReaderBuffer-inl.h:126
yarp::os::PortReaderBuffer::getEnvelope
virtual bool getEnvelope(PortReader &envelope)
Definition: PortReaderBuffer-inl.h:165
yarp::os::PortReaderBuffer::isClosed
bool isClosed() override
Returns whether the port associated with this reader has been closed.
Definition: PortReaderBuffer-inl.h:171
yarp::os::PortReaderBuffer::setTargetPeriod
void setTargetPeriod(double period) override
Try to provide data periodically.
Definition: PortReaderBuffer-inl.h:207
TypedReaderCallback.h
yarp::os::PortReaderBuffer::PortReaderBuffer
PortReaderBuffer(unsigned int maxBuffer=0)
Constructor.
Definition: PortReaderBuffer-inl.h:15
yarp::os::PortReaderBuffer::read
T * read(bool shouldWait=true) override
Read an available object from the port.
Definition: PortReaderBuffer-inl.h:72
yarp::os::PortReaderBuffer::setReplier
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
Definition: PortReaderBuffer-inl.h:159
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::os::PortReaderBuffer::detach
void detach()
Definition: PortReaderBuffer-inl.h:33
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::PortReaderBuffer::setStrict
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
Definition: PortReaderBuffer-inl.h:51
PortReader.h
PortReaderBuffer-inl.h
yarp::os::PortReaderBuffer::forgetObject
bool forgetObject(T *obj, yarp::os::PortWriter *wrapper) override
Sender is no longer interested in getting callbacks.
Definition: PortReaderBuffer-inl.h:189
TypedReader.h
yarp::os::PortReaderBuffer::release
void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method.
Definition: PortReaderBuffer-inl.h:201
Bottle.h
LocalReader.h
yarp::os::PortReaderBufferBaseCreator
Definition: PortReaderBufferBaseCreator.h:21