AutoPtr and SharedPtr comparison with nullptr

This commit is contained in:
Günter Obiltschnig 2020-02-07 18:13:22 +01:00
parent 500b8f93ab
commit e51449e159
4 changed files with 57 additions and 27 deletions

View File

@ -21,6 +21,7 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include <algorithm>
#include <cstddef>
namespace Poco {
@ -306,6 +307,11 @@ public:
return _ptr == ptr;
}
bool operator == (std::nullptr_t ptr) const
{
return _ptr == ptr;
}
bool operator != (const AutoPtr& ptr) const
{
return _ptr != ptr._ptr;
@ -321,6 +327,11 @@ public:
return _ptr != ptr;
}
bool operator != (std::nullptr_t ptr) const
{
return _ptr != ptr;
}
bool operator < (const AutoPtr& ptr) const
{
return _ptr < ptr._ptr;

View File

@ -22,6 +22,7 @@
#include "Poco/Exception.h"
#include "Poco/AtomicCounter.h"
#include <algorithm>
#include <cstddef>
namespace Poco {
@ -336,6 +337,11 @@ public:
return get() == ptr;
}
bool operator == (std::nullptr_t ptr) const
{
return get() == ptr;
}
bool operator != (const SharedPtr& ptr) const
{
return get() != ptr.get();
@ -351,6 +357,11 @@ public:
return get() != ptr;
}
bool operator != (std::nullptr_t ptr) const
{
return get() != ptr;
}
bool operator < (const SharedPtr& ptr) const
{
return get() < ptr.get();

View File

@ -179,6 +179,9 @@ void AutoPtrTest::testOps()
assertTrue (!(!ptr1));
ptr1 = 0;
assertTrue (!ptr1);
assertTrue (ptr1 == nullptr);
assertTrue (ptr2 != nullptr);
}

View File

@ -83,7 +83,10 @@ void SharedPtrTest::testSharedPtr()
{
SharedPtr<TestObject> ptr1;
assertNull(ptr1.get());
assertTrue (ptr1 == nullptr);
TestObject* pTO1 = new TestObject("one");
TestObject* pTO2 = new TestObject("two");
if (pTO2 < pTO1)
{
@ -93,6 +96,8 @@ void SharedPtrTest::testSharedPtr()
}
assertTrue (pTO1 < pTO2);
ptr1 = pTO1;
assertTrue (ptr1 != nullptr);
assertTrue (ptr1.referenceCount() == 1);
SharedPtr<TestObject> ptr2 = pTO2;
SharedPtr<TestObject> ptr3 = ptr1;