yarp-devices
RawDevice.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __RAW_DEVICE_HPP__
4 #define __RAW_DEVICE_HPP__
5 
6 #include <memory>
7 #include <string>
8 
9 #include <yarp/dev/PolyDriver.h>
10 
11 namespace roboticslab
12 {
13 
21 class RawDevice final
22 {
23 public:
25  explicit RawDevice(yarp::dev::PolyDriver * driver);
26 
29 
34  template<typename T>
35  T * getHandle() const;
36 
41  std::string getId() const
42  { return valid ? driver->getImplementation()->id() : ""; }
43 
48  template<typename T>
49  T * castToType() const
50  { return valid ? dynamic_cast<T *>(driver->getImplementation()) : nullptr; }
51 
56  bool isValid() const
57  { return valid; }
58 
59 private:
60  class Private;
61  std::unique_ptr<Private> priv;
62  yarp::dev::PolyDriver * driver {nullptr};
63  bool valid {false};
64 };
65 
67 inline const RawDevice invalidDevice(nullptr);
68 
69 } // namespace roboticslab
70 
71 #endif // __RAW_DEVICE_HPP__
Immutable container for YARP raw interface handles.
Definition: RawDevice.hpp:22
~RawDevice()
Destructor.
RawDevice(yarp::dev::PolyDriver *driver)
Constructor, extracts all interface handles of the supplied driver.
Definition: RawDevice.cpp:47
std::string getId() const
Retrieve the device id (can be an empty string if not set).
Definition: RawDevice.hpp:41
T * getHandle() const
Retrieve a handle to a raw interface implemented by the device.
T * castToType() const
Perform a dynamic cast on the given type.
Definition: RawDevice.hpp:49
bool isValid() const
Whether this instance wraps a device with a supported interface.
Definition: RawDevice.hpp:56
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6
const RawDevice invalidDevice(nullptr)
Singleton instance for an invalid (empty) raw device.