simplify copy_ptr
This commit is contained in:
parent
7f3c38025f
commit
d0f55224fb
|
@ -33,46 +33,34 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#ifndef TORRENT_COPY_PTR
|
#ifndef TORRENT_COPY_PTR
|
||||||
#define TORRENT_COPY_PTR
|
#define TORRENT_COPY_PTR
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace libtorrent {
|
namespace libtorrent {
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct copy_ptr
|
struct copy_ptr
|
||||||
{
|
{
|
||||||
copy_ptr(): m_ptr(nullptr) {}
|
copy_ptr() = default;
|
||||||
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; }
|
copy_ptr(copy_ptr&& p) noexcept = default;
|
||||||
|
|
||||||
void reset(T* t = nullptr) { delete m_ptr; m_ptr = t; }
|
void reset(T* t = nullptr) { m_ptr.reset(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;
|
m_ptr.reset(p.m_ptr ? new T(*p.m_ptr) : nullptr);
|
||||||
delete m_ptr;
|
|
||||||
m_ptr = c;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
copy_ptr& operator=(copy_ptr&& p) noexcept {
|
copy_ptr& operator=(copy_ptr&& p) noexcept = default;
|
||||||
if(m_ptr == p.m_ptr) return *this;
|
T* operator->() { return m_ptr.get(); }
|
||||||
delete m_ptr;
|
T const* operator->() const { return m_ptr.get(); }
|
||||||
m_ptr = p.m_ptr;
|
|
||||||
p.m_ptr = nullptr;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
T* operator->() { return m_ptr; }
|
|
||||||
T const* operator->() const { return m_ptr; }
|
|
||||||
T& operator*() { return *m_ptr; }
|
T& operator*() { return *m_ptr; }
|
||||||
T const& operator*() const { return *m_ptr; }
|
T const& operator*() const { return *m_ptr; }
|
||||||
void swap(copy_ptr<T>& p)
|
void swap(copy_ptr<T>& p) { std::swap(*this, p); }
|
||||||
{
|
explicit operator bool() const { return m_ptr.get() != nullptr; }
|
||||||
T* tmp = m_ptr;
|
|
||||||
m_ptr = p.m_ptr;
|
|
||||||
p.m_ptr = tmp;
|
|
||||||
}
|
|
||||||
explicit operator bool() const { return m_ptr != nullptr; }
|
|
||||||
~copy_ptr() { delete m_ptr; }
|
|
||||||
private:
|
private:
|
||||||
T* m_ptr = nullptr;
|
std::unique_ptr<T> m_ptr;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1008,3 +1008,19 @@ TORRENT_TEST(copy)
|
||||||
TEST_EQUAL(fs2.hash(i), file_hashes[i]);
|
TEST_EQUAL(fs2.hash(i), file_hashes[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct A
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
};
|
||||||
|
|
||||||
|
TORRENT_TEST(copy_ptr)
|
||||||
|
{
|
||||||
|
copy_ptr<A> a(new A{4});
|
||||||
|
copy_ptr<A> b(a);
|
||||||
|
|
||||||
|
TEST_EQUAL(a->val, b->val);
|
||||||
|
TEST_CHECK(&*a != &*b);
|
||||||
|
a->val = 5;
|
||||||
|
TEST_EQUAL(b->val, 4);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue