#ifndef DEVICE_VR_OPENXR_SCOPED_OPENXR_OBJECT_H_
#define DEVICE_VR_OPENXR_SCOPED_OPENXR_OBJECT_H_
#include "base/memory/raw_ref.h"
#include "device/vr/openxr/openxr_extension_helper.h"
#include "third_party/openxr/src/include/openxr/openxr.h"
namespace device {
template <typename T>
class ScopedOpenXrObject {
public:
explicit ScopedOpenXrObject(const OpenXrExtensionHelper& extension_helper,
T object = XR_NULL_HANDLE)
: extension_helper_(extension_helper), object_(object) {}
~ScopedOpenXrObject() { Free(); }
ScopedOpenXrObject(const ScopedOpenXrObject&) = delete;
ScopedOpenXrObject& operator=(const ScopedOpenXrObject&) = delete;
const T& get() const { return object_; }
void Reset(T object = XR_NULL_HANDLE) {
Free();
object_ = object;
}
T* receive() {
Reset();
return &object_;
}
private:
void Free();
const raw_ref<const OpenXrExtensionHelper> extension_helper_;
T object_;
};
template <>
inline void ScopedOpenXrObject<XrSpatialSnapshotEXT>::Free() {
if (object_ != XR_NULL_HANDLE) {
extension_helper_->ExtensionMethods().xrDestroySpatialSnapshotEXT(object_);
}
}
}
#endif