mirror of
https://github.com/pocoproject/poco.git
synced 2025-07-04 17:40:01 +02:00
fix move ctor/assignment
This commit is contained in:
parent
18adb1e43b
commit
eeb64cc340
@ -47,7 +47,7 @@ class AutoPtr
|
|||||||
/// AutoPtr works in the following way:
|
/// AutoPtr works in the following way:
|
||||||
/// If an AutoPtr is assigned an ordinary pointer to
|
/// If an AutoPtr is assigned an ordinary pointer to
|
||||||
/// an object (via the constructor or the assignment operator),
|
/// an object (via the constructor or the assignment operator),
|
||||||
/// it takes ownership of the object and the object's reference
|
/// it takes ownership of the object and the object's reference
|
||||||
/// count remains unchanged.
|
/// count remains unchanged.
|
||||||
/// If the AutoPtr is assigned another AutoPtr, the
|
/// If the AutoPtr is assigned another AutoPtr, the
|
||||||
/// object's reference count is incremented by one by
|
/// object's reference count is incremented by one by
|
||||||
@ -84,7 +84,7 @@ public:
|
|||||||
ptr._ptr = nullptr;
|
ptr._ptr = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
AutoPtr(const AutoPtr<Other>& ptr): _ptr(const_cast<Other*>(ptr.get()))
|
AutoPtr(const AutoPtr<Other>& ptr): _ptr(const_cast<Other*>(ptr.get()))
|
||||||
{
|
{
|
||||||
if (_ptr) _ptr->duplicate();
|
if (_ptr) _ptr->duplicate();
|
||||||
@ -94,7 +94,7 @@ public:
|
|||||||
{
|
{
|
||||||
if (_ptr) _ptr->release();
|
if (_ptr) _ptr->release();
|
||||||
}
|
}
|
||||||
|
|
||||||
AutoPtr& assign(C* ptr)
|
AutoPtr& assign(C* ptr)
|
||||||
{
|
{
|
||||||
if (_ptr != ptr)
|
if (_ptr != ptr)
|
||||||
@ -115,7 +115,7 @@ public:
|
|||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
AutoPtr& assign(const AutoPtr& ptr)
|
AutoPtr& assign(const AutoPtr& ptr)
|
||||||
{
|
{
|
||||||
if (&ptr != this)
|
if (&ptr != this)
|
||||||
@ -126,8 +126,8 @@ public:
|
|||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
AutoPtr& assign(const AutoPtr<Other>& ptr)
|
AutoPtr& assign(const AutoPtr<Other>& ptr)
|
||||||
{
|
{
|
||||||
if (ptr.get() != _ptr)
|
if (ptr.get() != _ptr)
|
||||||
@ -182,12 +182,12 @@ public:
|
|||||||
AutoPtr& operator = (AutoPtr&& ptr) noexcept
|
AutoPtr& operator = (AutoPtr&& ptr) noexcept
|
||||||
{
|
{
|
||||||
if (_ptr) _ptr->release();
|
if (_ptr) _ptr->release();
|
||||||
_ptr = std::move(ptr._ptr);
|
_ptr = ptr._ptr;
|
||||||
ptr._ptr = nullptr;
|
ptr._ptr = nullptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
AutoPtr& operator = (const AutoPtr<Other>& ptr)
|
AutoPtr& operator = (const AutoPtr<Other>& ptr)
|
||||||
{
|
{
|
||||||
return assign<Other>(ptr);
|
return assign<Other>(ptr);
|
||||||
@ -197,8 +197,8 @@ public:
|
|||||||
{
|
{
|
||||||
std::swap(_ptr, ptr._ptr);
|
std::swap(_ptr, ptr._ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
AutoPtr<Other> cast() const
|
AutoPtr<Other> cast() const
|
||||||
/// Casts the AutoPtr via a dynamic cast to the given type.
|
/// Casts the AutoPtr via a dynamic cast to the given type.
|
||||||
/// Returns an AutoPtr containing NULL if the cast fails.
|
/// Returns an AutoPtr containing NULL if the cast fails.
|
||||||
@ -211,7 +211,7 @@ public:
|
|||||||
return AutoPtr<Other>(pOther, true);
|
return AutoPtr<Other>(pOther, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
AutoPtr<Other> unsafeCast() const
|
AutoPtr<Other> unsafeCast() const
|
||||||
/// Casts the AutoPtr via a static cast to the given type.
|
/// Casts the AutoPtr via a static cast to the given type.
|
||||||
/// Example: (assume class Sub: public Super)
|
/// Example: (assume class Sub: public Super)
|
||||||
@ -269,12 +269,12 @@ public:
|
|||||||
{
|
{
|
||||||
return _ptr;
|
return _ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator const C* () const
|
operator const C* () const
|
||||||
{
|
{
|
||||||
return _ptr;
|
return _ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator ! () const
|
bool operator ! () const
|
||||||
{
|
{
|
||||||
return _ptr == nullptr;
|
return _ptr == nullptr;
|
||||||
@ -284,7 +284,7 @@ public:
|
|||||||
{
|
{
|
||||||
return _ptr == nullptr;
|
return _ptr == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
C* duplicate()
|
C* duplicate()
|
||||||
{
|
{
|
||||||
if (_ptr) _ptr->duplicate();
|
if (_ptr) _ptr->duplicate();
|
||||||
|
@ -123,7 +123,7 @@ public:
|
|||||||
Nullable& assign(C&& value)
|
Nullable& assign(C&& value)
|
||||||
/// Assigns a value to the Nullable.
|
/// Assigns a value to the Nullable.
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = std::move(value);
|
||||||
_isNull = false;
|
_isNull = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -152,7 +152,7 @@ public:
|
|||||||
Nullable& operator = (C&& value)
|
Nullable& operator = (C&& value)
|
||||||
/// Move-assigns a value to the Nullable.
|
/// Move-assigns a value to the Nullable.
|
||||||
{
|
{
|
||||||
return assign(value);
|
return assign(std::move(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
Nullable& operator = (const Nullable& other)
|
Nullable& operator = (const Nullable& other)
|
||||||
|
@ -105,7 +105,7 @@ public:
|
|||||||
Optional& assign(C&& value)
|
Optional& assign(C&& value)
|
||||||
/// Moves a value into the Optional.
|
/// Moves a value into the Optional.
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = std::move(value);
|
||||||
_isSpecified = true;
|
_isSpecified = true;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ public:
|
|||||||
|
|
||||||
Optional& operator = (C&& value)
|
Optional& operator = (C&& value)
|
||||||
{
|
{
|
||||||
return assign(value);
|
return assign(std::move(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional& operator = (const Optional& other)
|
Optional& operator = (const Optional& other)
|
||||||
|
@ -44,7 +44,7 @@ public:
|
|||||||
{
|
{
|
||||||
return --_cnt;
|
return --_cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
int referenceCount() const
|
int referenceCount() const
|
||||||
{
|
{
|
||||||
return _cnt.value();
|
return _cnt.value();
|
||||||
@ -61,7 +61,7 @@ class ReleasePolicy
|
|||||||
/// simply uses the delete operator to delete an object.
|
/// simply uses the delete operator to delete an object.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void release(C* pObj)
|
static void release(C* pObj) noexcept
|
||||||
/// Delete the object.
|
/// Delete the object.
|
||||||
/// Note that pObj can be nullptr.
|
/// Note that pObj can be nullptr.
|
||||||
{
|
{
|
||||||
@ -75,7 +75,7 @@ class ReleaseArrayPolicy
|
|||||||
/// The release policy for SharedPtr holding arrays.
|
/// The release policy for SharedPtr holding arrays.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void release(C* pObj)
|
static void release(C* pObj) noexcept
|
||||||
/// Delete the object.
|
/// Delete the object.
|
||||||
/// Note that pObj can be nullptr.
|
/// Note that pObj can be nullptr.
|
||||||
{
|
{
|
||||||
@ -94,11 +94,11 @@ class SharedPtr
|
|||||||
/// can be used with any class. For this to work, a
|
/// can be used with any class. For this to work, a
|
||||||
/// SharedPtr manages a reference count for the object
|
/// SharedPtr manages a reference count for the object
|
||||||
/// it manages.
|
/// it manages.
|
||||||
///
|
///
|
||||||
/// SharedPtr works in the following way:
|
/// SharedPtr works in the following way:
|
||||||
/// If an SharedPtr is assigned an ordinary pointer to
|
/// If an SharedPtr is assigned an ordinary pointer to
|
||||||
/// an object (via the constructor or the assignment operator),
|
/// an object (via the constructor or the assignment operator),
|
||||||
/// it takes ownership of the object and the object's reference
|
/// it takes ownership of the object and the object's reference
|
||||||
/// count is initialized to one.
|
/// count is initialized to one.
|
||||||
/// If the SharedPtr is assigned another SharedPtr, the
|
/// If the SharedPtr is assigned another SharedPtr, the
|
||||||
/// object's reference count is incremented by one.
|
/// object's reference count is incremented by one.
|
||||||
@ -115,33 +115,41 @@ class SharedPtr
|
|||||||
public:
|
public:
|
||||||
typedef C Type;
|
typedef C Type;
|
||||||
|
|
||||||
SharedPtr(): _pCounter(nullptr), _ptr(nullptr)
|
SharedPtr():
|
||||||
|
_pCounter(nullptr),
|
||||||
|
_ptr(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPtr(C* ptr)
|
SharedPtr(C* ptr)
|
||||||
try:
|
try:
|
||||||
_pCounter(ptr ? new RC : nullptr),
|
_pCounter(ptr ? new RC : nullptr),
|
||||||
_ptr(ptr)
|
_ptr(ptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
RP::release(ptr);
|
RP::release(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other, class OtherRP>
|
template <class Other, class OtherRP>
|
||||||
SharedPtr(const SharedPtr<Other, RC, OtherRP>& ptr): _pCounter(ptr._pCounter), _ptr(const_cast<Other*>(ptr.get()))
|
SharedPtr(const SharedPtr<Other, RC, OtherRP>& ptr):
|
||||||
|
_pCounter(ptr._pCounter),
|
||||||
|
_ptr(const_cast<Other*>(ptr.get()))
|
||||||
{
|
{
|
||||||
if (_pCounter) _pCounter->duplicate();
|
if (_pCounter) _pCounter->duplicate();
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPtr(const SharedPtr& ptr): _pCounter(ptr._pCounter), _ptr(ptr._ptr)
|
SharedPtr(const SharedPtr& ptr):
|
||||||
|
_pCounter(ptr._pCounter),
|
||||||
|
_ptr(ptr._ptr)
|
||||||
{
|
{
|
||||||
if (_pCounter) _pCounter->duplicate();
|
if (_pCounter) _pCounter->duplicate();
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPtr(SharedPtr&& ptr) noexcept: _pCounter(std::move(ptr._pCounter)), _ptr(std::move(ptr._ptr))
|
SharedPtr(SharedPtr&& ptr) noexcept:
|
||||||
|
_pCounter(ptr._pCounter),
|
||||||
|
_ptr(ptr._ptr)
|
||||||
{
|
{
|
||||||
ptr._pCounter = nullptr;
|
ptr._pCounter = nullptr;
|
||||||
ptr._ptr = nullptr;
|
ptr._ptr = nullptr;
|
||||||
@ -149,14 +157,7 @@ public:
|
|||||||
|
|
||||||
~SharedPtr()
|
~SharedPtr()
|
||||||
{
|
{
|
||||||
try
|
release();
|
||||||
{
|
|
||||||
release();
|
|
||||||
}
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
poco_unexpected();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPtr& assign(C* ptr)
|
SharedPtr& assign(C* ptr)
|
||||||
@ -168,7 +169,7 @@ public:
|
|||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPtr& assign(const SharedPtr& ptr)
|
SharedPtr& assign(const SharedPtr& ptr)
|
||||||
{
|
{
|
||||||
if (&ptr != this)
|
if (&ptr != this)
|
||||||
@ -178,7 +179,7 @@ public:
|
|||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other, class OtherRP>
|
template <class Other, class OtherRP>
|
||||||
SharedPtr& assign(const SharedPtr<Other, RC, OtherRP>& ptr)
|
SharedPtr& assign(const SharedPtr<Other, RC, OtherRP>& ptr)
|
||||||
{
|
{
|
||||||
@ -223,9 +224,10 @@ public:
|
|||||||
|
|
||||||
SharedPtr& operator = (SharedPtr&& ptr) noexcept
|
SharedPtr& operator = (SharedPtr&& ptr) noexcept
|
||||||
{
|
{
|
||||||
_ptr = std::move(ptr._ptr);
|
release();
|
||||||
_pCounter = std::move(ptr._pCounter);
|
_ptr = ptr._ptr;
|
||||||
ptr._ptr = nullptr;
|
ptr._ptr = nullptr;
|
||||||
|
_pCounter = ptr._pCounter;
|
||||||
ptr._pCounter = nullptr;
|
ptr._pCounter = nullptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -242,7 +244,7 @@ public:
|
|||||||
std::swap(_pCounter, ptr._pCounter);
|
std::swap(_pCounter, ptr._pCounter);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
SharedPtr<Other, RC, RP> cast() const
|
SharedPtr<Other, RC, RP> cast() const
|
||||||
/// Casts the SharedPtr via a dynamic cast to the given type.
|
/// Casts the SharedPtr via a dynamic cast to the given type.
|
||||||
/// Returns an SharedPtr containing NULL if the cast fails.
|
/// Returns an SharedPtr containing NULL if the cast fails.
|
||||||
@ -257,7 +259,7 @@ public:
|
|||||||
return SharedPtr<Other, RC, RP>();
|
return SharedPtr<Other, RC, RP>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Other>
|
template <class Other>
|
||||||
SharedPtr<Other, RC, RP> unsafeCast() const
|
SharedPtr<Other, RC, RP> unsafeCast() const
|
||||||
/// Casts the SharedPtr via a static cast to the given type.
|
/// Casts the SharedPtr via a static cast to the given type.
|
||||||
/// Example: (assume class Sub: public Super)
|
/// Example: (assume class Sub: public Super)
|
||||||
@ -303,7 +305,7 @@ public:
|
|||||||
{
|
{
|
||||||
return _ptr;
|
return _ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator const C* () const
|
operator const C* () const
|
||||||
{
|
{
|
||||||
return _ptr;
|
return _ptr;
|
||||||
@ -408,7 +410,7 @@ public:
|
|||||||
{
|
{
|
||||||
return get() >= ptr;
|
return get() >= ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int referenceCount() const
|
int referenceCount() const
|
||||||
{
|
{
|
||||||
return _pCounter ? _pCounter->referenceCount() : 0;
|
return _pCounter ? _pCounter->referenceCount() : 0;
|
||||||
@ -423,7 +425,7 @@ private:
|
|||||||
return _ptr;
|
return _ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void release()
|
void release() noexcept
|
||||||
{
|
{
|
||||||
if (_pCounter && _pCounter->release() == 0)
|
if (_pCounter && _pCounter->release() == 0)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user