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(likely(&rhs
!= this))
57 explicit operator bool() const noexcept
{ return mPtr
!= nullptr; }
59 T
& operator*() const noexcept
{ return *mPtr
; }
60 T
* operator->() const noexcept
{ return mPtr
; }
61 T
* get() const noexcept
{ return mPtr
; }
62 T
** getPtr() noexcept
{ return &mPtr
; }
64 T
* release() noexcept
{ return std::exchange(mPtr
, nullptr); }
66 void swap(ComPtr
&rhs
) noexcept
{ std::swap(mPtr
, rhs
.mPtr
); }
67 void swap(ComPtr
&& rhs
) noexcept
{ std::swap(mPtr
, rhs
.mPtr
); }