YARP
Yet Another Robot Platform
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 
10 #include <yarp/os/BufferedPort.h>
11 #include <yarp/os/Log.h>
12 
13 namespace yarp {
14 namespace os {
15 
21 template <class T>
23 {
24 public:
26 
32  Subscriber(const std::string& name = "")
33  {
34  buffered_port = nullptr;
35  T example;
36  port.promiseType(example.getType());
37  port.setInputMode(true);
38  port.setOutputMode(false);
39  port.setRpcMode(false);
40  if (name != "") {
41  bool ret = topic(name);
42  yAssert(ret);
43  YARP_UNUSED(ret); // FIXME [[maybe-unused]]
44  }
45  isStrict = false;
46  }
47 
51  virtual ~Subscriber()
52  {
53  clear();
54  }
55 
63  bool topic(const std::string& name)
64  {
65  port.includeNodeInName(true);
66  return open(name);
67  }
68 
69  // documentation provided in Contactable
70  bool open(const std::string& name) override
71  {
72  clear();
73  return port.open(name);
74  }
75 
76  // documentation provided in Contactable
77  bool open(const Contact& contact, bool registerName = true) override
78  {
79  clear();
80  return port.open(contact, registerName);
81  }
82 
83  // documentation provided in Contactable
84  void close() override
85  {
86  active().close();
87  }
88 
89  // documentation provided in Contactable
90  void interrupt() override
91  {
92  active().interrupt();
93  }
94 
95  // documentation provided in Contactable
96  void resume() override
97  {
98  active().resume();
99  }
100 
101  // documented in Contactable
102  void setReader(PortReader& reader) override
103  {
104  active().setReader(reader);
105  }
106 
114  T* read(bool shouldWait = true)
115  {
116  return buffer().read(shouldWait);
117  }
118 
119  Port& asPort() override
120  {
121  return port;
122  }
123 
124  const Port& asPort() const override
125  {
126  return port;
127  }
128 
130  void onRead(T& datum) override
131  {
132  YARP_UNUSED(datum);
133  // override this to do something
134  }
135 
137  {
138  buffer().useCallback(callback);
139  }
140 
141  void useCallback()
142  {
143  buffer().useCallback(*this);
144  }
145 
147  {
148  buffer().disableCallback();
149  }
150 
151  void setStrict(bool strict = true)
152  {
153  isStrict = strict;
154  if (buffered_port) {
155  buffered_port->setStrict(strict);
156  }
157  }
158 
159 private:
160  bool isStrict;
161  Port port;
162  BufferedPort<T>* buffered_port;
163 
164  Contactable& active()
165  {
166  if (buffered_port) {
167  return *buffered_port;
168  }
169  return port;
170  }
171 
172  BufferedPort<T>& buffer()
173  {
174  if (!buffered_port) {
175  buffered_port = new BufferedPort<T>(port);
176  if (isStrict) {
177  buffered_port->setStrict(isStrict);
178  }
179  }
180  return *buffered_port;
181  }
182 
183  void clear()
184  {
185  if (!buffered_port) {
186  return;
187  }
188  delete buffered_port;
189  buffered_port = nullptr;
190  }
191 };
192 
193 } // namespace os
194 } // namespace yarp
195 
196 #endif // YARP_OS_SUBSCRIBER_H
bool ret
#define yAssert(x)
Definition: Log.h:294
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.
Definition: BufferedPort.h:61
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:36
An abstract port.
Definition: Contactable.h:35
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:25
A mini-server for network communication.
Definition: Port.h:47
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: Port.cpp:617
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition: Port.cpp:601
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Definition: Port.cpp:663
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:648
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:609
A port specialized for reading data of a constant type published on a topic.
Definition: Subscriber.h:23
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition: Subscriber.h:77
void close() override
Stop port activity.
Definition: Subscriber.h:84
T * read(bool shouldWait=true)
Read a message from the port.
Definition: Subscriber.h:114
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition: Subscriber.h:124
virtual ~Subscriber()
Destructor.
Definition: Subscriber.h:51
void setStrict(bool strict=true)
Definition: Subscriber.h:151
bool topic(const std::string &name)
Set topic to subscribe to.
Definition: Subscriber.h:63
void onRead(T &datum) override
Callback method.
Definition: Subscriber.h:130
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Subscriber.h:70
Subscriber(const std::string &name="")
Constructor.
Definition: Subscriber.h:32
void useCallback(TypedReaderCallback< T > &callback)
Definition: Subscriber.h:136
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Subscriber.h:102
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Subscriber.h:90
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Subscriber.h:96
Port & asPort() override
Get the concrete Port being used for communication.
Definition: Subscriber.h:119
A callback for typed data from a port.
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_UNUSED(var)
Definition: api.h:162