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 * @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 The type of the device to return
134 * @param exactMatch True, to return a device with exact matching type; False, if the requested type is part of the device
135 * @return Requested device
136 */
137 DeviceRef device(const Device::DeviceType type, const bool exactMatch = false) const;
138
139 /**
140 * Returns whether no device is registered currently.
141 * @return True, if so
142 */
143 inline bool isEmpty() const;
144
145 /**
146 * Returns whether no device is registered created by a specific library.
147 * @param library The name of the library to check, must be valid
148 * @return True, if so
149 */
150 bool isEmpty(const std::string& library) const;
151
152 /**
153 * Returns the name of all existing devices which belong to a specific library.
154 * @param library The name of the library to which the devices belong, must be valid
155 * @return The names of all currently existing devices
156 */
157 Strings devicesFromLibrary(const std::string& library) const;
158
159 /**
160 * Adds a callback function which is called whenever a new device is created or deleted.
161 * The callback function will be called immediately for all already existing devices.
162 * @param callback The callback function, must be valid
163 */
165
166 /**
167 * Removes a previously added callback function for device events.
168 * @param callback The callback function, must be valid
169 */
170 inline void removeDeviceCallbackFunction(const DeviceCallback& callback);
171
172 protected:
173
174 /**
175 * Destructs the manager.
176 */
178
179 /**
180 * Returns whether a specified device is registered as exclusive.
181 * @param device Device to check
182 * @return True, if so
183 */
184 bool isExclusive(const Device* device);
185
186 /**
187 * Unregisters a device.
188 */
189 void unregisterDevice(const Device* device);
190
191 protected:
192
193 /// Map holding all device references.
195
196 /// The callback functions for device events.
198
199 /// Lock for the device map
200 mutable Lock lock_;
201};
202
203template <typename T>
206{
207 // nothing to do here
208}
209
210template <typename T>
212 SmartObjectRef<T, Device>(deviceRef)
213{
214 // nothing to do here
215}
216
217template <typename T>
219 SmartObjectRef<T, Device>(object, ReleaseCallback(DeviceRefManager::get(), &DeviceRefManager::unregisterDevice))
220{
221 // nothing to do here
222}
223
224template <typename T>
225template <typename T2>
227 SmartObjectRef<T, Device>(reference)
228{
229 // nothing to do here
230}
231
232template <typename T>
234{
236 return *this;
237}
238
239inline bool DeviceRefManager::isEmpty() const
240{
241 const ScopedLock scopedLock(lock_);
242
243 return deviceMap_.empty();
244}
245
247{
248 ocean_assert(callback);
250}
251
252}
253
254}
255
256#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: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:239
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:197
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:246
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.
Lock lock_
Lock for the device map.
Definition DeviceRef.h:200
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 std::string &name) const
Returns a device by a given device name.
DeviceMap deviceMap_
Map holding all device references.
Definition DeviceRef.h:194
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:218
SmartDeviceRef & operator=(const DeviceRef &deviceRef)
Assigns a smart device reference.
Definition DeviceRef.h:233
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:211
SmartDeviceRef()
Creates an empty smart device reference.
Definition DeviceRef.h:204
SmartDeviceRef(const SmartDeviceRef< T2 > &reference)
Copies a smart device reference.
Definition DeviceRef.h:226
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