YARP
Yet Another Robot Platform
Publisher.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_PUBLISHER_H
7 #define YARP_OS_PUBLISHER_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 
34  Publisher(const std::string& name = "")
35  {
36  buffered_port = nullptr;
37  T example;
38  port.promiseType(example.getType());
39  port.setInputMode(false);
40  port.setOutputMode(true);
41  port.setRpcMode(false);
42  if (name != "") {
43  bool ret = topic(name);
44  yAssert(ret);
45  YARP_UNUSED(ret); // FIXME [[maybe-unused]]
46  }
47  }
48 
52  virtual ~Publisher()
53  {
54  clear();
55  }
56 
64  bool topic(const std::string& name)
65  {
66  port.includeNodeInName(true);
67  return open(name);
68  }
69 
70  // documentation provided in Contactable
71  bool open(const std::string& name) override
72  {
73  clear();
74  return port.open(name);
75  }
76 
77  // documentation provided in Contactable
78  bool open(const Contact& contact, bool registerName = true) override
79  {
80  clear();
81  return port.open(contact, registerName);
82  }
83 
84  // documentation provided in Contactable
85  void close() override
86  {
87  active().close();
88  }
89 
90  // documentation provided in Contactable
91  void interrupt() override
92  {
93  active().interrupt();
94  }
95 
96  // documentation provided in Contactable
97  void resume() override
98  {
99  active().resume();
100  }
101 
102  // documented in Contactable
103  void setReader(PortReader& reader) override
104  {
105  active().setReader(reader);
106  }
107 
124  T& prepare()
125  {
126  return buffer().prepare();
127  }
128 
134  bool unprepare()
135  {
136  return buffer().unprepare();
137  }
138 
149  void write(bool forceStrict = false)
150  {
151  buffer().write(forceStrict);
152  }
153 
158  {
159  buffer().waitForWrite();
160  }
161 
162  virtual int getPendingReads()
163  {
164  if (buffered_port) {
165  return buffered_port->getPendingReads();
166  }
167  return 0;
168  }
169 
170  Port& asPort() override
171  {
172  return port;
173  }
174 
175  const Port& asPort() const override
176  {
177  return port;
178  }
179 
180 private:
181  Port port;
182  BufferedPort<T>* buffered_port;
183 
184  Contactable& active()
185  {
186  if (buffered_port) {
187  return *buffered_port;
188  }
189  return port;
190  }
191 
192  BufferedPort<T>& buffer()
193  {
194  if (!buffered_port) {
195  buffered_port = new BufferedPort<T>(port);
196  }
197  return *buffered_port;
198  }
199 
200  void clear()
201  {
202  if (!buffered_port) {
203  return;
204  }
205  delete buffered_port;
206  buffered_port = nullptr;
207  }
208 };
209 
210 } // namespace os
211 } // namespace yarp
212 
213 #endif // YARP_OS_PUBLISHER_H
bool ret
#define yAssert(x)
Definition: Log.h:294
A default implementation of an abstract port.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:61
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 publishing data of a constant type on a topic.
Definition: Publisher.h:23
void close() override
Stop port activity.
Definition: Publisher.h:85
virtual int getPendingReads()
Definition: Publisher.h:162
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Publisher.h:97
bool topic(const std::string &name)
Set topic to publish to.
Definition: Publisher.h:64
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Publisher.h:103
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition: Publisher.h:78
Publisher(const std::string &name="")
Constructor.
Definition: Publisher.h:34
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::Publisher::write.
Definition: Publisher.h:124
virtual ~Publisher()
Destructor.
Definition: Publisher.h:52
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition: Publisher.h:175
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: Publisher.h:134
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Publisher.h:91
void write(bool forceStrict=false)
Write the current object being returned by Publisher::prepare.
Definition: Publisher.h:149
Port & asPort() override
Get the concrete Port being used for communication.
Definition: Publisher.h:170
void waitForWrite()
Wait for any pending writes to complete.
Definition: Publisher.h:157
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Publisher.h:71
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_UNUSED(var)
Definition: api.h:162