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 {
22  namespace dev {
23  class DeviceDriver;
24  class DeprecatedDeviceDriver;
25  class DeviceResponder;
26  }
27 }
28 
35 {
36 public:
40  ~DeviceDriver() override = default;
41 
55  bool open(yarp::os::Searchable& config) override { YARP_UNUSED(config); return true; }
56 
61  bool close() override { return true; }
62 
63 
73  template <class T>
74  bool view(T *&x) {
75  x = nullptr;
76 
77  // This is not super-portable; and it requires RTTI compiled
78  // in. For systems on which this is a problem, suggest:
79  // either replace it with a regular cast (and warn user) or
80  // implement own method for checking interface support.
81  T *v = dynamic_cast<T *>(getImplementation());
82 
83  if (v != nullptr) {
84  x = v;
85  return true;
86  }
87 
88  return false;
89  }
90 
99  return this;
100  }
101 };
102 
116 {
117 };
118 
119 
130  public yarp::os::TypedReaderCallback<yarp::os::Bottle>
131 {
132 
133 private:
134  yarp::os::Bottle examples;
135  yarp::os::Bottle explains;
136  yarp::os::Bottle details;
137 
138 public:
142  DeviceResponder();
143 
150  void addUsage(const char *txt, const char *explain = nullptr);
151 
158  void addUsage(const yarp::os::Bottle& bot, const char *explain = nullptr);
159 
166  virtual bool respond(const yarp::os::Bottle& command,
167  yarp::os::Bottle& reply);
168 
175  bool read(yarp::os::ConnectionReader& connection) override;
176 
184  void onRead(yarp::os::Bottle& v) override {
185  yarp::os::Bottle reply;
186  respond(v,reply);
187  }
188 
192  void makeUsage();
193 
200  source.useCallback(*this);
201  source.setReplier(*this);
202  }
203 };
204 
205 
206 #endif // YARP_DEV_DEVICEDRIVER_H
Interface implemented by deprecated device drivers.
Definition: DeviceDriver.h:116
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
virtual DeviceDriver * getImplementation()
Some drivers are bureaucrats, pointing at others.
Definition: DeviceDriver.h:98
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: DeviceDriver.h:55
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
bool close() override
Close the DeviceDriver.
Definition: DeviceDriver.h:61
~DeviceDriver() override=default
Destructor.
A cheap and cheerful framework for human readable/writable forms of messages to devices.
Definition: DeviceDriver.h:131
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:184
void attach(yarp::os::TypedReader< yarp::os::Bottle > &source)
Attach this object to a source of messages.
Definition: DeviceDriver.h:199
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
An interface for reading from a network connection.
An object that can be configured.
Definition: IConfig.h:24
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
A base class for nested structures that can be searched.
Definition: Searchable.h:66
A callback for typed data from a port.
A base class for sources of typed data.
Definition: TypedReader.h:23
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.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:923
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