Ocean
Loading...
Searching...
No Matches
devices/Factory.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_FACTORY_H
9#define META_OCEAN_DEVICES_FACTORY_H
10
14
15#include <vector>
16
17namespace Ocean
18{
19
20namespace Devices
21{
22
23/**
24 * This class implements a factory able to create instances of devices.
25 * @ingroup devices
26 */
27class OCEAN_DEVICES_EXPORT Factory
28{
29 friend class Manager;
30 friend struct std::default_delete<Factory>;
31
32 protected:
33
34 /**
35 * Definition of a callback function creating a specific device.
36 */
38
39 /**
40 * This class stores information to describe and to creator a device.
41 */
43 {
44 public:
45
46 /**
47 * Creates an empty device triple object.
48 */
49 inline DeviceDescriptor();
50
51 /**
52 * Creates a new device descritpr object.
53 * @param name The name of the device
54 * @param type The type of the device
55 * @param instanceFunction The callback function to create the new device, must be valid
56 * @param priority The priority of the device, in case two devices are equivalent, the device with the higher priority will be used
57 */
58 inline DeviceDescriptor(const std::string& name, const Device::DeviceType type, const InstanceFunction& instanceFunction, const unsigned int priority);
59
60 public:
61
62 /// Device name.
63 std::string name_;
64
65 /// Device type.
67
68 /// Device instance function.
70
71 /// The device priority.
72 unsigned int priority_ = 0u;
73 };
74
75 /**
76 * Definition of a vector holding device descriptor objects.
77 */
78 using DeviceDescriptors = std::vector<DeviceDescriptor>;
79
80 public:
81
82 /**
83 * Returns the name of this factory.
84 */
85 inline const std::string& name() const;
86
87 protected:
88
89 /**
90 * Disabled copy constructor.
91 * @param factory Object which would be copied
92 */
93 Factory(const Factory& factory) = delete;
94
95 /**
96 * Creates a new factory.
97 * @param name The name of the factory to create
98 */
99 explicit Factory(const std::string& name);
100
101 /**
102 * Destructs this factory.
103 */
104 virtual ~Factory();
105
106 /**
107 * Returns a list of available devices.
108 * @return Device list
109 */
110 virtual Strings devices() const;
111
112 /**
113 * Returns a list of available devices matching a specified major device type.
114 * @param type Type to return device names for
115 * @return Device list
116 */
117 virtual Strings devices(const Device::DeviceType type) const;
118
119 /**
120 * Returns the type of a specified device.
121 * @param device Name of the device to return the type for
122 * @return Device type holding major and minor type
123 */
124 virtual Device::DeviceType deviceType(const std::string& device) const;
125
126 /**
127 * Creates a new device by its name or returns an existing one if an exclusive use is not necessary.
128 * @param name The name of the device to create
129 * @param useExclusive Determines whether the device will be used exclusively
130 * @return Requested device
131 */
132 virtual DeviceRef device(const std::string& name, const bool useExclusive = false) const;
133
134 /**
135 * Creates a new device defined by a major and a minor device type or returns an existing one if an exclusive use is not necessary.
136 * @param type Type of the device to return
137 * @param useExclusive Determines whether the device will be used exclusively
138 * @return Requested device
139 */
140 virtual DeviceRef device(const Device::DeviceType type, const bool useExclusive = false);
141
142 /**
143 * Returns a new adapter measurement device, which is exclusive always.
144 * @param type Type of the device to return
145 * @param name Unique name of the resulting adapter device, this name will be assigned to the new adapter device
146 */
147 DeviceRef adapterDevice(const Device::DeviceType type, const std::string& name);
148
149 /**
150 * Disabled copy operator.
151 * @param factory Object which would be copied
152 * @return Reference to this object
153 */
154 Factory& operator=(const Factory& factory) = delete;
155
156 /**
157 * Registers a factory at the manager.
158 * Each factory should be registered at most once.
159 * @param factory The factory to register
160 * @return True, if the factory hasn't been registered before
161 */
162 static bool registerFactory(std::unique_ptr<Factory>&& factory);
163
164 /**
165 * Unregisters a factory at the manager.
166 * Beware: All devices created by the factory must be released before,<br>
167 * otherwise the factory cannot be unregistered.
168 * @param factory Name of the factory to unregister
169 * @return True, if the factory could be unregistered
170 */
171 static bool unregisterFactory(const std::string& factory);
172
173 /**
174 * Registes a device at this factory.
175 * @param deviceName Unique name of the device to register
176 * @param deviceType Type of the device
177 * @param deviceInstanceFunction Function creating an instance of the device
178 * @param priority The priority of the device, in case two devices are equivalent, the device with the higher priority will be used
179 * @see unregisterDevice().
180 */
181 bool registerDevice(const std::string& deviceName, const Device::DeviceType deviceType, const InstanceFunction& deviceInstanceFunction, const unsigned int priority = 100u);
182
183 /**
184 * Unregisters a previously registered device from this factory.
185 * Beware: Ensure that the device is not used anymore before unregistering it.
186 * @param deviceName The name of the device to unregister, must be valid
187 * @return True, if succeeded
188 * @see registerDevice().
189 */
190 bool unregisterDevice(const std::string& deviceName);
191
192 /**
193 * Creates a new device by a given device triple.
194 * @param deviceDescriptor The descriptor of the device to be created
195 * @param useExclusive Determines whether the device will be used exclusively
196 * @return New device if succeeded
197 */
198 static DeviceRef createDevice(const DeviceDescriptor& deviceDescriptor, bool useExclusive);
199
200 /**
201 * Returns a specified factory
202 * @param name The name of the factory to return, must be valid
203 * @param scopedLock The scoped lock which will be used to lock access the access to the factor, needs to be released as soon as possible
204 * @return The requested factory, nullptr if the factory does not exist
205 */
206 static Factory* factory(const std::string& name, TemporaryScopedLock& scopedLock);
207
208 private:
209
210 /// The factory's name.
211 std::string name_;
212
213 /// Vector holding all registered devices with name and type.
215
216 /// Factory lock.
217 mutable Lock lock_;
218};
219
221 type_(Device::DEVICE_INVALID)
222{
223 // nothing to do here
224}
225
226inline Factory::DeviceDescriptor::DeviceDescriptor(const std::string& name, const Device::DeviceType type, const InstanceFunction& instanceFunction, const unsigned int priority) :
227 name_(name),
228 type_(type),
229 instanceFunction_(instanceFunction),
230 priority_(priority)
231{
232 // nothing to do here
233}
234
235inline const std::string& Factory::name() const
236{
237 return name_;
238}
239
240}
241
242}
243
244#endif // META_OCEAN_DEVICES_FACTORY_H
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 stores information to describe and to creator a device.
Definition devices/Factory.h:43
InstanceFunction instanceFunction_
Device instance function.
Definition devices/Factory.h:69
std::string name_
Device name.
Definition devices/Factory.h:63
DeviceDescriptor()
Creates an empty device triple object.
Definition devices/Factory.h:220
Device::DeviceType type_
Device type.
Definition devices/Factory.h:66
This class implements a factory able to create instances of devices.
Definition devices/Factory.h:28
static Factory * factory(const std::string &name, TemporaryScopedLock &scopedLock)
Returns a specified factory.
virtual Device::DeviceType deviceType(const std::string &device) const
Returns the type of a specified device.
virtual Strings devices() const
Returns a list of available devices.
virtual DeviceRef device(const std::string &name, const bool useExclusive=false) const
Creates a new device by its name or returns an existing one if an exclusive use is not necessary.
virtual ~Factory()
Destructs this factory.
bool registerDevice(const std::string &deviceName, const Device::DeviceType deviceType, const InstanceFunction &deviceInstanceFunction, const unsigned int priority=100u)
Registes a device at this factory.
std::string name_
The factory's name.
Definition devices/Factory.h:211
Factory(const Factory &factory)=delete
Disabled copy constructor.
DeviceDescriptors deviceDescriptors_
Vector holding all registered devices with name and type.
Definition devices/Factory.h:214
bool unregisterDevice(const std::string &deviceName)
Unregisters a previously registered device from this factory.
virtual Strings devices(const Device::DeviceType type) const
Returns a list of available devices matching a specified major device type.
Factory & operator=(const Factory &factory)=delete
Disabled copy operator.
Factory(const std::string &name)
Creates a new factory.
virtual DeviceRef device(const Device::DeviceType type, const bool useExclusive=false)
Creates a new device defined by a major and a minor device type or returns an existing one if an excl...
Lock lock_
Factory lock.
Definition devices/Factory.h:217
std::vector< DeviceDescriptor > DeviceDescriptors
Definition of a vector holding device descriptor objects.
Definition devices/Factory.h:78
DeviceRef adapterDevice(const Device::DeviceType type, const std::string &name)
Returns a new adapter measurement device, which is exclusive always.
static bool registerFactory(std::unique_ptr< Factory > &&factory)
Registers a factory at the manager.
static bool unregisterFactory(const std::string &factory)
Unregisters a factory at the manager.
static DeviceRef createDevice(const DeviceDescriptor &deviceDescriptor, bool useExclusive)
Creates a new device by a given device triple.
const std::string & name() const
Returns the name of this factory.
Definition devices/Factory.h:235
This class implements as singleton-based manager which allows to access all available devices.
Definition devices/Manager.h:32
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a recursive scoped lock object allowing to release the lock before the scoped o...
Definition Lock.h:266
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