Ocean
Loading...
Searching...
No Matches
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
15namespace Ocean
16{
17
18namespace Devices
19{
20
21namespace ARCore
22{
23
24// Forward declaration.
25template <typename T, void (*tDestroyFunction)(T*)>
26class 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 ArFrame.
51 * @see ScopedARObject
52 * @ingroup devicesarcore
53 */
55
56/**
57 * Definition of a scoped object for ArImage.
58 * @see ScopedARObject
59 * @ingroup devicesarcore
60 */
62
63/**
64 * Definition of a scoped object for ArCamera.
65 * @see ScopedARObject
66 * @ingroup devicesarcore
67 */
69
70/**
71 * Definition of a scoped object for ArCameraConfig.
72 * @see ScopedARObject
73 * @ingroup devicesarcore
74 */
76
77/**
78 * Definition of a scoped object for ArCameraConfigList.
79 * @see ScopedARObject
80 * @ingroup devicesarcore
81 */
83
84/**
85 * Definition of a scoped object for ArCameraConfigFilter.
86 * @see ScopedARObject
87 * @ingroup devicesarcore
88 */
90
91/**
92 * Definition of a scoped object for ArCameraIntrinsics.
93 * @see ScopedARObject
94 * @ingroup devicesarcore
95 */
97
98/**
99 * Definition of a scoped object for ArTrackable.
100 * @see ScopedARObject
101 * @ingroup devicesarcore
102 */
104
105/**
106 * Definition of a scoped object for ArTrackableList.
107 * @see ScopedARObject
108 * @ingroup devicesarcore
109 */
111
112/**
113 * Definition of a scoped object for ArPointCloud.
114 * @see ScopedARObject
115 * @ingroup devicesarcore
116 */
118
119/**
120 * This class implements a scoped object for ARCore objects.
121 * The class is a helper class to ensure that objects are always destoyed after usage.
122 * @tparam T The data type of the object
123 * @tparam tDestroyFunction The static function to destroy the object
124 * @ingroup devicesarcore
125 */
126template <typename T, void (*tDestroyFunction)(T*)>
128{
129 public:
130
131 /**
132 * Definition of a function pointer to a function creating the object.
133 * @param arSession The ARCore session for which the object will be created
134 * @param object The resulting created object
135 */
136 using CreateFunction = void(*)(const ArSession* arSession, T** object);
137
138 public:
139
140 /**
141 * Default constructor creating an invalid object.
142 */
143 ScopedARObject() = default;
144
145 /**
146 * Creates a new scoped ARCore object.
147 * @param object The actual object which will be destroyed once this object is disposed, must be valid
148 */
149 explicit inline ScopedARObject(T* object);
150
151 /**
152 * Creates a new scoped ARCore object.
153 * @param arSession The ARCore session for which the object will be created
154 * @param createFunction The create function which will be used to create the new object, must be valid
155 */
156 inline ScopedARObject(const ArSession* arSession, const CreateFunction& createFunction);
157
158 /**
159 * Move constructor.
160 * @param arScopedObject The scoped object to be moved
161 */
163
164 /**
165 * Destructs the object and destroys it.
166 */
168
169 /**
170 * Allows ingesting an object via an external function.
171 * In case this scoped object already holds an existing object, the existing object will be released first.
172 * @return The pointer to the internal object's pointer.
173 */
174 inline T** ingest();
175
176 /**
177 * Explicitly releases the scoped object.
178 */
179 inline void release();
180
181 /**
182 * Returns the actual object.
183 * @return The object of this object
184 */
185 inline operator T*() const;
186
187 /**
188 * Returns whether this scoped object holds an actual object.
189 * @return True, if so
190 */
191 explicit inline operator bool() const;
192
193 /**
194 * Move operator.
195 * @param arScopedObject The object to be moved
196 * @return Reference to this object
197 */
199
200 protected:
201
202 /**
203 * Disabled copy constructor.
204 */
206
207 /**
208 * Disabled assign operator.
209 */
211
212 protected:
213
214 /// The actual object.
215 T* object_ = nullptr;
216};
217
218template <typename T, void (*tDestroyFunction)(T*)>
220 object_(object)
221{
222 static_assert(tDestroyFunction != nullptr, "Invalid destroy function!");
223}
224
225template <typename T, void (*tDestroyFunction)(T*)>
226inline ScopedARObject<T, tDestroyFunction>::ScopedARObject(const ArSession* arSession, const CreateFunction& createFunction)
227{
228 ocean_assert(arSession != nullptr);
229 ocean_assert(createFunction != nullptr);
230
231 createFunction(arSession, &object_);
232 ocean_assert(object_ != nullptr);
233}
234
235template <typename T, void (*tDestroyFunction)(T*)>
237{
238 *this = std::move(arScopedObject);
239}
240
241template <typename T, void (*tDestroyFunction)(T*)>
246
247template <typename T, void (*tDestroyFunction)(T*)>
249{
250 release();
251
252 return &object_;
253}
254
255template <typename T, void (*tDestroyFunction)(T*)>
257{
258 if (object_ != nullptr)
259 {
260 tDestroyFunction(object_);
261 object_ = nullptr;
262 }
263}
264
265template <typename T, void (*tDestroyFunction)(T*)>
267{
268 return object_;
269}
270
271template <typename T, void (*tDestroyFunction)(T*)>
273{
274 return object_ != nullptr;
275}
276
277template <typename T, void (*tDestroyFunction)(T*)>
279{
280 if (this != &arScopedObject)
281 {
282 release();
283
284 object_ = arScopedObject.object_;
285 arScopedObject.object_ = nullptr;
286 }
287
288 return *this;
289}
290
291}
292
293}
294
295}
296
297#endif // META_OCEAN_DEVICES_ARCORE_AC_SCOPED_AR_OBJECT_H
T ** ingest()
Allows ingesting an object via an external function.
Definition ScopedARObject.h:248
void(*)(const ArSession *arSession, T **object) CreateFunction
Definition of a function pointer to a function creating the object.
Definition ScopedARObject.h:136
~ScopedARObject()
Destructs the object and destroys it.
Definition ScopedARObject.h:242
ScopedARObject< T, tDestroyFunction > & operator=(ScopedARObject< T, tDestroyFunction > &&arScopedObject)
Move operator.
Definition ScopedARObject.h:278
ScopedARObject(T *object)
Creates a new scoped ARCore object.
Definition ScopedARObject.h:219
ScopedARObject(const ArSession *arSession, const CreateFunction &createFunction)
Creates a new scoped ARCore object.
Definition ScopedARObject.h:226
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:256
ScopedARObject< T, tDestroyFunction > & operator=(const ScopedARObject< T, tDestroyFunction > &)=delete
Disabled assign operator.
ScopedARObject(ScopedARObject< T, tDestroyFunction > &&arScopedObject)
Move constructor.
Definition ScopedARObject.h:236
T * object_
The actual object.
Definition ScopedARObject.h:215
The namespace covering the entire Ocean framework.
Definition Accessor.h:15