1 #ifndef COMMON_COMPTR_H
2 #define COMMON_COMPTR_H
7 #include "opthelpers.h"
15 ComPtr() noexcept = default;
16 ComPtr(const ComPtr &rhs) : mPtr{rhs.mPtr} { if(mPtr) mPtr->AddRef(); }
17 ComPtr(ComPtr&& rhs) noexcept : mPtr{rhs.mPtr} { rhs.mPtr = nullptr; }
18 ComPtr(std::nullptr_t) noexcept { }
19 explicit ComPtr(T *ptr) noexcept : mPtr{ptr} { }
20 ~ComPtr() { if(mPtr) mPtr->Release(); }
22 ComPtr& operator=(const ComPtr &rhs)
45 ComPtr& operator=(ComPtr&& rhs)
47 if(&rhs != this) LIKELY
49 if(mPtr) mPtr->Release();
50 mPtr = std::exchange(rhs.mPtr, nullptr);
55 explicit operator bool() const noexcept { return mPtr != nullptr; }
57 T& operator*() const noexcept { return *mPtr; }
58 T* operator->() const noexcept { return mPtr; }
59 T* get() const noexcept { return mPtr; }
60 T** getPtr() noexcept { return &mPtr; }
62 T* release() noexcept { return std::exchange(mPtr, nullptr); }
64 void swap(ComPtr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
65 void swap(ComPtr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }