scoped_libusb_device_ref.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_USB_SCOPED_LIBUSB_DEVICE_REF_H_
  5. #define SERVICES_DEVICE_USB_SCOPED_LIBUSB_DEVICE_REF_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/scoped_refptr.h"
  8. struct libusb_device;
  9. namespace device {
  10. class UsbContext;
  11. // This class owns a reference to a libusb_device as well as a reference to
  12. // the libusb_context. The libusb_context must outlive any libusb_device
  13. // instances created from it.
  14. class ScopedLibusbDeviceRef {
  15. public:
  16. ScopedLibusbDeviceRef(libusb_device* device,
  17. scoped_refptr<UsbContext> context);
  18. ScopedLibusbDeviceRef(ScopedLibusbDeviceRef&& other);
  19. ScopedLibusbDeviceRef(const ScopedLibusbDeviceRef&) = delete;
  20. ScopedLibusbDeviceRef& operator=(const ScopedLibusbDeviceRef&) = delete;
  21. ~ScopedLibusbDeviceRef();
  22. libusb_device* get() const { return device_; }
  23. scoped_refptr<UsbContext> GetContext() const { return context_; }
  24. void Reset();
  25. bool IsValid() const;
  26. private:
  27. raw_ptr<libusb_device> device_;
  28. scoped_refptr<UsbContext> context_;
  29. };
  30. bool operator==(const ScopedLibusbDeviceRef& ref, libusb_device* device);
  31. } // namespace device
  32. #endif // SERVICES_DEVICE_USB_SCOPED_LIBUSB_DEVICE_REF_H_