Ocean
MediumRef.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_MEDIUM_REF_H
9 #define META_OCEAN_MEDIA_MEDIUM_REF_H
10 
11 #include "ocean/media/Media.h"
12 #include "ocean/media/Medium.h"
13 
14 #include "ocean/base/ObjectRef.h"
15 #include "ocean/base/Singleton.h"
17 
18 #include <map>
19 
20 namespace Ocean
21 {
22 
23 namespace Media
24 {
25 
26 /**
27  * This class implements a smart medium reference.
28  * @tparam T Type of the derived object that has to be encapsulated by the smart reference object
29  * @ingroup media
30  */
31 template <typename T>
32 class SmartMediumRef : public SmartObjectRef<T, Medium>
33 {
34  private:
35 
36  /**
37  * Redefinition of the release callback function defined in ObjectRef.
38  */
40 
41  public:
42 
43  /**
44  * Creates an empty smart medium reference.
45  */
47 
48  /**
49  * Creates a new smart medium reference by a given medium reference.
50  * @param mediumRef Medium reference to copy
51  */
52  SmartMediumRef(const MediumRef& mediumRef);
53 
54  /**
55  * Creates a new SmartMediumRef by a given object.
56  * This given object will be released by the smart medium reference itself.
57  * @param object Internal object
58  */
59  explicit SmartMediumRef(Medium* object);
60 
61  /**
62  * Copies a smart medium reference.
63  * @param reference Reference to copy
64  */
65  template <typename T2> SmartMediumRef(const SmartMediumRef<T2>& reference);
66 
67  /**
68  * Assigns a smart medium reference.
69  * @param mediumRef reference Reference to assign
70  * @return Reference to this object
71  */
72  SmartMediumRef& operator=(const MediumRef& mediumRef);
73 };
74 
75 /**
76  * This class implements a medium reference manager.
77  * @ingroup media
78  */
79 class OCEAN_MEDIA_EXPORT MediumRefManager : public Singleton<MediumRefManager>
80 {
81  friend class Singleton<MediumRefManager>;
82  friend class ObjectRef<Medium>;
83  friend class Medium;
84  friend class Manager;
85 
86  protected:
87 
88  /**
89  * Map mapping urls to medium references.
90  */
91  typedef std::multimap<std::string, MediumRef> MediumMap;
92 
93  public:
94 
95  /**
96  * Registers a new medium.
97  * @param medium Medium object to manage
98  * @return Medium reference
99  */
101 
102  /**
103  * Returns a medium by a given url.
104  * If the medium does not exist an empty reference is returned.
105  * @param url Url of the new medium
106  * @param library Name of the owner library
107  * @param type Type of the expected medium
108  * @return Medium reference of the requested url
109  */
110  MediumRef medium(const std::string& url, const std::string& library, const Medium::Type type);
111 
112  protected:
113 
114  /**
115  * Destructs the manager.
116  */
118 
119  /**
120  * Returns a medium by a given url.
121  * If the medium does not exist an empty reference is returned.
122  * @param url Url of the medium that has to be returned
123  * @return Medium reference of the requested url
124  */
125  MediumRef medium(const std::string& url);
126 
127  /**
128  * Returns a medium by a given url.
129  * If the medium does not exist an empty reference is returned.
130  * @param url Url of the requested medium
131  * @param type Type of the expected medium
132  * @return Medium reference of the requested url
133  */
134  MediumRef medium(const std::string& url, const Medium::Type type);
135 
136  /**
137  * Returns whether a medium is registered at this manager.
138  * @param medium Medium to check
139  * @return True, if so
140  */
141  bool isRegistered(const Medium* medium);
142 
143  /**
144  * Unregisters a medium.
145  * @param medium Medium to unregister
146  */
147  void unregisterMedium(const Medium* medium);
148 
149  protected:
150 
151  /// Map holding all medium references.
153 
154  /// Lock for the medium map
156 };
157 
158 template <typename T>
160  SmartObjectRef<T, Medium>()
161 {
162  // nothing to do here
163 }
164 
165 template <typename T>
167  SmartObjectRef<T, Medium>(mediumRef)
168 {
169  // nothing to do here
170 }
171 
172 template <typename T>
174  SmartObjectRef<T, Medium>(object, ReleaseCallback(MediumRefManager::get(), &MediumRefManager::unregisterMedium))
175 {
176  // nothing to do here
177 }
178 
179 template <typename T>
180 template <typename T2>
182  SmartObjectRef<T, Medium>(reference)
183 {
184  // nothing to do here
185 }
186 
187 template <typename T>
189 {
191  return *this;
192 }
193 
194 }
195 
196 }
197 
198 #endif // META_OCEAN_MEDIA_MEDIUM_REF_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class is the manager for all media objects.
Definition: media/Manager.h:34
This is the base class for all mediums.
Definition: Medium.h:48
Type
Definition of different medium types.
Definition: Medium.h:57
This class implements a medium reference manager.
Definition: MediumRef.h:80
MediumRef medium(const std::string &url)
Returns a medium by a given url.
MediumRef medium(const std::string &url, const Medium::Type type)
Returns a medium by a given url.
MediumRef registerMedium(Medium *medium)
Registers a new medium.
MediumMap mediumMap
Map holding all medium references.
Definition: MediumRef.h:152
~MediumRefManager()
Destructs the manager.
bool isRegistered(const Medium *medium)
Returns whether a medium is registered at this manager.
std::multimap< std::string, MediumRef > MediumMap
Map mapping urls to medium references.
Definition: MediumRef.h:91
void unregisterMedium(const Medium *medium)
Unregisters a medium.
Lock lock
Lock for the medium map.
Definition: MediumRef.h:155
MediumRef medium(const std::string &url, const std::string &library, const Medium::Type type)
Returns a medium by a given url.
This class implements a smart medium reference.
Definition: MediumRef.h:33
SmartMediumRef(const SmartMediumRef< T2 > &reference)
Copies a smart medium reference.
Definition: MediumRef.h:181
SmartMediumRef()
Creates an empty smart medium reference.
Definition: MediumRef.h:159
SmartMediumRef(Medium *object)
Creates a new SmartMediumRef by a given object.
Definition: MediumRef.h:173
SmartMediumRef(const MediumRef &mediumRef)
Creates a new smart medium reference by a given medium reference.
Definition: MediumRef.h:166
SmartMediumRef & operator=(const MediumRef &mediumRef)
Assigns a smart medium reference.
Definition: MediumRef.h:188
SmartObjectRef< T, Medium >::ReleaseCallback ReleaseCallback
Redefinition of the release callback function defined in ObjectRef.
Definition: MediumRef.h:39
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
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
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15