YARP
Yet Another Robot Platform
PortReaderBuffer-inl.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_INL_H
11 #define YARP_OS_PORTREADERBUFFER_INL_H
12 
13 
14 template <typename T>
16  implementation(maxBuffer),
17  autoDiscard(true),
18  last(nullptr),
19  default_value(nullptr),
20  reader(nullptr)
21 {
22  implementation.setCreator(this);
23  setStrict(false);
24 }
25 
26 template <typename T>
28 {
29  detach();
30 }
31 
32 template <typename T>
34 {
35  // it would also help to close the port, so
36  // that incoming inputs are interrupted
37  if (reader != nullptr) {
38  reader->stop();
39  delete reader;
40  reader = nullptr;
41  }
42  if (default_value != nullptr) {
43  delete default_value;
44  default_value = nullptr;
45  }
46  last = nullptr;
47  implementation.clear();
48 }
49 
50 template <typename T>
52 {
53  autoDiscard = !strict;
54  // do discard at earliest time possible
55  implementation.setPrune(autoDiscard);
56 }
57 
58 template <typename T>
60 {
61  return implementation.check() > 0;
62 }
63 
64 template <typename T>
66 {
67  return implementation.check();
68 }
69 
70 
71 template <typename T>
73 {
74  if (!shouldWait) {
75  if (!check()) {
76  last = nullptr;
77  return last;
78  }
79  }
80  bool missed = false;
81  T* prev = last;
82  last = static_cast<T*>(implementation.readBase(missed, false));
83  if (last != nullptr) {
84  if (autoDiscard) {
85  // go up to date
86  while (check()) {
87  // printf("Dropping something\n");
88  bool tmp;
89  last = static_cast<T*>(implementation.readBase(tmp, true));
90  }
91  }
92  }
93  if (missed) {
94  // we've been asked to enforce a period
95  last = prev;
96  if (last == nullptr) {
97  if (default_value == nullptr) {
98  default_value = new T;
99  }
100  last = default_value;
101  }
102  }
103  return last;
104 }
105 
106 template <typename T>
108 {
109  implementation.interrupt();
110 }
111 
112 template <typename T>
114 {
115  return last;
116 }
117 
118 template <typename T>
120 {
121  // port.setReader(*this);
122  implementation.attachBase(port);
123 }
124 
125 template <typename T>
127 {
128  if (reader != nullptr) {
129  reader->stop();
130  delete reader;
131  reader = nullptr;
132  }
133  reader = new TypedReaderThread<T>(*this, callback);
134 }
135 
136 template <typename T>
138 {
139  if (reader != nullptr) {
140  reader->stop();
141  delete reader;
142  reader = nullptr;
143  }
144 }
145 
146 template <typename T>
148 {
149  return implementation.read(connection);
150 }
151 
152 template <typename T>
154 {
155  return new T;
156 }
157 
158 template <typename T>
160 {
161  implementation.setReplier(reader);
162 }
163 
164 template <typename T>
166 {
167  return implementation.getEnvelope(envelope);
168 }
169 
170 template <typename T>
172 {
173  return implementation.isClosed();
174 }
175 
176 template <typename T>
178 {
179  return implementation.getName();
180 }
181 
182 template <typename T>
184 {
185  return implementation.acceptObjectBase(obj, wrapper);
186 }
187 
188 template <typename T>
190 {
191  return implementation.forgetObjectBase(obj, wrapper);
192 }
193 
194 template <typename T>
196 {
197  return implementation.acquire();
198 }
199 
200 template <typename T>
202 {
203  implementation.release(handle);
204 }
205 
206 template <typename T>
208 {
209  implementation.setTargetPeriod(period);
210 }
211 
212 
213 #endif // YARP_OS_PORTREADERBUFFER_INL_H
yarp::os::TypedReaderThread
Definition: TypedReaderThread.h:26
yarp::os::PortReaderBuffer::acquire
void * acquire() override
Take control of the last object read.
Definition: PortReaderBuffer-inl.h:195
yarp::os::PortReaderBuffer::check
bool check()
Check if data is available.
Definition: PortReaderBuffer-inl.h:59
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::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:31
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
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::PortReaderBuffer::getPendingReads
int getPendingReads() override
Check how many messages are waiting to be read.
Definition: PortReaderBuffer-inl.h:65
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
yarp::os::PortReaderBufferBase::setCreator
void setCreator(PortReaderBufferBaseCreator *creator)
Definition: PortReaderBufferBase.cpp:511
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::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
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
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
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