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 typedef std::pair<DeviceRef, bool> DevicePair;
102
103 /**
104 * Map mapping urls to device references.
105 */
106 typedef std::multimap<std::string, DevicePair> DeviceMap;
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 * @param name The name of the new device
127 * @return Device reference of the requested device
128 */
129 DeviceRef device(const std::string& name) const;
130
131 /**
132 * Returns a specified device by it's device type.
133 * @param type Device type
134 * @return Requested device
135 */
137
138 /**
139 * Returns whether no device is registered currently.
140 * @return True, if so
141 */
142 inline bool isEmpty() const;
143
144 /**
145 * Returns whether no device is registered created by a specific library.
146 * @param library The name of the library to check, must be valid
147 * @return True, if so
148 */
149 bool isEmpty(const std::string& library) const;
150
151 /**
152 * Returns the name of all existing devices which belong to a specific library.
153 * @param library The name of the library to which the devices belong, must be valid
154 * @return The names of all currently existing devices
155 */
156 std::vector<std::string> devicesFromLibrary(const std::string& library) const;
157
158 /**
159 * Adds a callback function which is called whenever a new device is created or deleted.
160 * The callback function will be called immediately for all already existing devices.
161 * @param callback The callback function, must be valid
162 */
164
165 /**
166 * Removes a previously added callback function for device events.
167 * @param callback The callback function, must be valid
168 */
169 inline void removeDeviceCallbackFunction(const DeviceCallback& callback);
170
171 protected:
172
173 /**
174 * Destructs the manager.
175 */
177
178 /**
179 * Returns whether a specified device is registered as exclusive.
180 * @param device Device to check
181 * @return True, if so
182 */
183 bool isExclusive(const Device* device);
184
185 /**
186 * Unregisters a device.
187 */
188 void unregisterDevice(const Device* device);
189
190 protected:
191
192 /// Map holding all device references.
194
195 /// The callback functions for device events.
197
198 /// Lock for the device map
199 mutable Lock lock_;
200};
201
202template <typename T>
205{
206 // nothing to do here
207}
208
209template <typename T>
211 SmartObjectRef<T, Device>(deviceRef)
212{
213 // nothing to do here
214}
215
216template <typename T>
218 SmartObjectRef<T, Device>(object, ReleaseCallback(DeviceRefManager::get(), &DeviceRefManager::unregisterDevice))
219{
220 // nothing to do here
221}
222
223template <typename T>
224template <typename T2>
226 SmartObjectRef<T, Device>(reference)
227{
228 // nothing to do here
229}
230
231template <typename T>
233{
235 return *this;
236}
237
238inline bool DeviceRefManager::isEmpty() const
239{
240 const ScopedLock scopedLock(lock_);
241
242 return deviceMap_.empty();
243}
244
246{
247 ocean_assert(callback);
249}
250
251}
252
253}
254
255#endif // META_OCEAN_DEVICES_DEF_H
This class implements a vector holding several callbacks with the same type.
Definition Callback.h:3800
void removeCallback(const T &callback)
Removes a callback object.
Definition Callback.h:4050
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:238
Callback< void, Device *, bool > DeviceCallback
Definition of a callback function for devices.
Definition DeviceRef.h:94
DeviceCallbacks deviceCallbacks_
The callback functions for device events.
Definition DeviceRef.h:196
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:245
std::vector< std::string > devicesFromLibrary(const std::string &library) const
Returns the name of all existing devices which belong to a specific library.
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 registerDevice(Device *device, const bool exclusive)
Registers a new device.
virtual ~DeviceRefManager()
Destructs the manager.
Callbacks< DeviceCallback > DeviceCallbacks
Definition of device callback functions.
Definition DeviceRef.h:111
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
Lock lock_
Lock for the device map.
Definition DeviceRef.h:199
DeviceRef device(const Device::DeviceType type) const
Returns a specified device by it's device type.
std::multimap< std::string, DevicePair > DeviceMap
Map mapping urls to device references.
Definition DeviceRef.h:106
void unregisterDevice(const Device *device)
Unregisters a device.
DeviceRef device(const std::string &name) const
Returns a device by a given device name.
DeviceMap deviceMap_
Map holding all device references.
Definition DeviceRef.h:193
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:217
SmartObjectRef< T, Device >::ReleaseCallback ReleaseCallback
Redefinition of the release callback function defined in ObjectRef.
Definition DeviceRef.h:42
SmartDeviceRef & operator=(const DeviceRef &deviceRef)
Assigns a smart device reference.
Definition DeviceRef.h:232
SmartDeviceRef(const DeviceRef &deviceRef)
Creates a new smart device reference by a given device reference.
Definition DeviceRef.h:210
SmartDeviceRef()
Creates an empty smart device reference.
Definition DeviceRef.h:203
SmartDeviceRef(const SmartDeviceRef< T2 > &reference)
Copies a smart device reference.
Definition DeviceRef.h:225
This class implements a recursive lock object.
Definition Lock.h:31
This template class implements a object reference with an internal reference counter.
Definition base/ObjectRef.h:58
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:135
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
ObjectRef< Device > DeviceRef
This class implements a device reference with an internal reference counter.
Definition DeviceRef.h:27
The namespace covering the entire Ocean framework.
Definition Accessor.h:15