mirror of
https://github.com/schrodinger/pymol-open-source.git
synced 2026-06-03 19:54:24 +08:00
pymol::copyable_ptr and pymol::cache_ptr
This commit is contained in:
@@ -48,4 +48,60 @@ template <typename T> void destroy(T* iter, T* end)
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* A copyable unique pointer. Will make a copy of the managed object with "new".
|
||||
*/
|
||||
template <typename T, class D = std::default_delete<T>>
|
||||
class copyable_ptr : public std::unique_ptr<T, D>
|
||||
{
|
||||
T* copy_ptr() const { return *this ? new T(**this) : nullptr; }
|
||||
|
||||
public:
|
||||
using std::unique_ptr<T, D>::unique_ptr;
|
||||
|
||||
copyable_ptr() = default;
|
||||
// move
|
||||
copyable_ptr(copyable_ptr&& other) : copyable_ptr(other.release()) {}
|
||||
copyable_ptr& operator=(copyable_ptr&& other)
|
||||
{
|
||||
this->reset(other.release());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// copy
|
||||
copyable_ptr(const copyable_ptr &other) : copyable_ptr(other.copy_ptr()) {}
|
||||
copyable_ptr& operator=(const copyable_ptr& other)
|
||||
{
|
||||
this->reset(other.copy_ptr());
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* A unique pointer which copies to nullptr.
|
||||
*/
|
||||
template <typename T, class D = std::default_delete<T>>
|
||||
class cache_ptr : public std::unique_ptr<T, D>
|
||||
{
|
||||
public:
|
||||
using std::unique_ptr<T, D>::unique_ptr;
|
||||
|
||||
cache_ptr() = default;
|
||||
// move
|
||||
cache_ptr(cache_ptr&& other) : cache_ptr(other.release()) {}
|
||||
cache_ptr& operator=(cache_ptr&& other)
|
||||
{
|
||||
this->reset(other.release());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// copy
|
||||
cache_ptr(const cache_ptr& other) {}
|
||||
cache_ptr& operator=(const cache_ptr& other)
|
||||
{
|
||||
this->reset();
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace pymol
|
||||
|
||||
89
layerCTest/Test_cache_ptr.cpp
Normal file
89
layerCTest/Test_cache_ptr.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "Test.h"
|
||||
|
||||
#include "pymol/memory.h"
|
||||
|
||||
using namespace pymol::test;
|
||||
|
||||
TEST_CASE("cache_ptr move construct", "[cache_ptr]")
|
||||
{
|
||||
pymol::cache_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2(std::move(p1));
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2(std::move(p1));
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == ptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("cache_ptr copy construct", "[cache_ptr]")
|
||||
{
|
||||
pymol::cache_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2(p1);
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2(p1);
|
||||
REQUIRE(p1.get() == ptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("cache_ptr move assign", "[cache_ptr]")
|
||||
{
|
||||
pymol::cache_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2;
|
||||
p2 = std::move(p1);
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2;
|
||||
p2 = std::move(p1);
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == ptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("cache_ptr copy assign", "[cache_ptr]")
|
||||
{
|
||||
pymol::cache_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2;
|
||||
p2 = p1;
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::cache_ptr<int> p2;
|
||||
p2 = p1;
|
||||
REQUIRE(p1.get() == ptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
}
|
||||
93
layerCTest/Test_copyable_ptr.cpp
Normal file
93
layerCTest/Test_copyable_ptr.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "Test.h"
|
||||
|
||||
#include "pymol/memory.h"
|
||||
|
||||
using namespace pymol::test;
|
||||
|
||||
TEST_CASE("copyable_ptr move construct", "[copyable_ptr]")
|
||||
{
|
||||
pymol::copyable_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2(std::move(p1));
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2(std::move(p1));
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == ptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("copyable_ptr copy construct", "[copyable_ptr]")
|
||||
{
|
||||
pymol::copyable_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2(p1);
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2(p1);
|
||||
REQUIRE(p1.get() == ptr);
|
||||
REQUIRE(p2.get() != ptr);
|
||||
REQUIRE(p2.get() != nullptr);
|
||||
REQUIRE(*p2 == 123);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("copyable_ptr move assign", "[copyable_ptr]")
|
||||
{
|
||||
pymol::copyable_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2;
|
||||
p2 = std::move(p1);
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2;
|
||||
p2 = std::move(p1);
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == ptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("copyable_ptr copy assign", "[copyable_ptr]")
|
||||
{
|
||||
pymol::copyable_ptr<int> p1;
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2;
|
||||
p2 = p1;
|
||||
REQUIRE(p1.get() == nullptr);
|
||||
REQUIRE(p2.get() == nullptr);
|
||||
}
|
||||
|
||||
auto ptr = new int(123);
|
||||
p1.reset(ptr);
|
||||
|
||||
{
|
||||
pymol::copyable_ptr<int> p2;
|
||||
p2 = p1;
|
||||
REQUIRE(p1.get() == ptr);
|
||||
REQUIRE(p2.get() != ptr);
|
||||
REQUIRE(p2.get() != nullptr);
|
||||
REQUIRE(*p2 == 123);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user