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
11#include <yarp/os/Log.h>
12
13namespace yarp::os {
14
20template <class T>
22{
23public:
25
33 Publisher(const std::string& name = "")
34 {
35 buffered_port = nullptr;
36 T example;
37 port.promiseType(example.getType());
38 port.setInputMode(false);
39 port.setOutputMode(true);
40 port.setRpcMode(false);
41 if (name != "") {
42 bool ret = topic(name);
43 yAssert(ret);
44 YARP_UNUSED(ret); // FIXME [[maybe-unused]]
45 }
46 }
47
51 virtual ~Publisher()
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
124 {
125 return buffer().prepare();
126 }
127
134 {
135 return buffer().unprepare();
136 }
137
148 void write(bool forceStrict = false)
149 {
150 buffer().write(forceStrict);
151 }
152
157 {
158 buffer().waitForWrite();
159 }
160
161 virtual int getPendingReads()
162 {
163 if (buffered_port) {
164 return buffered_port->getPendingReads();
165 }
166 return 0;
167 }
168
169 Port& asPort() override
170 {
171 return port;
172 }
173
174 const Port& asPort() const override
175 {
176 return port;
177 }
178
179private:
180 Port port;
181 BufferedPort<T>* buffered_port;
182
183 Contactable& active()
184 {
185 if (buffered_port) {
186 return *buffered_port;
187 }
188 return port;
189 }
190
191 BufferedPort<T>& buffer()
192 {
193 if (!buffered_port) {
194 buffered_port = new BufferedPort<T>(port);
195 }
196 return *buffered_port;
197 }
198
199 void clear()
200 {
201 if (!buffered_port) {
202 return;
203 }
204 delete buffered_port;
205 buffered_port = nullptr;
206 }
207};
208
209} // namespace yarp::os
210
211#endif // YARP_OS_PUBLISHER_H
bool ret
#define yAssert(x)
Definition: Log.h:383
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:60
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 publishing data of a constant type on a topic.
Definition: Publisher.h:22
void close() override
Stop port activity.
Definition: Publisher.h:84
virtual int getPendingReads()
Definition: Publisher.h:161
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Publisher.h:96
bool topic(const std::string &name)
Set topic to publish to.
Definition: Publisher.h:63
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Publisher.h:102
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition: Publisher.h:77
Publisher(const std::string &name="")
Constructor.
Definition: Publisher.h:33
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition: Publisher.h:174
virtual ~Publisher()
Destructor.
Definition: Publisher.h:51
Port & asPort() override
Get the concrete Port being used for communication.
Definition: Publisher.h:169
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: Publisher.h:133
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Publisher.h:90
void write(bool forceStrict=false)
Write the current object being returned by Publisher::prepare.
Definition: Publisher.h:148
void waitForWrite()
Wait for any pending writes to complete.
Definition: Publisher.h:156
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::Publisher::write.
Definition: Publisher.h:123
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Publisher.h:70
An interface to the operating system, including Port based communication.
#define YARP_UNUSED(var)
Definition: api.h:162