YARP
Yet Another Robot Platform
Publisher.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_PUBLISHER_H
10 #define YARP_OS_PUBLISHER_H
11 
13 #include <yarp/os/BufferedPort.h>
14 #include <yarp/os/Log.h>
15 
16 namespace yarp {
17 namespace os {
18 
24 template <class T>
26 {
27 public:
29 
37  Publisher(const std::string& name = "")
38  {
39  buffered_port = nullptr;
40  T example;
41  port.promiseType(example.getType());
42  port.setInputMode(false);
43  port.setOutputMode(true);
44  port.setRpcMode(false);
45  if (name != "") {
46  bool ret = topic(name);
47  yAssert(ret);
48  YARP_UNUSED(ret); // FIXME [[maybe-unused]]
49  }
50  }
51 
55  virtual ~Publisher()
56  {
57  clear();
58  }
59 
67  bool topic(const std::string& name)
68  {
69  port.includeNodeInName(true);
70  return open(name);
71  }
72 
73  // documentation provided in Contactable
74  bool open(const std::string& name) override
75  {
76  clear();
77  return port.open(name);
78  }
79 
80  // documentation provided in Contactable
81  bool open(const Contact& contact, bool registerName = true) override
82  {
83  clear();
84  return port.open(contact, registerName);
85  }
86 
87  // documentation provided in Contactable
88  void close() override
89  {
90  active().close();
91  }
92 
93  // documentation provided in Contactable
94  void interrupt() override
95  {
96  active().interrupt();
97  }
98 
99  // documentation provided in Contactable
100  void resume() override
101  {
102  active().resume();
103  }
104 
105  // documented in Contactable
106  void setReader(PortReader& reader) override
107  {
108  active().setReader(reader);
109  }
110 
127  T& prepare()
128  {
129  return buffer().prepare();
130  }
131 
137  bool unprepare()
138  {
139  return buffer().unprepare();
140  }
141 
152  void write(bool forceStrict = false)
153  {
154  buffer().write(forceStrict);
155  }
156 
161  {
162  buffer().waitForWrite();
163  }
164 
165  virtual int getPendingReads()
166  {
167  if (buffered_port)
168  return buffered_port->getPendingReads();
169  return 0;
170  }
171 
172  Port& asPort() override
173  {
174  return port;
175  }
176 
177  const Port& asPort() const override
178  {
179  return port;
180  }
181 
182 private:
183  Port port;
184  BufferedPort<T>* buffered_port;
185 
186  Contactable& active()
187  {
188  if (buffered_port)
189  return *buffered_port;
190  return port;
191  }
192 
193  BufferedPort<T>& buffer()
194  {
195  if (!buffered_port) {
196  buffered_port = new BufferedPort<T>(port);
197  }
198  return *buffered_port;
199  }
200 
201  void clear()
202  {
203  if (!buffered_port)
204  return;
205  delete buffered_port;
206  buffered_port = nullptr;
207  }
208 };
209 
210 } // namespace os
211 } // namespace yarp
212 
213 #endif // YARP_OS_PUBLISHER_H
yarp::os::Publisher::close
void close() override
Stop port activity.
Definition: Publisher.h:88
yarp::os::Publisher::setReader
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Publisher.h:106
yarp::os::Port::includeNodeInName
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Definition: Port.cpp:666
yarp::os::Publisher::open
bool open(const Contact &contact, bool registerName=true) override
Start port operation with user-chosen network parameters.
Definition: Publisher.h:81
yarp::os::Publisher::interrupt
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Publisher.h:94
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:82
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::os::Publisher
A port specialized for publishing data of a constant type on a topic.
Definition: Publisher.h:26
yarp::os::Contactable::interrupt
virtual void interrupt()=0
Interrupt any current reads or writes attached to the port.
yarp::os::Publisher::~Publisher
virtual ~Publisher()
Destructor.
Definition: Publisher.h:55
yarp::os::Publisher::resume
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Publisher.h:100
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
Log.h
yarp::os::Publisher::topic
bool topic(const std::string &name)
Set topic to publish to.
Definition: Publisher.h:67
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::Publisher::write
void write(bool forceStrict=false)
Write the current object being returned by Publisher::prepare.
Definition: Publisher.h:152
yarp::os::Publisher::asPort
const Port & asPort() const override
Get the concrete Port being used for communication, const version.
Definition: Publisher.h:177
yarp::os::Publisher::Publisher
Publisher(const std::string &name="")
Constructor.
Definition: Publisher.h:37
yarp::os::Contactable::setReader
virtual void setReader(PortReader &reader)=0
Set an external reader for port data.
buffer
Definition: V4L_camera.h:75
yarp::os::Contactable::resume
virtual void resume()=0
Put the port back in an operative state after interrupt() has been called.
AbstractContactable.h
BufferedPort.h
yarp::os::Publisher::asPort
Port & asPort() override
Get the concrete Port being used for communication.
Definition: Publisher.h:172
yarp::os::Publisher::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Publisher.h:74
yarp::os::Port::setRpcMode
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: Port.cpp:620
yarp::os::Publisher::getPendingReads
virtual int getPendingReads()
Definition: Publisher.h:165
yarp::os::Publisher::waitForWrite
void waitForWrite()
Wait for any pending writes to complete.
Definition: Publisher.h:160
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Port::setOutputMode
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
Definition: Port.cpp:612
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::Publisher::unprepare
bool unprepare()
Give the last prepared object back to YARP without writing it.
Definition: Publisher.h:137
yarp::os::Contactable::close
virtual void close()=0
Stop port activity.
yarp::os::Port::setInputMode
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition: Port.cpp:604
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::os::Port::promiseType
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:651
yarp::os::Publisher::prepare
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::Publisher::write.
Definition: Publisher.h:127
yarp::os::AbstractContactable::write
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: AbstractContactable.cpp:149
yarp::os::Contactable
An abstract port.
Definition: Contactable.h:38
yarp::os::AbstractContactable
A default implementation of an abstract port.
Definition: AbstractContactable.h:28