YARP
Yet Another Robot Platform
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 
11 template <typename T>
13  implementation(maxBuffer),
14  autoDiscard(true),
15  last(nullptr),
16  default_value(nullptr),
17  reader(nullptr)
18 {
19  implementation.setCreator(this);
20  setStrict(false);
21 }
22 
23 template <typename T>
25 {
26  detach();
27 }
28 
29 template <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 }
46 
47 template <typename T>
49 {
50  autoDiscard = !strict;
51  // do discard at earliest time possible
52  implementation.setPrune(autoDiscard);
53 }
54 
55 template <typename T>
57 {
58  return implementation.check() > 0;
59 }
60 
61 template <typename T>
63 {
64  return implementation.check();
65 }
66 
67 
68 template <typename T>
70 {
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 
103 template <typename T>
105 {
106  implementation.interrupt();
107 }
108 
109 template <typename T>
111 {
112  return last;
113 }
114 
115 template <typename T>
117 {
118  // port.setReader(*this);
119  implementation.attachBase(port);
120 }
121 
122 template <typename T>
124 {
125  if (reader != nullptr) {
126  reader->stop();
127  delete reader;
128  reader = nullptr;
129  }
130  reader = new TypedReaderThread<T>(*this, callback);
131 }
132 
133 template <typename T>
135 {
136  if (reader != nullptr) {
137  reader->stop();
138  delete reader;
139  reader = nullptr;
140  }
141 }
142 
143 template <typename T>
145 {
146  return implementation.read(connection);
147 }
148 
149 template <typename T>
151 {
152  return new T;
153 }
154 
155 template <typename T>
157 {
158  implementation.setReplier(reader);
159 }
160 
161 template <typename T>
163 {
164  return implementation.getEnvelope(envelope);
165 }
166 
167 template <typename T>
169 {
170  return implementation.isClosed();
171 }
172 
173 template <typename T>
175 {
176  return implementation.getName();
177 }
178 
179 template <typename T>
181 {
182  return implementation.acceptObjectBase(obj, wrapper);
183 }
184 
185 template <typename T>
187 {
188  return implementation.forgetObjectBase(obj, wrapper);
189 }
190 
191 template <typename T>
193 {
194  return implementation.acquire();
195 }
196 
197 template <typename T>
199 {
200  implementation.release(handle);
201 }
202 
203 template <typename T>
205 {
206  implementation.setTargetPeriod(period);
207 }
208 
209 
210 #endif // YARP_OS_PORTREADERBUFFER_INL_H
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:17
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:25
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
A mini-server for network communication.
Definition: Port.h:47
A callback for typed data from a port.