aboutsummaryrefslogtreecommitdiffstats
path: root/OpenAL32/Include
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-10-31 10:05:15 -0700
committerChris Robinson <[email protected]>2018-10-31 10:05:15 -0700
commit1e8c6df7b99f1db7aa38c91c885ed9866c9a7873 (patch)
tree567d2ae7e56a8526b1c0b18c46aac50369b0c579 /OpenAL32/Include
parentda150572f94ec354c8061c0bf1aafa0926c6dbc7 (diff)
Add a C++ ContextRef helper to manage a ALCcontext reference
Diffstat (limited to 'OpenAL32/Include')
-rw-r--r--OpenAL32/Include/alMain.h36
1 files changed, 35 insertions, 1 deletions
diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h
index 0fd77491..b3380ae2 100644
--- a/OpenAL32/Include/alMain.h
+++ b/OpenAL32/Include/alMain.h
@@ -914,7 +914,41 @@ int EventThread(void *arg);
vector_al_string SearchDataFiles(const char *match, const char *subdir);
#ifdef __cplusplus
-}
+} // extern "C"
+
+/* Simple RAII context reference. Takes the reference of the provided
+ * ALCcontext, and decrements it when leaving scope. Movable (transfer
+ * reference) but not copyable (no new references).
+ */
+class ContextRef {
+ ALCcontext *mCtx{nullptr};
+
+ void release() noexcept
+ {
+ if(mCtx)
+ ALCcontext_DecRef(mCtx);
+ mCtx = nullptr;
+ }
+
+public:
+ ContextRef() noexcept = default;
+ explicit ContextRef(ALCcontext *ctx) noexcept : mCtx(ctx) { }
+ ~ContextRef() { release(); }
+
+ ContextRef& operator=(const ContextRef&) = delete;
+ ContextRef& operator=(ContextRef&& rhs) noexcept
+ {
+ release();
+ mCtx = rhs.mCtx;
+ rhs.mCtx = nullptr;
+ return *this;
+ }
+
+ operator bool() const noexcept { return static_cast<bool>(mCtx); }
+
+ ALCcontext* operator->() noexcept { return mCtx; }
+ ALCcontext* get() noexcept { return mCtx; }
+};
#endif
#endif