Ocean
Loading...
Searching...
No Matches
DeviceRef.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_DEVICES_REF_H
9#define META_OCEAN_DEVICES_REF_H
10
13
15
16namespace Ocean
17{
18
19namespace Devices
20{
21
22/**
23 * This class implements a device reference with an internal reference counter.
24 * @see Device
25 * @ingroup devices
26 */
28
29/**
30 * This class implements a smart device reference.
31 * @tparam T Type of the derived object that has to be encapsulated by the smart reference object
32 * @ingroup devices
33 */
34template <typename T>
35class SmartDeviceRef : public SmartObjectRef<T, Device>
36{
37 private:
38
39 /**
40 * Redefinition of the release callback function defined in ObjectRef.
41 */
43
44 public:
45
46 /**
47 * Creates an empty smart device reference.
48 */
50
51 /**
52 * Creates a new smart device reference by a given device reference.
53 * @param deviceRef Device reference to copy
54 */
55 SmartDeviceRef(const DeviceRef& deviceRef);
56
57 /**
58 * Creates a new SmartDeviceRef by a given object.
59 * This given object will be released by the smart device reference itself.
60 * @param object Internal object
61 */
62 explicit SmartDeviceRef(Device* object);
63
64 /**
65 * Copies a smart device reference.
66 * @param reference Reference to copy
67 */
68 template <typename T2> SmartDeviceRef(const SmartDeviceRef<T2>& reference);
69
70 /**
71 * Assigns a smart device reference.
72 * @param deviceRef reference Reference to assign
73 * @return Reference to this object
74 */
76};
77
78/**
79 * This class implements a manager for device references.
80 * @ingroup devices
81 */
82class OCEAN_DEVICES_EXPORT DeviceRefManager : public Singleton<DeviceRefManager>
83{
84 friend class Device;
85 friend class Singleton<DeviceRefManager>;
86 friend class ObjectRef<Device>;
87 friend class Manager;
88
89 public:
90
91 /**
92 * Definition of a callback function for devices.
93 */
95
96 protected:
97
98 /**
99 * Definition of a pair combining a device reference with a state specifying whether the medium is used exclusively.
100 */
101 using DevicePair = std::pair<DeviceRef, bool>;
102
103 /**
104 * Map mapping urls to device references.
105 */
106 using DeviceMap = std::multimap<std::string, DevicePair>;
107
108 /**
109 * Definition of device callback functions.
110 */
112
113 public:
114
115 /**
116 * Registers a new device.
117 * @param device Device object to manage
118 * @param exclusive State specifying whether the device will be used exclusively
119 * @return Device reference
120 */
121 DeviceRef registerDevice(Device* device, const bool exclusive);
122
123 /**
124 * Returns a device by a given device name.
125 * If the device does not exist an empty reference is returned.
126 * Several devices can be registered with the same name, in that case it is not defined which of them is returned.
127 * @param name The name of the device
128 * @param allowExclusive True, to also return a device which has been created for exclusive usage; False, to return a shareable device only
129 * @return Device reference of the requested device
130 * @see device(const Device*).
131 */
132 DeviceRef device(const std::string& name, const bool allowExclusive = true) const;
133
134 /**
135 * Returns a specified device by it's device type.
136 * Devices which have been created for exclusive usage are skipped, and if several of the remaining devices match it is not defined which of them is returned.
137 * @param type The type of the device to return
138 * @param exactMatch True, to return a device with exact matching type; False, if the requested type is part of the device
139 * @return Requested device, an empty reference if no matching device exists
140 */
141 DeviceRef device(const Device::DeviceType type, const bool exactMatch = false) const;
142
143 /**
144 * Returns the reference of a specific device.
145 * In contrast to the name-based lookup, this function identifies the device by its address, so it also works if several devices share the same name.
146 * @param device The device for which the reference will be returned, must be valid
147 * @return Device reference of the given device, an empty reference if the device is not registered
148 */
149 DeviceRef device(const Device* device) const;
150
151 /**
152 * Returns whether no device is registered currently.
153 * @return True, if so
154 */
155 inline bool isEmpty() const;
156
157 /**
158 * Returns whether no device is registered created by a specific library.
159 * @param library The name of the library to check, must be valid
160 * @return True, if so
161 */
162 bool isEmpty(const std::string& library) const;
163
164 /**
165 * Returns the name of all existing devices which belong to a specific library.
166 * @param library The name of the library to which the devices belong, must be valid
167 * @return The names of all currently existing devices
168 */
169 Strings devicesFromLibrary(const std::string& library) const;
170
171 /**
172 * Adds a callback function which is called whenever a new device is created or deleted.
173 * The callback function will be called immediately for all already existing devices.
174 * @param callback The callback function, must be valid
175 */
177
178 /**
179 * Removes a previously added callback function for device events.
180 * @param callback The callback function, must be valid
181 */
182 inline void removeDeviceCallbackFunction(const DeviceCallback& callback);
183
184 protected:
185
186 /**
187 * Destructs the manager.
188 */
190
191 /**
192 * Returns whether a specified device is registered as exclusive.
193 * @param device Device to check
194 * @return True, if so
195 */
196 bool isExclusive(const Device* device);
197
198 /**
199 * Unregisters a device.
200 */
201 void unregisterDevice(const Device* device);
202
203 protected:
204
205 /// Map holding all device references.
207
208 /// The callback functions for device events.
210
211 /// Lock for the device map
212 mutable Lock lock_;
213};
214
215template <typename T>
218{
219 // nothing to do here
220}
221
222template <typename T>
224 SmartObjectRef<T, Device>(deviceRef)
225{
226 // nothing to do here
227}
228
229template <typename T>
231 SmartObjectRef<T, Device>(object, ReleaseCallback(DeviceRefManager::get(), &DeviceRefManager::unregisterDevice))
232{
233 // nothing to do here
234}
235
236template <typename T>
237template <typename T2>
239 SmartObjectRef<T, Device>(reference)
240{
241 // nothing to do here
242}
243
244template <typename T>
246{
248 return *this;
249}
250
251inline bool DeviceRefManager::isEmpty() const
252{
253 const ScopedLock scopedLock(lock_);
254
255 return deviceMap_.empty();
256}
257
259{
260 ocean_assert(callback);
262}
263
264}
265
266}
267
268#endif // META_OCEAN_DEVICES_DEF_H
This class implements a container for callback functions.
Definition Callback.h:3454
void removeCallback(const T &callback)
Removes a callback object.
Definition Callback.h:4053
Definition of a class holding the major and minor device type.
Definition devices/Device.h:62
This class is the base class for all devices of any type.
Definition devices/Device.h:28
This class implements a manager for device references.
Definition DeviceRef.h:83
bool isEmpty() const
Returns whether no device is registered currently.
Definition DeviceRef.h:251
std::multimap< std::string, DevicePair > DeviceMap
Map mapping urls to device references.
Definition DeviceRef.h:106
DeviceCallbacks deviceCallbacks_
The callback functions for device events.
Definition DeviceRef.h:209
void addDeviceCallbackFunction(const DeviceCallback &callback)
Adds a callback function which is called whenever a new device is created or deleted.
void removeDeviceCallbackFunction(const DeviceCallback &callback)
Removes a previously added callback function for device events.
Definition DeviceRef.h:258
bool isExclusive(const Device *device)
Returns whether a specified device is registered as exclusive.
bool isEmpty(const std::string &library) const
Returns whether no device is registered created by a specific library.
DeviceRef device(const std::string &name, const bool allowExclusive=true) const
Returns a device by a given device name.
DeviceRef registerDevice(Device *device, const bool exclusive)
Registers a new device.
virtual ~DeviceRefManager()
Destructs the manager.
Lock lock_
Lock for the device map.
Definition DeviceRef.h:212
DeviceRef device(const Device::DeviceType type, const bool exactMatch=false) const
Returns a specified device by it's device type.
std::pair< DeviceRef, bool > DevicePair
Definition of a pair combining a device reference with a state specifying whether the medium is used ...
Definition DeviceRef.h:101
void unregisterDevice(const Device *device)
Unregisters a device.
Strings devicesFromLibrary(const std::string &library) const
Returns the name of all existing devices which belong to a specific library.
DeviceRef device(const Device *device) const
Returns the reference of a specific device.
DeviceMap deviceMap_
Map holding all device references.
Definition DeviceRef.h:206
This class implements as singleton-based manager which allows to access all available devices.
Definition devices/Manager.h:32
This class implements a smart device reference.
Definition DeviceRef.h:36
SmartDeviceRef(Device *object)
Creates a new SmartDeviceRef by a given object.
Definition DeviceRef.h:230
SmartDeviceRef & operator=(const DeviceRef &deviceRef)
Assigns a smart device reference.
Definition DeviceRef.h:245
typename SmartObjectRef< T, Device >::ReleaseCallback ReleaseCallback
Redefinition of the release callback function defined in ObjectRef.
Definition DeviceRef.h:42
SmartDeviceRef(const DeviceRef &deviceRef)
Creates a new smart device reference by a given device reference.
Definition DeviceRef.h:223
SmartDeviceRef()
Creates an empty smart device reference.
Definition DeviceRef.h:216
SmartDeviceRef(const SmartDeviceRef< T2 > &reference)
Copies a smart device reference.
Definition DeviceRef.h:238
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:147
This template class is the base class for all singleton objects.
Definition Singleton.h:71
This template class implements a smart object reference which is a specialization of an ObjectRef obj...
Definition SmartObjectRef.h:90
SmartObjectRef< T, TBase > & operator=(const SmartObjectRef< T, TBase > &smartObjectRef)
Assign operator.
Definition SmartObjectRef.h:280
typename ObjectRef< TBase >::ReleaseCallback ReleaseCallback
Redefinition of the release callback function defined in ObjectRef.
Definition SmartObjectRef.h:96
std::vector< std::string > Strings
Definition of a vector holding strings.
Definition Base.h:162
The namespace covering the entire Ocean framework.
Definition Accessor.h:15