YARP
Yet Another Robot Platform
Things.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_THINGS_H
7 #define YARP_OS_THINGS_H
8 
10 #include <yarp/os/Portable.h>
11 
12 namespace yarp {
13 namespace os {
14 
19 {
20 public:
21  Things();
22 
23  virtual ~Things();
24 
28  void setPortWriter(yarp::os::PortWriter* writer);
29 
30  yarp::os::PortWriter* getPortWriter();
31 
35  void setPortReader(yarp::os::PortReader* reader);
36 
37  yarp::os::PortReader* getPortReader();
38 
42  bool setConnectionReader(yarp::os::ConnectionReader& reader);
43 
44  /*
45  * Things writer
46  */
47  bool write(yarp::os::ConnectionWriter& connection);
48 
49  void reset();
50 
51  bool hasBeenRead();
52 
53  template <typename T>
54  T* cast_as()
55  {
56  if (this->writer) {
57  return dynamic_cast<T*>(this->writer);
58  }
59  if (this->reader) {
60  return dynamic_cast<T*>(this->reader);
61  }
62  if (!this->portable) {
63  if (!this->conReader) {
64  return nullptr;
65  }
66  this->portable = new T();
67  if (!this->portable->read(*this->conReader)) {
68  delete this->portable;
69  this->portable = nullptr;
70  return nullptr;
71  }
72  beenRead = true;
73  }
74  return dynamic_cast<T*>(this->portable);
75  }
76 
77 private:
78  bool beenRead;
79  yarp::os::ConnectionReader* conReader;
80  yarp::os::PortWriter* writer;
81  yarp::os::PortReader* reader;
82  yarp::os::Portable* portable;
83 };
84 
85 } // namespace os
86 } // namespace yarp
87 
88 #endif // YARP_OS_THINGS_H
An interface for reading from a network connection.
An interface for writing to a network connection.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
Base class for generic things.
Definition: Things.h:19
T * cast_as()
Definition: Things.h:54
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1098
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_os_API
Definition: api.h:18