Ocean
media/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_MEDIA_MANAGER_H
9 #define META_OCEAN_MEDIA_MANAGER_H
10 
11 #include "ocean/media/Media.h"
12 #include "ocean/media/Library.h"
13 #include "ocean/media/MediumRef.h"
14 #include "ocean/media/Recorder.h"
15 
16 #include "ocean/base/Singleton.h"
17 
18 #include <vector>
19 
20 namespace Ocean
21 {
22 
23 namespace Media
24 {
25 
26 /**
27  * This class is the manager for all media objects.
28  * As media objects cannot be created directly this manager is necessary to create individual media object.<br>
29  * Further, this manager encapsulates individual media libraries and allows to create media objects from specific or random libraries during one unique interface.<br>
30  * @see Library, Medium.
31  * @ingroup media
32  */
33 class OCEAN_MEDIA_EXPORT Manager : public Singleton<Manager>
34 {
35  friend class Singleton<Manager>;
36  friend class Library;
37 
38  public:
39 
40  /**
41  * Definition of a vector holding library names.
42  */
43  typedef std::vector<std::string> Names;
44 
45  private:
46 
47  /**
48  * Definition of a pair combining a library with a reference counter.
49  */
50  typedef std::pair<LibraryRef, unsigned int> LibraryCounterPair;
51 
52  /**
53  * Definition of a vector holding library pairs.
54  */
55  typedef std::vector<LibraryCounterPair> Libraries;
56 
57  public:
58 
59  /**
60  * Creates a new medium by a given url.
61  * If no medium can be created an empty reference is returned.
62  * @param url Url of the medium
63  * @param useExclusive Determines whether the caller would like to use this medium exclusively
64  * @return Reference to the new medium
65  */
66  MediumRef newMedium(const std::string& url, bool useExclusive = false);
67 
68  /**
69  * Creates a new medium by a given url and an expected type.
70  * If no medium can be created an empty reference is returned.
71  * @param url Url of the medium
72  * @param type Type of the expected medium
73  * @param useExclusive Determines whether the caller would like to use this medium exclusively
74  * @return Reference to the new medium
75  */
76  MediumRef newMedium(const std::string& url, const Medium::Type type, bool useExclusive = false);
77 
78  /**
79  * Creates a new medium by a given url, a library name and an expected type.
80  * If no medium can be created an empty reference is returned.
81  * @param url Url of the medium
82  * @param library Name of the library to use for this medium
83  * @param type Type of the expected medium
84  * @param useExclusive Determines whether the caller would like to use this medium exclusively
85  * @return Reference to the new medium
86  */
87  MediumRef newMedium(const std::string& url, const std::string& library, const Medium::Type type, bool useExclusive = false);
88 
89  /**
90  * Creates a new recorder specified by the recorder type.
91  * @param type Type of the recorder to return
92  * @param library The optional name of the library to be used, empty to use any library
93  * @return Reference to the new recorder
94  */
95  RecorderRef newRecorder(const Recorder::Type type, const std::string& library = std::string());
96 
97  /**
98  * Returns a list of selectable mediums.
99  * @return Selectable mediums
100  */
102 
103  /**
104  * Returns a list of specific selectable mediums.
105  * @param type Type of the selectable mediums
106  * @return Selectable mediums
107  */
109 
110  /**
111  * Returns the names currently registered media libraries.
112  * @return Registered media libraries
113  */
114  Names libraries() const;
115 
116  /**
117  * Releases all registered libraries.
118  * This function should be called once before the application is shutting down.
119  * However this function should be called after all medium reference have been released.
120  */
121  void release();
122 
123  /**
124  * Creates a new manager object.
125  */
127 
128  /**
129  * Destructs the manager, called by the singleton object.
130  * Beware: The release function should be called before the singleton invokes the destructor.
131  * In common cases the singleton is released to late for internal library objects.
132  */
133  virtual ~Manager();
134 
135  /**
136  * Registers a new library.
137  * With each register call, the reference counter for a specific library will be incremented.
138  * Each call to registerLibrary() needs to be balanced with a corresponding call of unregisterLibrary() before shutting down.
139  * @param name The name of the library to register, must be valid
140  * @return True, if the library has not been registered before
141  * @tparam T The data type of the library to register
142  * @see unregisterLibrary().
143  */
144  template <typename T>
145  bool registerLibrary(const std::string& name);
146 
147  /**
148  * Unregisters a library.
149  * With each unregister call, the reference counter for a specific library will be decremented and removed from the system if the counter reaches zero.
150  * Each call to registerLibrary() needs to be balanced with a corresponding call of unregisterLibrary() before shutting down.
151  * @param name The name of the library to unregister, must be valid
152  * @return True, if the library was actually removed from the system (as the reference counter reached zero); False, if the library is still used by someone else
153  * @see registerLibrary().
154  */
155  bool unregisterLibrary(const std::string& name);
156 
157  protected:
158 
159  /// Registered libraries.
161 
162  /// Lock for the libraries.
163  mutable Lock lock_;
164 };
165 
166 template <typename T>
167 bool Manager::registerLibrary(const std::string& name)
168 {
169  const ScopedLock scopedLock(lock_);
170 
171  // first we check whether the library has been registered already
172 
173  for (Libraries::iterator i = libraries_.begin(); i != libraries_.end(); ++i)
174  {
175  ocean_assert(i->first);
176 
177  if (i->first->name() == name)
178  {
179  ++i->second;
180  return false;
181  }
182  }
183 
184  // the library has not been registered before, so we insert the library based on the priority
185 
186  LibraryRef newLibrary = T::create();
187 
188  for (Libraries::iterator i = libraries_.begin(); i != libraries_.end(); ++i)
189  {
190  ocean_assert(i->first);
191 
192  if (i->first->priority() < newLibrary->priority())
193  {
194  libraries_.insert(i, std::make_pair(std::move(newLibrary), 1u));
195  return true;
196  }
197  }
198 
199  // the library goes to the end
200 
201  libraries_.emplace_back(std::make_pair(std::move(newLibrary), 1u));
202  return true;
203 }
204 
205 }
206 
207 }
208 
209 #endif
This class implements a recursive lock object.
Definition: Lock.h:31
This class is the base class for all media libraries.
Definition: media/Library.h:42
std::vector< Definition > Definitions
Definition of a vector holding medium definition objects.
Definition: media/Library.h:111
This class is the manager for all media objects.
Definition: media/Manager.h:34
MediumRef newMedium(const std::string &url, bool useExclusive=false)
Creates a new medium by a given url.
MediumRef newMedium(const std::string &url, const Medium::Type type, bool useExclusive=false)
Creates a new medium by a given url and an expected type.
bool registerLibrary(const std::string &name)
Registers a new library.
Definition: media/Manager.h:167
std::pair< LibraryRef, unsigned int > LibraryCounterPair
Definition of a pair combining a library with a reference counter.
Definition: media/Manager.h:50
virtual ~Manager()
Destructs the manager, called by the singleton object.
std::vector< LibraryCounterPair > Libraries
Definition of a vector holding library pairs.
Definition: media/Manager.h:55
Library::Definitions selectableMedia(const Medium::Type type) const
Returns a list of specific selectable mediums.
bool unregisterLibrary(const std::string &name)
Unregisters a library.
RecorderRef newRecorder(const Recorder::Type type, const std::string &library=std::string())
Creates a new recorder specified by the recorder type.
MediumRef newMedium(const std::string &url, const std::string &library, const Medium::Type type, bool useExclusive=false)
Creates a new medium by a given url, a library name and an expected type.
Manager()
Creates a new manager object.
void release()
Releases all registered libraries.
Names libraries() const
Returns the names currently registered media libraries.
Libraries libraries_
Registered libraries.
Definition: media/Manager.h:160
Library::Definitions selectableMedia() const
Returns a list of selectable mediums.
std::vector< std::string > Names
Definition of a vector holding library names.
Definition: media/Manager.h:43
Lock lock_
Lock for the libraries.
Definition: media/Manager.h:163
Type
Definition of different medium types.
Definition: Medium.h:57
Type
Definition of different recorder types.
Definition: Recorder.h:46
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
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15