Ocean
system/usb/Context.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SYSTEM_USB_CONTEXT_H
9 #define META_OCEAN_SYSTEM_USB_CONTEXT_H
10 
11 #include "ocean/system/usb/USB.h"
12 
13 #include "ocean/base/Lock.h"
14 
15 namespace Ocean
16 {
17 
18 namespace System
19 {
20 
21 namespace USB
22 {
23 
24 // Forward declaration.
25 class Context;
26 
27 /**
28  * Definition of a shared pointer holding a context.
29  * @see Context
30  * @ingroup systemusb
31  */
32 using SharedContext = std::shared_ptr<Context>;
33 
34 /**
35  * This class wraps a libusb context.
36  * @ingroup systemusb
37  */
38 class OCEAN_SYSTEM_USB_EXPORT Context
39 {
40  public:
41 
42  /**
43  * Move constructor.
44  * @param context The context to be moved
45  */
46  inline Context(Context&& context) noexcept;
47 
48  /**
49  * Creates a new custom context.
50  * Commonly there is no need to create a custom context, instead use the Manager's context.
51  * @param noDeviceDiscovery True, to disable device discovery (e.g., on Android platforms devices cannot be discovered but need to be provided explicitly from the Java side); False, to enable/allow device discovery
52  * @param usbDk True, to use the USB-Dk driver on Windows platforms; False, otherwise
53  * @see Manager::context().
54  */
55  explicit Context(const bool noDeviceDiscovery = false, const bool usbDk = false);
56 
57  /**
58  * Destructs and releases the context.
59  */
61 
62  /**
63  * Returns the actual libusb context.
64  * @return The actual libusb context, may be nullptr which indicates the global default context
65  */
66  inline libusb_context* usbContext() const;
67 
68  /**
69  * Explicitly releases the context.
70  */
71  void release();
72 
73  /**
74  * Returns whether this object wraps a valid context (which is not nullptr).
75  * @return True, if so
76  */
77  inline bool isValid() const;
78 
79  /**
80  * Move operator.
81  * @param context The context to be moved
82  * @return Reference to this object
83  */
84  Context& operator=(Context&& context) noexcept;
85 
86  protected:
87 
88  /**
89  * Disabled copy constructor.
90  */
91  Context(const Context&) = delete;
92 
93  /**
94  * Disabled copy operator.
95  * @return Reference to this object
96  */
97  Context& operator=(const Context&) = delete;
98 
99  protected:
100 
101  /// The actual libusb context.
102  libusb_context* usbContext_ = nullptr;
103 
104  /// The context's lock.
105  mutable Lock lock_;
106 };
107 
108 inline Context::Context(Context&& context) noexcept
109 {
110  *this = std::move(context);
111 }
112 
113 inline libusb_context* Context::usbContext() const
114 {
115  return usbContext_;
116 }
117 
118 inline bool Context::isValid() const
119 {
120  const ScopedLock scopedLock(lock_);
121 
122  return usbContext_ != nullptr;
123 }
124 
125 }
126 
127 }
128 
129 }
130 
131 #endif // META_OCEAN_SYSTEM_USB_CONTEXT_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class wraps a libusb context.
Definition: system/usb/Context.h:39
Lock lock_
The context's lock.
Definition: system/usb/Context.h:105
libusb_context * usbContext_
The actual libusb context.
Definition: system/usb/Context.h:102
Context(Context &&context) noexcept
Move constructor.
Definition: system/usb/Context.h:108
Context & operator=(Context &&context) noexcept
Move operator.
Context(const Context &)=delete
Disabled copy constructor.
Context(const bool noDeviceDiscovery=false, const bool usbDk=false)
Creates a new custom context.
Context & operator=(const Context &)=delete
Disabled copy operator.
libusb_context * usbContext() const
Returns the actual libusb context.
Definition: system/usb/Context.h:113
void release()
Explicitly releases the context.
~Context()
Destructs and releases the context.
bool isValid() const
Returns whether this object wraps a valid context (which is not nullptr).
Definition: system/usb/Context.h:118
std::shared_ptr< Context > SharedContext
Definition of a shared pointer holding a context.
Definition: system/usb/Context.h:32
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15