co_usb
Loading...
Searching...
No Matches
device_ref.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <libusb.h>
10
11namespace co_usb
12{
13
25{
29 device_ref () noexcept : m_dev(nullptr)
30 {
31 }
32
36 explicit device_ref (libusb_device *dev) noexcept : m_dev(dev)
37 {
38 libusb_ref_device(m_dev);
39 }
40
41 ~device_ref () noexcept
42 {
43 if (m_dev)
44 {
45 libusb_unref_device(m_dev);
46 }
47 }
48
49 device_ref (device_ref const &other) noexcept : m_dev(other.m_dev)
50 {
51 if (m_dev)
52 libusb_ref_device(m_dev);
53 }
54
55 device_ref &operator=(device_ref const &other) noexcept
56 {
57 m_dev = other.m_dev;
58 if (m_dev)
59 libusb_ref_device(m_dev);
60 return *this;
61 }
62
68 auto valid () const noexcept -> bool
69 {
70 return m_dev != nullptr;
71 }
72
76 auto get () const noexcept -> libusb_device *
77 {
78 return m_dev;
79 }
80
89 auto release () noexcept -> libusb_device *
90 {
91 auto tmp = m_dev;
92 m_dev = nullptr;
93 return tmp;
94 }
95
96 private:
97 libusb_device *m_dev;
98};
99
100} // namespace co_usb
Definition flag_type.hpp:6
Wrapper for nullable libusb_device that increments ref count on ctor and decrements on dtor.
Definition device_ref.hpp:25
device_ref() noexcept
constructs a null ref
Definition device_ref.hpp:29
device_ref(device_ref const &other) noexcept
Definition device_ref.hpp:49
device_ref(libusb_device *dev) noexcept
constructs a new ref object and increments ref count of underlying device
Definition device_ref.hpp:36
auto get() const noexcept -> libusb_device *
Definition device_ref.hpp:76
auto release() noexcept -> libusb_device *
Releases the device from a ref.
Definition device_ref.hpp:89
device_ref & operator=(device_ref const &other) noexcept
Definition device_ref.hpp:55
~device_ref() noexcept
Definition device_ref.hpp:41
auto valid() const noexcept -> bool
Checks if a device reference is valid.
Definition device_ref.hpp:68