Fixes to copy_ptr
* Add move constructor * Add move assignment * Fix exception safety bug in copy assignment
This commit is contained in:
parent
15d11d29e7
commit
170c2a6984
|
@ -41,12 +41,22 @@ namespace libtorrent {
|
||||||
copy_ptr(): m_ptr(nullptr) {}
|
copy_ptr(): m_ptr(nullptr) {}
|
||||||
explicit copy_ptr(T* t): m_ptr(t) {}
|
explicit copy_ptr(T* t): m_ptr(t) {}
|
||||||
copy_ptr(copy_ptr const& p): m_ptr(p.m_ptr ? new T(*p.m_ptr) : nullptr) {}
|
copy_ptr(copy_ptr const& p): m_ptr(p.m_ptr ? new T(*p.m_ptr) : nullptr) {}
|
||||||
|
copy_ptr(copy_ptr&& p) noexcept : m_ptr(p.m_ptr) { p.m_ptr = nullptr; }
|
||||||
|
|
||||||
void reset(T* t = nullptr) { delete m_ptr; m_ptr = t; }
|
void reset(T* t = nullptr) { delete m_ptr; m_ptr = t; }
|
||||||
copy_ptr& operator=(copy_ptr const& p)
|
copy_ptr& operator=(copy_ptr const& p)
|
||||||
{
|
{
|
||||||
if (m_ptr == p.m_ptr) return *this;
|
if (m_ptr == p.m_ptr) return *this;
|
||||||
|
T* c = p.m_ptr ? new T(*p.m_ptr) : nullptr;
|
||||||
delete m_ptr;
|
delete m_ptr;
|
||||||
m_ptr = p.m_ptr ? new T(*p.m_ptr) : nullptr;
|
m_ptr = c;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
copy_ptr& operator=(copy_ptr&& p) noexcept {
|
||||||
|
if(m_ptr == p.m_ptr) return *this;
|
||||||
|
delete m_ptr;
|
||||||
|
m_ptr = p.m_ptr;
|
||||||
|
p.m_ptr = nullptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
T* operator->() { return m_ptr; }
|
T* operator->() { return m_ptr; }
|
||||||
|
|
Loading…
Reference in New Issue