YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Subscriber.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef YARP_OS_SUBSCRIBER_H
7#define YARP_OS_SUBSCRIBER_H
8
11#include <yarp/os/Log.h>
12
13namespace yarp::os {
14
20template <class T>
22{
23public:
25
31 Subscriber(const std::string& name = "")
32 {
33 buffered_port = nullptr;
34 T example;
36 port.setInputMode(true);
37 port.setOutputMode(false);
38 port.setRpcMode(false);
39 if (name != "") {
40 bool ret = topic(name);
41 yAssert(ret);
42 YARP_UNUSED(ret); // FIXME [[maybe-unused]]
43 }
44 isStrict = false;
45 }
46
50 virtual ~Subscriber()
51 {
52 clear();
53 }
54
62 bool topic(const std::string& name)
63 {
64 port.includeNodeInName(true);
65 return open(name);
66 }
67
68 // documentation provided in Contactable
69 bool open(const std::string& name) override
70 {
71 clear();
72 return port.open(name);
73 }
74
75 // documentation provided in Contactable
76 bool open(const Contact& contact, bool registerName = true) override
77 {
78 clear();
79 return port.open(contact, registerName);
80 }
81
82 // documentation provided in Contactable
83 void close() override
84 {
85 active().close();
86 }
87
88 // documentation provided in Contactable
89 void interrupt() override
90 {
91 active().interrupt();
92 }
93
94 // documentation provided in Contactable
95 void resume() override
96 {
97 active().resume();
98 }
99
100 // documented in Contactable
101 void setReader(PortReader& reader) override
102 {
103 active().setReader(reader);
104 }
105
113 T* read(bool shouldWait = true)
114 {
115 return buffer().read(shouldWait);
116 }
117
118 Port& asPort() override
119 {
120 return port;
121 }
122
123 const Port& asPort() const override
124 {
125 return port;
126 }
127
129 void onRead(T& datum) override
130 {
132 // override this to do something
133 }
134
136 {
137 buffer().useCallback(callback);
138 }
139
141 {
142 buffer().useCallback(*this);
143 }
144
146 {
147 buffer().disableCallback();
148 }
149
150 void setStrict(bool strict = true)
151 {
152 isStrict = strict;
153 if (buffered_port) {
154 buffered_port->setStrict(strict);
155 }
156 }
157
158private:
159 bool isStrict;
160 Port port;
161 BufferedPort<T>* buffered_port;
162
163 Contactable& active()
164 {
165 if (buffered_port) {
166 return *buffered_port;
167 }
168 return port;
169 }
170
171 BufferedPort<T>& buffer()
172 {
173 if (!buffered_port) {
174 buffered_port = new BufferedPort<T>(port);
175 if (isStrict) {
176 buffered_port->setStrict(isStrict);
177 }
178 }
179 return *buffered_port;
180 }
181
182 void clear()
183 {
184 if (!buffered_port) {
185 return;
186 }
187 delete buffered_port;
188 buffered_port = nullptr;
189 }
190};
191
192} // namespace yarp::os
193
194#endif // YARP_OS_SUBSCRIBER_H
bool ret
#define yAssert(x)
Definition Log.h:388
A default implementation of an abstract port.
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
A mini-server for performing network communication in the background.
Type getType() override
Get the type of data the port has committed to send/receive.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
An abstract port.
Definition Contactable.h:28
virtual void close()=0
Stop port activity.
virtual void setReader(PortReader &reader)=0
Set an external reader for port data.
virtual void resume()=0
Put the port back in an operative state after interrupt() has been called.
virtual void interrupt()=0
Interrupt any current reads or writes attached to the port.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
A mini-server for network communication.
Definition Port.h:46
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition Port.cpp:626
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition Port.cpp:610
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Definition Port.cpp:672
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition Port.cpp:657
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
Definition Port.cpp:618
A port specialized for reading data of a constant type published on a topic.
Definition Subscriber.h:22
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition Subscriber.h:76
T * read(bool shouldWait=true)
Read a message from the port.
Definition Subscriber.h:113
void close() override
Stop port activity.
Definition Subscriber.h:83
virtual ~Subscriber()
Destructor.
Definition Subscriber.h:50
Port & asPort() override
Get the concrete Port being used for communication.
Definition Subscriber.h:118
void setStrict(bool strict=true)
Definition Subscriber.h:150
bool topic(const std::string &name)
Set topic to subscribe to.
Definition Subscriber.h:62
void onRead(T &datum) override
Callback method.
Definition Subscriber.h:129
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Subscriber.h:69
Subscriber(const std::string &name="")
Constructor.
Definition Subscriber.h:31
void useCallback(TypedReaderCallback< T > &callback)
Definition Subscriber.h:135
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition Subscriber.h:123
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Subscriber.h:101
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Subscriber.h:89
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition Subscriber.h:95
A callback for typed data from a port.
An interface to the operating system, including Port based communication.
#define YARP_UNUSED(var)
Definition api.h:162