Ocean
devices/Manager.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_MANAGER_H
9 #define META_OCEAN_DEVICES_MANAGER_H
10 
11 #include "ocean/devices/Devices.h"
12 #include "ocean/devices/Device.h"
13 #include "ocean/devices/Factory.h"
15 
16 #include "ocean/base/Lock.h"
17 #include "ocean/base/Singleton.h"
18 
19 #include <vector>
20 
21 namespace Ocean
22 {
23 
24 namespace Devices
25 {
26 
27 /**
28  * This class implements as singleton-based manager which allows to access all available devices.
29  * @ingroup devices
30  */
31 class OCEAN_DEVICES_EXPORT Manager : public Singleton<Manager>
32 {
33  friend class Singleton<Manager>;
34  friend class Factory;
35  friend class Measurement;
36 
37  public:
38 
39  /**
40  * Definition of a callback function creating an ad-hoc device.
41  */
43 
44  protected:
45 
46  /**
47  * This class implements an ad-hoc factory for devices which are implemented outside of an own library e.g., during rapid prototyping.
48  * In general, devices should be implemented in an own dedicated plugin-based library.
49  * However, for prototyping the ad-hoc factory offers a simple possibility to create devices without the overhead of writing the plugin related code.
50  */
51  class AdhocFactory : public Factory
52  {
53  friend class Manager;
54 
55  protected:
56 
57  /**
58  * Creates a new factory object.
59  */
61  };
62 
63  /**
64  * Definition of a vector holding unique pointers of factories.
65  */
66  typedef std::vector<std::unique_ptr<Factory>> FactoryUniquePointers;
67 
68  /**
69  * Definition of an unsorted multimap mapping object ids to descriptions.
70  */
71  typedef std::unordered_multimap<Measurement::ObjectId, std::string> ObjectIdMultimap;
72 
73  public:
74 
75  /**
76  * Returns a specific device.
77  * @param name The name of the device to return, must be valid
78  * @param useExclusive True, if the caller would like to use this device exclusively; False, if the device can be shared
79  * @return The requested device, if available
80  */
81  DeviceRef device(const std::string& name, const bool useExclusive = false);
82 
83  /**
84  * Returns a specified device.
85  * @param type The major type of the device to return
86  * @param useExclusive True, if the caller would like to use this device exclusively; False, if the device can be shared
87  * @return The requested device, if available
88  */
89  DeviceRef device(const Device::DeviceType type, const bool useExclusive = false);
90 
91  /**
92  * Returns a list with the names of all available devices.
93  * @return Device names
94  */
95  Strings devices() const;
96 
97  /**
98  * Returns a list with the names of all available devices matching a major and minor device type.
99  * @param type Type of the device to return
100  * @return Device names
101  */
102  Strings devices(const Device::DeviceType type) const;
103 
104  /**
105  * Returns whether a specified library is registered at this manager.
106  * @param library Name of the library to check
107  * @return True, if so
108  */
109  bool isRegistered(const std::string& library);
110 
111  /**
112  * Returns the names of all registered libraries.
113  * @return Registered library names
114  */
116 
117  /**
118  * Returns the description of an object id.
119  * @param objectId The object id for which the description will be returned
120  * @return The description of the object, empty if the object id is unknown
121  */
122  std::string objectDescription(const Measurement::ObjectId objectId) const;
123 
124  /**
125  * Destructs all registered libraries and their corresponding factories.
126  */
127  void release();
128 
129  /**
130  * Registes an ad-hoc device at this manager.
131  * In general, devices should be implemented in an own decicated plugin-based library.
132  * However, for prototyping an ad-hoc device cames with the same features as a device implemented in a dedicatd library without the overhead of writing the code for the plugin mechanisms etc.
133  * @param deviceName Unique name of the device to register
134  * @param deviceType Type of the device
135  * @param deviceInstanceFunction Function creating an instance of the device
136  * @see unregisterAdhocDevice().
137  */
138  bool registerAdhocDevice(const std::string& deviceName, const Device::DeviceType& deviceType, const AdhocInstanceFunction& deviceInstanceFunction);
139 
140  /**
141  * Unregisters a previously registered ad-hoc device from this manager.
142  * Beware: Ensure that the device is not used anymore before unregistering it.
143  * @param deviceName The name of the device to unregister, must be valid
144  * @return True, if succeeded
145  * @see registerAdhocDevice().
146  */
147  bool unregisterAdhocDevice(const std::string& deviceName);
148 
149  /**
150  * Returns the name of the ad-hoc factory.
151  * The ad-hoc factory is for devices which are implemented outside of an own library e.g., during rapid prototyping.
152  * @return The name of the ad-hoc factor
153  */
154  static std::string nameAdhocFactory();
155 
156  private:
157 
158  /**
159  * Creates a new manager object.
160  */
162 
163  /**
164  * Destructs a manager object.
165  */
166  virtual ~Manager();
167 
168  /**
169  * Creates a unique object id for a new object (e.g., a tracking object like an image, a marker, or a location).
170  * @param description The description of the new object, must be valid
171  * @return The unique id which is unique across all devices
172  */
173  Measurement::ObjectId createUniqueObjectId(const std::string& description);
174 
175  /**
176  * Registers a new factory.
177  * @param factory The factory to register
178  * @return True, if the factory hasn't been registered before
179  */
180  bool registerFactory(std::unique_ptr<Factory>&& factory);
181 
182  /**
183  * Unregisters a factory.
184  * @param factory Name of the factory to unregister
185  * @return True, if succeeded
186  */
187  bool unregisterFactory(const std::string& factory);
188 
189  private:
190 
191  /// The map mapping object ids to descriptions.
193 
194  /// The counter for unique object ids.
195  Measurement::ObjectId uniqueObjectIdCounter_ = Measurement::ObjectId(0);
196 
197  /// All registered device factories.
199 
200  /// The Manager's lock.
201  mutable Lock lock_;
202 };
203 
204 }
205 
206 }
207 
208 #endif // META_OCEAN_DEVICES_MANAGER_H
Definition of a class holding the major and minor device type.
Definition: devices/Device.h:62
This class implements a factory able to create instances of devices.
Definition: devices/Factory.h:28
Callback< Device *, const std::string &, const Device::DeviceType & > InstanceFunction
Definition of a callback function creating a specific device.
Definition: devices/Factory.h:37
This class implements an ad-hoc factory for devices which are implemented outside of an own library e...
Definition: devices/Manager.h:52
AdhocFactory()
Creates a new factory object.
This class implements as singleton-based manager which allows to access all available devices.
Definition: devices/Manager.h:32
Lock lock_
The Manager's lock.
Definition: devices/Manager.h:201
bool unregisterFactory(const std::string &factory)
Unregisters a factory.
bool unregisterAdhocDevice(const std::string &deviceName)
Unregisters a previously registered ad-hoc device from this manager.
DeviceRef device(const std::string &name, const bool useExclusive=false)
Returns a specific device.
Strings libraries() const
Returns the names of all registered libraries.
std::string objectDescription(const Measurement::ObjectId objectId) const
Returns the description of an object id.
bool isRegistered(const std::string &library)
Returns whether a specified library is registered at this manager.
DeviceRef device(const Device::DeviceType type, const bool useExclusive=false)
Returns a specified device.
Manager()
Creates a new manager object.
FactoryUniquePointers factories_
All registered device factories.
Definition: devices/Manager.h:198
std::unordered_multimap< Measurement::ObjectId, std::string > ObjectIdMultimap
Definition of an unsorted multimap mapping object ids to descriptions.
Definition: devices/Manager.h:71
Measurement::ObjectId createUniqueObjectId(const std::string &description)
Creates a unique object id for a new object (e.g., a tracking object like an image,...
bool registerAdhocDevice(const std::string &deviceName, const Device::DeviceType &deviceType, const AdhocInstanceFunction &deviceInstanceFunction)
Registes an ad-hoc device at this manager.
Strings devices() const
Returns a list with the names of all available devices.
virtual ~Manager()
Destructs a manager object.
std::vector< std::unique_ptr< Factory > > FactoryUniquePointers
Definition of a vector holding unique pointers of factories.
Definition: devices/Manager.h:66
void release()
Destructs all registered libraries and their corresponding factories.
ObjectIdMultimap objectIdMultimap_
The map mapping object ids to descriptions.
Definition: devices/Manager.h:192
bool registerFactory(std::unique_ptr< Factory > &&factory)
Registers a new factory.
static std::string nameAdhocFactory()
Returns the name of the ad-hoc factory.
Strings devices(const Device::DeviceType type) const
Returns a list with the names of all available devices matching a major and minor device type.
This class implements the base class for all devices providing measurement samples.
Definition: Measurement.h:40
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
This class implements a recursive lock object.
Definition: Lock.h:31
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
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