From 170c2a69843e5214dd0df6f0c91cf3d10ed1e57d Mon Sep 17 00:00:00 2001 From: Matthew Fioravante Date: Wed, 4 Oct 2017 22:42:40 -0400 Subject: [PATCH] Fixes to copy_ptr * Add move constructor * Add move assignment * Fix exception safety bug in copy assignment --- include/libtorrent/copy_ptr.hpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/libtorrent/copy_ptr.hpp b/include/libtorrent/copy_ptr.hpp index d605d45c8..953dfc0e1 100644 --- a/include/libtorrent/copy_ptr.hpp +++ b/include/libtorrent/copy_ptr.hpp @@ -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; }