YARP
Yet Another Robot Platform
DeviceDriver.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_DEV_DEVICEDRIVER_H
8 #define YARP_DEV_DEVICEDRIVER_H
9 
12 #include <yarp/os/BufferedPort.h>
13 #include <yarp/os/Property.h>
14 #include <yarp/os/IConfig.h>
15 
16 #include <yarp/dev/api.h>
17 
18 namespace yarp::dev {
19 class DeviceDriver;
20 class DeprecatedDeviceDriver;
21 class DeviceResponder;
22 }
23 
30 {
31 public:
32  DeviceDriver();
33  DeviceDriver(const DeviceDriver& other) = delete;
34  DeviceDriver(DeviceDriver&& other) noexcept = delete;
35  DeviceDriver& operator=(const DeviceDriver& other) = delete;
36  DeviceDriver& operator=(DeviceDriver&& other) noexcept = delete;
37 
38  ~DeviceDriver() override;
39 
53  bool open(yarp::os::Searchable& config) override { YARP_UNUSED(config); return true; }
54 
59  bool close() override { return true; }
60 
61 
71  virtual std::string id() const;
72 
76  virtual void setId(const std::string& id);
77 
87  template <class T>
88  bool view(T *&x) {
89  x = nullptr;
90 
91  // This is not super-portable; and it requires RTTI compiled
92  // in. For systems on which this is a problem, suggest:
93  // either replace it with a regular cast (and warn user) or
94  // implement own method for checking interface support.
95  T *v = dynamic_cast<T *>(getImplementation());
96 
97  if (v != nullptr) {
98  x = v;
99  return true;
100  }
101 
102  return false;
103  }
104 
113  return this;
114  }
115 
116 #ifndef DOXYGEN_SHOULD_SKIP_THIS
117 private:
118  class Private;
119  Private* mPriv = nullptr;
120 #endif
121 };
122 
136 {
137 };
138 
139 
150  public yarp::os::TypedReaderCallback<yarp::os::Bottle>
151 {
152 
153 private:
154  yarp::os::Bottle examples;
155  yarp::os::Bottle explains;
156  yarp::os::Bottle details;
157 
158 public:
162  DeviceResponder();
163 
170  void addUsage(const char *txt, const char *explain = nullptr);
171 
178  void addUsage(const yarp::os::Bottle& bot, const char *explain = nullptr);
179 
186  virtual bool respond(const yarp::os::Bottle& command,
187  yarp::os::Bottle& reply);
188 
195  bool read(yarp::os::ConnectionReader& connection) override;
196 
204  void onRead(yarp::os::Bottle& v) override {
205  yarp::os::Bottle reply;
206  respond(v,reply);
207  }
208 
212  void makeUsage();
213 
220  source.useCallback(*this);
221  source.setReplier(*this);
222  }
223 };
224 
225 
226 #endif // YARP_DEV_DEVICEDRIVER_H
Interface implemented by deprecated device drivers.
Definition: DeviceDriver.h:136
Interface implemented by all device drivers.
Definition: DeviceDriver.h:30
virtual void setId(const std::string &id)
Set the id for this device.
virtual DeviceDriver * getImplementation()
Some drivers are bureaucrats, pointing at others.
Definition: DeviceDriver.h:112
virtual std::string id() const
Return the id assigned to the PolyDriver.
DeviceDriver(const DeviceDriver &other)=delete
DeviceDriver & operator=(DeviceDriver &&other) noexcept=delete
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: DeviceDriver.h:53
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
bool close() override
Close the DeviceDriver.
Definition: DeviceDriver.h:59
DeviceDriver & operator=(const DeviceDriver &other)=delete
DeviceDriver(DeviceDriver &&other) noexcept=delete
A cheap and cheerful framework for human readable/writable forms of messages to devices.
Definition: DeviceDriver.h:151
void onRead(yarp::os::Bottle &v) override
Alternative handler for reading messages from the network, and passing them on to the respond() metho...
Definition: DeviceDriver.h:204
void attach(yarp::os::TypedReader< yarp::os::Bottle > &source)
Attach this object to a source of messages.
Definition: DeviceDriver.h:219
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
An interface for reading from a network connection.
An object that can be configured.
Definition: IConfig.h:23
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
A base class for nested structures that can be searched.
Definition: Searchable.h:63
A callback for typed data from a port.
A base class for sources of typed data.
Definition: TypedReader.h:22
virtual void setReplier(PortReader &reader)=0
If a message is received that requires a reply, use this handler.
virtual void useCallback(TypedReaderCallback< T > &callback)=0
Set an object whose onRead method will be called when data is available.
For streams capable of holding different kinds of content, check what they actually have.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:915
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_UNUSED(var)
Definition: api.h:162
#define YARP_dev_API
Definition: api.h:18