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