aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2021-03-08 22:47:50 -0800
committerChris Robinson <[email protected]>2021-03-08 22:47:50 -0800
commit1d57db6836fd577e66bafc84095d672d288e4552 (patch)
tree4e126186fdf13e4870646e554970a46255c42a25
parent730c964029f7b649510490d8766aba801f576492 (diff)
Move the ComPtr wrapper to a common header
-rw-r--r--CMakeLists.txt1
-rw-r--r--alc/backends/dsound.cpp64
-rw-r--r--alc/backends/wasapi.cpp64
-rw-r--r--common/comptr.h70
4 files changed, 73 insertions, 126 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0cf0613d..cd9e6d9b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -621,6 +621,7 @@ set(COMMON_OBJS
common/alstring.cpp
common/alstring.h
common/atomic.h
+ common/comptr.h
common/dynload.cpp
common/dynload.h
common/intrusive_ptr.h
diff --git a/alc/backends/dsound.cpp b/alc/backends/dsound.cpp
index 34fe25f4..72f9c47a 100644
--- a/alc/backends/dsound.cpp
+++ b/alc/backends/dsound.cpp
@@ -47,6 +47,7 @@
#include "alcmain.h"
#include "alu.h"
#include "compat.h"
+#include "comptr.h"
#include "core/logging.h"
#include "dynload.h"
#include "ringbuffer.h"
@@ -107,69 +108,6 @@ HRESULT (WINAPI *pDirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW pDSEnumCallbac
#endif
-template<typename T>
-class ComPtr {
- T *mPtr{nullptr};
-
-public:
- ComPtr() noexcept = default;
- ComPtr(const ComPtr &rhs) : mPtr{rhs.mPtr} { if(mPtr) mPtr->AddRef(); }
- ComPtr(ComPtr&& rhs) noexcept : mPtr{rhs.mPtr} { rhs.mPtr = nullptr; }
- ComPtr(std::nullptr_t) noexcept { }
- explicit ComPtr(T *ptr) noexcept : mPtr{ptr} { }
- ~ComPtr() { if(mPtr) mPtr->Release(); }
-
- ComPtr& operator=(const ComPtr &rhs)
- {
- if(!rhs.mPtr)
- {
- if(mPtr)
- mPtr->Release();
- mPtr = nullptr;
- }
- else
- {
- rhs.mPtr->AddRef();
- try {
- if(mPtr)
- mPtr->Release();
- mPtr = rhs.mPtr;
- }
- catch(...) {
- rhs.mPtr->Release();
- throw;
- }
- }
- return *this;
- }
- ComPtr& operator=(ComPtr&& rhs)
- {
- if(mPtr)
- mPtr->Release();
- mPtr = rhs.mPtr;
- rhs.mPtr = nullptr;
- return *this;
- }
-
- operator bool() const noexcept { return mPtr != nullptr; }
-
- T& operator*() const noexcept { return *mPtr; }
- T* operator->() const noexcept { return mPtr; }
- T* get() const noexcept { return mPtr; }
- T** getPtr() noexcept { return &mPtr; }
-
- T* release() noexcept
- {
- T *ret{mPtr};
- mPtr = nullptr;
- return ret;
- }
-
- void swap(ComPtr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
- void swap(ComPtr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
-};
-
-
#define MONO SPEAKER_FRONT_CENTER
#define STEREO (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT)
#define QUAD (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp
index b594aebe..3dd08613 100644
--- a/alc/backends/wasapi.cpp
+++ b/alc/backends/wasapi.cpp
@@ -59,6 +59,7 @@
#include "alcmain.h"
#include "alu.h"
#include "compat.h"
+#include "comptr.h"
#include "converter.h"
#include "core/logging.h"
#include "ringbuffer.h"
@@ -129,69 +130,6 @@ inline uint RefTime2Samples(const ReferenceTime &val, uint srate)
}
-template<typename T>
-class ComPtr {
- T *mPtr{nullptr};
-
-public:
- ComPtr() noexcept = default;
- ComPtr(const ComPtr &rhs) : mPtr{rhs.mPtr} { if(mPtr) mPtr->AddRef(); }
- ComPtr(ComPtr&& rhs) noexcept : mPtr{rhs.mPtr} { rhs.mPtr = nullptr; }
- ComPtr(std::nullptr_t) noexcept { }
- explicit ComPtr(T *ptr) noexcept : mPtr{ptr} { }
- ~ComPtr() { if(mPtr) mPtr->Release(); }
-
- ComPtr& operator=(const ComPtr &rhs)
- {
- if(!rhs.mPtr)
- {
- if(mPtr)
- mPtr->Release();
- mPtr = nullptr;
- }
- else
- {
- rhs.mPtr->AddRef();
- try {
- if(mPtr)
- mPtr->Release();
- mPtr = rhs.mPtr;
- }
- catch(...) {
- rhs.mPtr->Release();
- throw;
- }
- }
- return *this;
- }
- ComPtr& operator=(ComPtr&& rhs)
- {
- if(mPtr)
- mPtr->Release();
- mPtr = rhs.mPtr;
- rhs.mPtr = nullptr;
- return *this;
- }
-
- operator bool() const noexcept { return mPtr != nullptr; }
-
- T& operator*() const noexcept { return *mPtr; }
- T* operator->() const noexcept { return mPtr; }
- T* get() const noexcept { return mPtr; }
- T** getPtr() noexcept { return &mPtr; }
-
- T* release() noexcept
- {
- T *ret{mPtr};
- mPtr = nullptr;
- return ret;
- }
-
- void swap(ComPtr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
- void swap(ComPtr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
-};
-
-
class GuidPrinter {
char mMsg[64];
diff --git a/common/comptr.h b/common/comptr.h
new file mode 100644
index 00000000..c238991a
--- /dev/null
+++ b/common/comptr.h
@@ -0,0 +1,70 @@
+#ifndef COMMON_COMPTR_H
+#define COMMON_COMPTR_H
+
+#include <cstddef>
+#include <utility>
+
+
+template<typename T>
+class ComPtr {
+ T *mPtr{nullptr};
+
+public:
+ ComPtr() noexcept = default;
+ ComPtr(const ComPtr &rhs) : mPtr{rhs.mPtr} { if(mPtr) mPtr->AddRef(); }
+ ComPtr(ComPtr&& rhs) noexcept : mPtr{rhs.mPtr} { rhs.mPtr = nullptr; }
+ ComPtr(std::nullptr_t) noexcept { }
+ explicit ComPtr(T *ptr) noexcept : mPtr{ptr} { }
+ ~ComPtr() { if(mPtr) mPtr->Release(); }
+
+ ComPtr& operator=(const ComPtr &rhs)
+ {
+ if(!rhs.mPtr)
+ {
+ if(mPtr)
+ mPtr->Release();
+ mPtr = nullptr;
+ }
+ else
+ {
+ rhs.mPtr->AddRef();
+ try {
+ if(mPtr)
+ mPtr->Release();
+ mPtr = rhs.mPtr;
+ }
+ catch(...) {
+ rhs.mPtr->Release();
+ throw;
+ }
+ }
+ return *this;
+ }
+ ComPtr& operator=(ComPtr&& rhs)
+ {
+ if(mPtr)
+ mPtr->Release();
+ mPtr = rhs.mPtr;
+ rhs.mPtr = nullptr;
+ return *this;
+ }
+
+ operator bool() const noexcept { return mPtr != nullptr; }
+
+ T& operator*() const noexcept { return *mPtr; }
+ T* operator->() const noexcept { return mPtr; }
+ T* get() const noexcept { return mPtr; }
+ T** getPtr() noexcept { return &mPtr; }
+
+ T* release() noexcept
+ {
+ T *ret{mPtr};
+ mPtr = nullptr;
+ return ret;
+ }
+
+ void swap(ComPtr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
+ void swap(ComPtr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
+};
+
+#endif