Ocean
ScopedARObject.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_ARCORE_AC_SCOPED_AR_OBJECT_H
9 #define META_OCEAN_DEVICES_ARCORE_AC_SCOPED_AR_OBJECT_H
10 
12 
13 #include <arcore_c_api.h>
14 
15 namespace Ocean
16 {
17 
18 namespace Devices
19 {
20 
21 namespace ARCore
22 {
23 
24 // Forward declaration.
25 template <typename T, void (*tDestroyFunction)(T*)>
26 class ScopedARObject;
27 
28 /**
29  * Definition of a scoped object for ArSession.
30  * @see ScopedARObject
31  * @ingroup devicesarcore
32  */
34 
35 /**
36  * Definition of a scoped object for ArConfig.
37  * @see ScopedARObject
38  * @ingroup devicesarcore
39  */
41 
42 /**
43  * Definition of a scoped object for ArPose.
44  * @see ScopedARObject
45  * @ingroup devicesarcore
46  */
48 
49 /**
50  * Definition of a scoped object for ArImage.
51  * @see ScopedARObject
52  * @ingroup devicesarcore
53  */
55 
56 /**
57  * Definition of a scoped object for ArCameraConfig.
58  * @see ScopedARObject
59  * @ingroup devicesarcore
60  */
62 
63 /**
64  * Definition of a scoped object for ArCameraConfigList.
65  * @see ScopedARObject
66  * @ingroup devicesarcore
67  */
69 
70 /**
71  * Definition of a scoped object for ArCameraConfigFilter.
72  * @see ScopedARObject
73  * @ingroup devicesarcore
74  */
76 
77 /**
78  * Definition of a scoped object for ArCameraIntrinsics.
79  * @see ScopedARObject
80  * @ingroup devicesarcore
81  */
83 
84 /**
85  * Definition of a scoped object for ArTrackable.
86  * @see ScopedARObject
87  * @ingroup devicesarcore
88  */
90 
91 /**
92  * Definition of a scoped object for ArTrackableList.
93  * @see ScopedARObject
94  * @ingroup devicesarcore
95  */
97 
98 /**
99  * Definition of a scoped object for ArPointCloud.
100  * @see ScopedARObject
101  * @ingroup devicesarcore
102  */
104 
105 /**
106  * This class implements a scoped object for ARCore objects.
107  * The class is a helper class to ensure that objects are always destoyed after usage.
108  * @tparam T The data type of the object
109  * @tparam tDestroyFunction The static function to destroy the object
110  * @ingroup devicesarcore
111  */
112 template <typename T, void (*tDestroyFunction)(T*)>
114 {
115  public:
116 
117  /**
118  * Definition of a function pointer to a function creating the object.
119  * @param arSession The ARCore session for which the object will be created
120  * @param object The resulting created object
121  */
122  typedef void(*CreateFunction)(const ArSession* arSession, T** object);
123 
124  public:
125 
126  /**
127  * Default constructor creating an invalid object.
128  */
129  ScopedARObject() = default;
130 
131  /**
132  * Creates a new scoped ARCore object.
133  * @param object The actual object which will be destroyed once this object is disposed, must be valid
134  */
135  explicit inline ScopedARObject(T* object);
136 
137  /**
138  * Creates a new scoped ARCore object.
139  * @param arSession The ARCore session for which the object will be created
140  * @param createFunction The create function which will be used to create the new object, must be valid
141  */
142  inline ScopedARObject(const ArSession* arSession, const CreateFunction& createFunction);
143 
144  /**
145  * Move constructor.
146  * @param arScopedObject The scoped object to be moved
147  */
149 
150  /**
151  * Destructs the object and destroys it.
152  */
153  inline ~ScopedARObject();
154 
155  /**
156  * Allows to ingest an object via an external function.
157  * In case this scoped object already holds an existing object, the existing object will be released first.
158  * @return The pointer to the internal object's pointer.
159  */
160  inline T** ingest();
161 
162  /**
163  * Explicitly releases the scoped object.
164  */
165  inline void release();
166 
167  /**
168  * Returns the actual object.
169  * @return The object of this object
170  */
171  inline operator T*() const;
172 
173  /**
174  * Returns whether this scoped object holds an actual object.
175  * @return True, if so
176  */
177  explicit inline operator bool() const;
178 
179  /**
180  * Move operator.
181  * @param arScopedObject The object to be moved
182  * @return Reference to this object
183  */
185 
186  protected:
187 
188  /**
189  * Disabled copy constructor.
190  */
192 
193  /**
194  * Disabled assign operator.
195  */
197 
198  protected:
199 
200  /// The actual object.
201  T* object_ = nullptr;
202 };
203 
204 template <typename T, void (*tDestroyFunction)(T*)>
206  object_(object)
207 {
208  static_assert(tDestroyFunction != nullptr, "Invalid destroy function!");
209 }
210 
211 template <typename T, void (*tDestroyFunction)(T*)>
212 inline ScopedARObject<T, tDestroyFunction>::ScopedARObject(const ArSession* arSession, const CreateFunction& createFunction)
213 {
214  ocean_assert(arSession != nullptr);
215  ocean_assert(createFunction != nullptr);
216 
217  createFunction(arSession, &object_);
218  ocean_assert(object_ != nullptr);
219 }
220 
221 template <typename T, void (*tDestroyFunction)(T*)>
223 {
224  *this = std::move(arScopedObject);
225 }
226 
227 template <typename T, void (*tDestroyFunction)(T*)>
229 {
230  release();
231 }
232 
233 template <typename T, void (*tDestroyFunction)(T*)>
235 {
236  release();
237 
238  return &object_;
239 }
240 
241 template <typename T, void (*tDestroyFunction)(T*)>
243 {
244  if (object_ != nullptr)
245  {
246  tDestroyFunction(object_);
247  object_ = nullptr;
248  }
249 }
250 
251 template <typename T, void (*tDestroyFunction)(T*)>
253 {
254  return object_;
255 }
256 
257 template <typename T, void (*tDestroyFunction)(T*)>
259 {
260  return object_ != nullptr;
261 }
262 
263 template <typename T, void (*tDestroyFunction)(T*)>
265 {
266  if (this != &arScopedObject)
267  {
268  release();
269 
270  object_ = arScopedObject.object_;
271  arScopedObject.object_ = nullptr;
272  }
273 
274  return *this;
275 }
276 
277 }
278 
279 }
280 
281 }
282 
283 #endif // META_OCEAN_DEVICES_ARCORE_AC_SCOPED_AR_OBJECT_H
T ** ingest()
Allows to ingest an object via an external function.
Definition: ScopedARObject.h:234
~ScopedARObject()
Destructs the object and destroys it.
Definition: ScopedARObject.h:228
ScopedARObject< T, tDestroyFunction > & operator=(ScopedARObject< T, tDestroyFunction > &&arScopedObject)
Move operator.
Definition: ScopedARObject.h:264
ScopedARObject(T *object)
Creates a new scoped ARCore object.
Definition: ScopedARObject.h:205
ScopedARObject(const ArSession *arSession, const CreateFunction &createFunction)
Creates a new scoped ARCore object.
Definition: ScopedARObject.h:212
ScopedARObject(const ScopedARObject< T, tDestroyFunction > &)=delete
Disabled copy constructor.
ScopedARObject()=default
Default constructor creating an invalid object.
void release()
Explicitly releases the scoped object.
Definition: ScopedARObject.h:242
ScopedARObject< T, tDestroyFunction > & operator=(const ScopedARObject< T, tDestroyFunction > &)=delete
Disabled assign operator.
ScopedARObject(ScopedARObject< T, tDestroyFunction > &&arScopedObject)
Move constructor.
Definition: ScopedARObject.h:222
void(* CreateFunction)(const ArSession *arSession, T **object)
Definition of a function pointer to a function creating the object.
Definition: ScopedARObject.h:122
T * object_
The actual object.
Definition: ScopedARObject.h:201
ScopedARObject< ArImage, ArImage_release > ScopedARImage
Definition of a scoped object for ArImage.
Definition: ScopedARObject.h:54
ScopedARObject< ArSession, ArSession_destroy > ScopedARSession
Definition of a scoped object for ArSession.
Definition: ScopedARObject.h:26
ScopedARObject< ArCameraIntrinsics, ArCameraIntrinsics_destroy > ScopedARCameraIntrinsics
Definition of a scoped object for ArCameraIntrinsics.
Definition: ScopedARObject.h:82
ScopedARObject< ArCameraConfigList, ArCameraConfigList_destroy > ScopedARCameraConfigList
Definition of a scoped object for ArCameraConfigList.
Definition: ScopedARObject.h:68
ScopedARObject< ArCameraConfig, ArCameraConfig_destroy > ScopedARCameraConfig
Definition of a scoped object for ArCameraConfig.
Definition: ScopedARObject.h:61
ScopedARObject< ArTrackableList, ArTrackableList_destroy > ScopedARTrackableList
Definition of a scoped object for ArTrackableList.
Definition: ScopedARObject.h:96
ScopedARObject< ArPose, ArPose_destroy > ScopedARPose
Definition of a scoped object for ArPose.
Definition: ScopedARObject.h:47
ScopedARObject< ArTrackable, ArTrackable_release > ScopedARTrackable
Definition of a scoped object for ArTrackable.
Definition: ScopedARObject.h:89
ScopedARObject< ArConfig, ArConfig_destroy > ScopedARConfig
Definition of a scoped object for ArConfig.
Definition: ScopedARObject.h:40
ScopedARObject< ArPointCloud, ArPointCloud_release > ScopedARPointCloud
Definition of a scoped object for ArPointCloud.
Definition: ScopedARObject.h:103
ScopedARObject< ArCameraConfigFilter, ArCameraConfigFilter_destroy > ScopedARCameraConfigFilter
Definition of a scoped object for ArCameraConfigFilter.
Definition: ScopedARObject.h:75
void release(T *object)
This functions allows to release a DirectShow object if it does exist.
Definition: DSObject.h:266
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15