Ocean
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 
11 #include "ocean/devices/Devices.h"
12 #include "ocean/devices/Device.h"
14 
15 #include <vector>
16 
17 namespace Ocean
18 {
19 
20 namespace Devices
21 {
22 
23 /**
24  * This class implements a factory able to create instances of devices.
25  * @ingroup devices
26  */
27 class 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  */
57  inline DeviceDescriptor(const std::string& name, const Device::DeviceType type, const InstanceFunction& instanceFunction);
58 
59  public:
60 
61  /// Device name.
62  std::string name_;
63 
64  /// Device type.
66 
67  /// Device instance function.
69  };
70 
71  /**
72  * Definition of a vector holding device descriptor objects.
73  */
74  typedef std::vector<DeviceDescriptor> DeviceDescriptors;
75 
76  public:
77 
78  /**
79  * Returns the name of this factory.
80  */
81  inline const std::string& name() const;
82 
83  protected:
84 
85  /**
86  * Disabled copy constructor.
87  * @param factory Object which would be copied
88  */
89  Factory(const Factory& factory) = delete;
90 
91  /**
92  * Creates a new factory.
93  * @param name The name of the factory to create
94  */
95  explicit Factory(const std::string& name);
96 
97  /**
98  * Destructs this factory.
99  */
100  virtual ~Factory();
101 
102  /**
103  * Returns a list of available devices.
104  * @return Device list
105  */
106  virtual Strings devices() const;
107 
108  /**
109  * Returns a list of available devices matching a specified major device type.
110  * @param type Type to return device names for
111  * @return Device list
112  */
113  virtual Strings devices(const Device::DeviceType type) const;
114 
115  /**
116  * Returns the type of a specified device.
117  * @param device Name of the device to return the type for
118  * @return Device type holding major and minor type
119  */
120  virtual Device::DeviceType deviceType(const std::string& device) const;
121 
122  /**
123  * Creates a new device by its name or returns an existing one if an exclusive use is not necessary.
124  * @param name The name of the device to create
125  * @param useExclusive Determines whether the device will be used exclusively
126  * @return Requested device
127  */
128  virtual DeviceRef device(const std::string& name, const bool useExclusive = false) const;
129 
130  /**
131  * 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.
132  * @param type Type of the device to return
133  * @param useExclusive Determines whether the device will be used exclusively
134  * @return Requested device
135  */
136  virtual DeviceRef device(const Device::DeviceType type, const bool useExclusive = false);
137 
138  /**
139  * Returns a new adapter measurement device, which is exclusive always.
140  * @param type Type of the device to return
141  * @param name Unique name of the resulting adapter device, this name will be assigned to the new adapter device
142  */
143  DeviceRef adapterDevice(const Device::DeviceType type, const std::string& name);
144 
145  /**
146  * Disabled copy operator.
147  * @param factory Object which would be copied
148  * @return Reference to this object
149  */
150  Factory& operator=(const Factory& factory) = delete;
151 
152  /**
153  * Registers a factory at the manager.
154  * Each factory should be registered at most once.
155  * @param factory The factory to register
156  * @return True, if the factory hasn't been registered before
157  */
158  static bool registerFactory(std::unique_ptr<Factory>&& factory);
159 
160  /**
161  * Unregisters a factory at the manager.
162  * Beware: All devices created by the factory must be released before,<br>
163  * otherwise the factory cannot be unregistered.
164  * @param factory Name of the factory to unregister
165  * @return True, if the factory could be unregistered
166  */
167  static bool unregisterFactory(const std::string& factory);
168 
169  /**
170  * Registes a device at this factory.
171  * @param deviceName Unique name of the device to register
172  * @param deviceType Type of the device
173  * @param deviceInstanceFunction Function creating an instance of the device
174  * @see unregisterDevice().
175  */
176  bool registerDevice(const std::string& deviceName, const Device::DeviceType deviceType, const InstanceFunction& deviceInstanceFunction);
177 
178  /**
179  * Unregisters a previously registered device from this factory.
180  * Beware: Ensure that the device is not used anymore before unregistering it.
181  * @param deviceName The name of the device to unregister, must be valid
182  * @return True, if succeeded
183  * @see registerDevice().
184  */
185  bool unregisterDevice(const std::string& deviceName);
186 
187  /**
188  * Creates a new device by a given device triple.
189  * @param deviceDescriptor The descriptor of the device to be created
190  * @param useExclusive Determines whether the device will be used exclusively
191  * @return New device if succeeded
192  */
193  static DeviceRef createDevice(const DeviceDescriptor& deviceDescriptor, bool useExclusive);
194 
195  private:
196 
197  /// The factory's name.
198  std::string name_;
199 
200  /// Vector holding all registered devices with name and type.
202 
203  /// Factory lock.
204  mutable Lock lock_;
205 };
206 
208  type_(Device::DEVICE_INVALID)
209 {
210  // nothing to do here
211 }
212 
213 inline Factory::DeviceDescriptor::DeviceDescriptor(const std::string& name, const Device::DeviceType type, const InstanceFunction& instanceFunction) :
214  name_(name),
215  type_(type),
216  instanceFunction_(instanceFunction)
217 {
218  // nothing to do here
219 }
220 
221 inline const std::string& Factory::name() const
222 {
223  return name_;
224 }
225 
226 }
227 
228 }
229 
230 #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:68
std::string name_
Device name.
Definition: devices/Factory.h:62
DeviceDescriptor()
Creates an empty device triple object.
Definition: devices/Factory.h:207
Device::DeviceType type_
Device type.
Definition: devices/Factory.h:65
This class implements a factory able to create instances of devices.
Definition: devices/Factory.h:28
bool registerDevice(const std::string &deviceName, const Device::DeviceType deviceType, const InstanceFunction &deviceInstanceFunction)
Registes a device at this factory.
Callback< Device *, const std::string &, const Device::DeviceType & > InstanceFunction
Definition of a callback function creating a specific device.
Definition: devices/Factory.h:37
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.
std::vector< DeviceDescriptor > DeviceDescriptors
Definition of a vector holding device descriptor objects.
Definition: devices/Factory.h:74
std::string name_
The factory's name.
Definition: devices/Factory.h:198
Factory(const Factory &factory)=delete
Disabled copy constructor.
DeviceDescriptors deviceDescriptors_
Vector holding all registered devices with name and type.
Definition: devices/Factory.h:201
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(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:204
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:221
Factory & operator=(const Factory &factory)=delete
Disabled copy operator.
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
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