YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
PortReaderBuffer-inl.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef YARP_OS_PORTREADERBUFFER_INL_H
8#define YARP_OS_PORTREADERBUFFER_INL_H
9
10
11template <typename T>
14 autoDiscard(true),
15 last(nullptr),
16 default_value(nullptr),
17 reader(nullptr)
18{
19 implementation.setCreator(this);
20 setStrict(false);
21}
22
23template <typename T>
28
29template <typename T>
31{
32 // it would also help to close the port, so
33 // that incoming inputs are interrupted
34 if (reader != nullptr) {
35 reader->stop();
36 delete reader;
37 reader = nullptr;
38 }
39 if (default_value != nullptr) {
40 delete default_value;
41 default_value = nullptr;
42 }
43 last = nullptr;
44 implementation.clear();
45}
47template <typename T>
49{
50 autoDiscard = !strict;
51 // do discard at earliest time possible
52 implementation.setPrune(autoDiscard);
54
55template <typename T>
57{
58 return implementation.check() > 0;
59}
60
61template <typename T>
63{
64 return implementation.check();
65}
66
68template <typename T>
71 if (!shouldWait) {
72 if (!check()) {
73 last = nullptr;
74 return last;
75 }
76 }
77 bool missed = false;
78 T* prev = last;
79 last = static_cast<T*>(implementation.readBase(missed, false));
80 if (last != nullptr) {
81 if (autoDiscard) {
82 // go up to date
83 while (check()) {
84 // printf("Dropping something\n");
85 bool tmp;
86 last = static_cast<T*>(implementation.readBase(tmp, true));
87 }
88 }
89 }
90 if (missed) {
91 // we've been asked to enforce a period
92 last = prev;
93 if (last == nullptr) {
94 if (default_value == nullptr) {
95 default_value = new T;
96 }
97 last = default_value;
98 }
99 }
100 return last;
101}
102
103template <typename T>
108
109template <typename T>
111{
112 return last;
114
115template <typename T>
117{
118 // port.setReader(*this);
119 implementation.attachBase(port);
120}
122template <typename T>
125 if (reader != nullptr) {
126 reader->stop();
127 delete reader;
128 reader = nullptr;
129 }
130 reader = new TypedReaderThread<T>(*this, callback);
131}
132
133template <typename T>
135{
136 if (reader != nullptr) {
137 reader->stop();
138 delete reader;
139 reader = nullptr;
140 }
141}
142
143template <typename T>
148
149template <typename T>
154
155template <typename T>
160
161template <typename T>
166
167template <typename T>
172
173template <typename T>
175{
176 return implementation.getName();
177}
178
179template <typename T>
184
185template <typename T>
190
191template <typename T>
196
197template <typename T>
202
203template <typename T>
208
209
210#endif // YARP_OS_PORTREADERBUFFER_INL_H
RandScalar * implementation(void *t)
A mini-server for performing network communication in the background.
void * acquire() override
Take control of the last object read.
std::string getName() const override
Get name of port.
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method.
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool isClosed() override
Returns whether the port associated with this reader has been closed.
T * read(bool shouldWait=true) override
Read an available object from the port.
void setTargetPeriod(double period) override
Try to provide data periodically.
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
An interface for reading from a network connection.
void setCreator(PortReaderBufferBaseCreator *creator)
bool forgetObject(T *obj, yarp::os::PortWriter *wrapper) override
Sender is no longer interested in getting callbacks.
T * read(bool shouldWait=true) override
Read an available object from the port.
virtual bool getEnvelope(PortReader &envelope)
void setTargetPeriod(double period) override
Try to provide data periodically.
bool acceptObject(T *obj, PortWriter *wrapper) override
Send object obj; expect callback to wrapper when obj is no longer in use.
PortReaderBuffer(unsigned int maxBuffer=0)
Constructor.
bool isClosed() override
Returns whether the port associated with this reader has been closed.
void * acquire() override
Take control of the last object read.
PortReader * create() const override
Factory method.
bool check()
Check if data is available.
virtual ~PortReaderBuffer()
Destructor.
void attach(Port &port)
Attach this buffer to a particular port.
T * lastRead() override
Get the last data returned by read()
std::string getName() const override
Get name of port being read from.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
void interrupt() override
Abort any read operation currently in progress.
void setReplier(PortReader &reader) override
If a message is received that requires a reply, use this handler.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void release(void *handle) override
Return control to YARP of an object previously taken control of with the acquire() method.
int getPendingReads() override
Check how many messages are waiting to be read.
void disableCallback() override
Remove a callback set up with useCallback()
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition PortWriter.h:23
A mini-server for network communication.
Definition Port.h:46