YARP
Yet Another Robot Platform
Drivers.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_DRIVERS_H
11 #define YARP_DRIVERS_H
12 
13 #include <yarp/os/Network.h>
14 #include <yarp/os/Property.h>
15 #include <yarp/dev/DeviceDriver.h>
16 
17 namespace yarp {
18  namespace dev {
19  template <class T> class DriverCreatorOf;
20  class DriverCreator;
21  class Drivers;
22  class PolyDriver;
23  class StubDriverCreator;
24  }
25 }
26 
32 public:
36  virtual ~DriverCreator() {}
37 
42  virtual std::string toString() const = 0;
43 
47  virtual DeviceDriver *create() const = 0;
48 
53  virtual std::string getName() const = 0;
54 
61  virtual std::string getWrapper() const = 0;
62 
68  virtual std::string getCode() const = 0;
69 
76  virtual PolyDriver *owner() {
77  return nullptr;
78  }
79 };
80 
84 template <class T>
86 private:
87  std::string desc, wrap, code;
88 public:
102  DriverCreatorOf(const char *name, const char *wrap, const char *code) :
103  desc(name), wrap(wrap), code(code)
104  {
105  }
106 
107  std::string toString() const override {
108  return desc;
109  }
110 
111  std::string getName() const override {
112  return desc;
113  }
114 
115  std::string getWrapper() const override {
116  return wrap;
117  }
118 
119  std::string getCode() const override {
120  return code;
121  }
122 
123  DeviceDriver *create() const override {
124  return new T;
125  }
126 };
127 
128 
129 
134 private:
135  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::string) desc, wrap, code, libname, fnname;
136 public:
137  StubDriverCreator(const char *name, const char *wrap, const char *code,
138  const char *libname, const char *fnname) :
139  desc(name), wrap(wrap), code(code), libname(libname), fnname(fnname)
140  {
141  }
142 
143  std::string toString() const override {
144  return desc;
145  }
146 
147  std::string getName() const override {
148  return desc;
149  }
150 
151  std::string getWrapper() const override {
152  return wrap;
153  }
154 
155  std::string getCode() const override {
156  return code;
157  }
158 
159  DeviceDriver *create() const override;
160 };
161 
162 
163 
176 public:
181  static Drivers& factory();
193  DeviceDriver *open(const char *device) {
195  p.put("device",device);
196  return open(p);
197  }
198 
212  virtual DeviceDriver *open(yarp::os::Searchable& config);
213 
218  virtual std::string toString() const;
219 
223  virtual ~Drivers();
224 
231  void add(DriverCreator *creator);
232 
233 
239  DriverCreator *find(const char *name);
240 
246  bool remove(const char *name);
247 
254  static int yarpdev(int argc, char *argv[]);
255 
256 private:
257  Drivers();
258 
259 #ifndef DOXYGEN_SHOULD_SKIP_THIS
260 private:
261  class Private;
262  Private* mPriv;
263 #endif // DOXYGEN_SHOULD_SKIP_THIS
264 };
265 
266 #endif // YARP_DRIVERS_H
yarp::dev::DriverCreatorOf::getCode
std::string getCode() const override
Definition: Drivers.h:119
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
Network.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::dev::DriverCreator::owner
virtual PolyDriver * owner()
For "links" to other devices.
Definition: Drivers.h:76
yarp::dev::DriverCreator::getWrapper
virtual std::string getWrapper() const =0
Get the common name of a device that can wrap this one.
yarp::dev::StubDriverCreator::toString
std::string toString() const override
Returns a simple description of devices the factory can make.
Definition: Drivers.h:143
yarp::dev::DriverCreatorOf
A factory for creating driver objects of a particular type.
Definition: Drivers.h:85
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
yarp::dev::StubDriverCreator::getWrapper
std::string getWrapper() const override
Get the common name of a device that can wrap this one.
Definition: Drivers.h:151
YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:339
yarp::dev::Drivers::factory
static Drivers & factory()
Get the global factory for devices.
Definition: Drivers.cpp:252
yarp::dev::DriverCreatorOf::toString
std::string toString() const override
Definition: Drivers.h:107
yarp::dev::Drivers::~Drivers
virtual ~Drivers()
Destructor.
Definition: Drivers.cpp:263
yarp::dev::StubDriverCreator::getName
std::string getName() const override
Get the common name of the device.
Definition: Drivers.h:147
yarp::dev::DriverCreatorOf::getWrapper
std::string getWrapper() const override
Definition: Drivers.h:115
yarp::dev::DriverCreator::create
virtual DeviceDriver * create() const =0
Create a device.
Property.h
yarp::dev::DriverCreator::toString
virtual std::string toString() const =0
Returns a simple description of devices the factory can make.
yarp::dev::DriverCreatorOf::getName
std::string getName() const override
Definition: Drivers.h:111
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
yarp::dev::DriverCreator
A base class for factories that create driver objects.
Definition: Drivers.h:31
yarp::dev::DriverCreatorOf::DriverCreatorOf
DriverCreatorOf(const char *name, const char *wrap, const char *code)
Constructor.
Definition: Drivers.h:102
yarp::dev::StubDriverCreator::getCode
std::string getCode() const override
Get the name of the C++ class associated with this device.
Definition: Drivers.h:155
yarp::dev::Drivers::toString
virtual std::string toString() const
A description of the available devices.
Definition: Drivers.cpp:267
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
yarp::dev::Drivers::find
DriverCreator * find(const char *name)
Find the factory for a named device.
Definition: Drivers.cpp:276
yarp::dev::DriverCreator::~DriverCreator
virtual ~DriverCreator()
Destructor.
Definition: Drivers.h:36
yarp::dev::DriverCreator::getCode
virtual std::string getCode() const =0
Get the name of the C++ class associated with this device.
yarp::dev::StubDriverCreator::StubDriverCreator
StubDriverCreator(const char *name, const char *wrap, const char *code, const char *libname, const char *fnname)
Definition: Drivers.h:137
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::dev::Drivers::remove
bool remove(const char *name)
Remove a factory for a named device.
Definition: Drivers.cpp:280
yarp::dev::Drivers::open
DeviceDriver * open(const char *device)
Create and configure a device, by name.
Definition: Drivers.h:193
yarp::dev::Drivers::add
void add(DriverCreator *creator)
Add a factory for creating a particular device.
Definition: Drivers.cpp:271
yarp::dev::Drivers
Global factory for devices.
Definition: Drivers.h:175
yarp::dev::StubDriverCreator
A factory for creating driver objects from DLLs / shared libraries.
Definition: Drivers.h:133
yarp::dev::DriverCreatorOf::create
DeviceDriver * create() const override
Definition: Drivers.h:123
yarp::dev::Drivers::yarpdev
static int yarpdev(int argc, char *argv[])
Body of the yarpdev program for starting device wrappers.
Definition: Drivers.cpp:407
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
yarp::dev::DriverCreator::getName
virtual std::string getName() const =0
Get the common name of the device.
DeviceDriver.h