YARP
Yet Another Robot Platform
Drivers.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_DRIVERS_H
8 #define YARP_DRIVERS_H
9 
10 #include <yarp/os/Network.h>
11 #include <yarp/os/Property.h>
12 #include <yarp/dev/DeviceDriver.h>
13 
14 namespace yarp {
15  namespace dev {
16  template <class T> class DriverCreatorOf;
17  class DriverCreator;
18  class Drivers;
19  class PolyDriver;
20  class StubDriverCreator;
21  }
22 }
23 
29 public:
33  virtual ~DriverCreator() {}
34 
39  virtual std::string toString() const = 0;
40 
44  virtual DeviceDriver *create() const = 0;
45 
50  virtual std::string getName() const = 0;
51 
58  virtual std::string getWrapper() const = 0;
59 
65  virtual std::string getCode() const = 0;
66 
73  virtual PolyDriver *owner() {
74  return nullptr;
75  }
76 };
77 
81 template <class T>
83 private:
84  std::string desc, wrap, code;
85 public:
99  DriverCreatorOf(const char *name, const char *wrap, const char *code) :
100  desc(name), wrap(wrap), code(code)
101  {
102  }
103 
104  std::string toString() const override {
105  return desc;
106  }
107 
108  std::string getName() const override {
109  return desc;
110  }
111 
112  std::string getWrapper() const override {
113  return wrap;
114  }
115 
116  std::string getCode() const override {
117  return code;
118  }
119 
120  DeviceDriver *create() const override {
121  return new T;
122  }
123 };
124 
125 
126 
131 private:
132  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::string) desc, wrap, code, libname, fnname;
133 public:
134  StubDriverCreator(const char *name, const char *wrap, const char *code,
135  const char *libname, const char *fnname) :
136  desc(name), wrap(wrap), code(code), libname(libname), fnname(fnname)
137  {
138  }
139 
140  std::string toString() const override {
141  return desc;
142  }
143 
144  std::string getName() const override {
145  return desc;
146  }
147 
148  std::string getWrapper() const override {
149  return wrap;
150  }
151 
152  std::string getCode() const override {
153  return code;
154  }
155 
156  DeviceDriver *create() const override;
157 };
158 
159 
160 
173 public:
178  static Drivers& factory();
190  DeviceDriver *open(const char *device) {
192  p.put("device",device);
193  return open(p);
194  }
195 
209  virtual DeviceDriver *open(yarp::os::Searchable& config);
210 
215  virtual std::string toString() const;
216 
220  virtual ~Drivers();
221 
228  void add(DriverCreator *creator);
229 
230 
236  DriverCreator *find(const char *name);
237 
243  bool remove(const char *name);
244 
251  static int yarpdev(int argc, char *argv[]);
252 
253 private:
254  Drivers();
255 
256 #ifndef DOXYGEN_SHOULD_SKIP_THIS
257 private:
258  class Private;
259  Private* mPriv;
260 #endif // DOXYGEN_SHOULD_SKIP_THIS
261 };
262 
263 #endif // YARP_DRIVERS_H
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
A factory for creating driver objects of a particular type.
Definition: Drivers.h:82
DriverCreatorOf(const char *name, const char *wrap, const char *code)
Constructor.
Definition: Drivers.h:99
std::string getName() const override
Get the common name of the device.
Definition: Drivers.h:108
std::string getWrapper() const override
Get the common name of a device that can wrap this one.
Definition: Drivers.h:112
std::string toString() const override
Returns a simple description of devices the factory can make.
Definition: Drivers.h:104
std::string getCode() const override
Get the name of the C++ class associated with this device.
Definition: Drivers.h:116
DeviceDriver * create() const override
Create a device.
Definition: Drivers.h:120
A base class for factories that create driver objects.
Definition: Drivers.h:28
virtual std::string getName() const =0
Get the common name of the device.
virtual DeviceDriver * create() const =0
Create a device.
virtual std::string getCode() const =0
Get the name of the C++ class associated with this device.
virtual PolyDriver * owner()
For "links" to other devices.
Definition: Drivers.h:73
virtual std::string getWrapper() const =0
Get the common name of a device that can wrap this one.
virtual ~DriverCreator()
Destructor.
Definition: Drivers.h:33
virtual std::string toString() const =0
Returns a simple description of devices the factory can make.
Global factory for devices.
Definition: Drivers.h:172
DeviceDriver * open(const char *device)
Create and configure a device, by name.
Definition: Drivers.h:190
A container for a device driver.
Definition: PolyDriver.h:24
A factory for creating driver objects from DLLs / shared libraries.
Definition: Drivers.h:130
std::string toString() const override
Returns a simple description of devices the factory can make.
Definition: Drivers.h:140
StubDriverCreator(const char *name, const char *wrap, const char *code, const char *libname, const char *fnname)
Definition: Drivers.h:134
std::string getCode() const override
Get the name of the C++ class associated with this device.
Definition: Drivers.h:152
std::string getName() const override
Get the common name of the device.
Definition: Drivers.h:144
std::string getWrapper() const override
Get the common name of a device that can wrap this one.
Definition: Drivers.h:148
A class for storing options and configuration information.
Definition: Property.h:34
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
A base class for nested structures that can be searched.
Definition: Searchable.h:66
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:336
#define YARP_dev_API
Definition: api.h:18