Fixes to copy_ptr

* Add move constructor
* Add move assignment
* Fix exception safety bug in copy assignment
This commit is contained in:
Matthew Fioravante 2017-10-04 22:42:40 -04:00 committed by Arvid Norberg
parent 15d11d29e7
commit 170c2a6984
1 changed files with 11 additions and 1 deletions

View File

@ -41,12 +41,22 @@ namespace libtorrent {
copy_ptr(): m_ptr(nullptr) {}
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&& p) noexcept : m_ptr(p.m_ptr) { p.m_ptr = nullptr; }
void reset(T* t = nullptr) { delete m_ptr; m_ptr = t; }
copy_ptr& operator=(copy_ptr const& p)
{
if (m_ptr == p.m_ptr) return *this;
T* c = p.m_ptr ? new T(*p.m_ptr) : nullptr;
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;
}
T* operator->() { return m_ptr; }