mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-13 20:32:21 +00:00
[C++11] Add support for OwningPtr<T> to be converted to and from
std::unique_ptr<T>. Patch by Ahmed Charles! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202609 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
fd18fcb111
commit
80949c599f
@ -17,6 +17,7 @@
|
||||
#include "llvm/Support/Compiler.h"
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
namespace llvm {
|
||||
|
||||
@ -39,6 +40,17 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
OwningPtr(std::unique_ptr<T> &&Other) : Ptr(Other.release()) {}
|
||||
|
||||
OwningPtr &operator=(std::unique_ptr<T> &&Other) {
|
||||
reset(Other.release());
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if LLVM_HAS_RVALUE_REFERENCE_THIS
|
||||
operator std::unique_ptr<T>() && { return std::unique_ptr<T>(take()); }
|
||||
#endif
|
||||
|
||||
~OwningPtr() {
|
||||
delete Ptr;
|
||||
}
|
||||
@ -61,6 +73,8 @@ public:
|
||||
return Tmp;
|
||||
}
|
||||
|
||||
std::unique_ptr<T> take_unique() { return std::unique_ptr<T>(take()); }
|
||||
|
||||
T &operator*() const {
|
||||
assert(Ptr && "Cannot dereference null pointer");
|
||||
return *Ptr;
|
||||
|
@ -174,4 +174,82 @@ TEST_F(OwningPtrTest, Swap) {
|
||||
EXPECT_EQ(2u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, UniqueToOwningConstruction) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
std::unique_ptr<TrackDestructor> A(new TrackDestructor(3));
|
||||
OwningPtr<TrackDestructor> B = std::move(A);
|
||||
EXPECT_FALSE(A);
|
||||
EXPECT_TRUE(!A);
|
||||
EXPECT_FALSE(A.get());
|
||||
EXPECT_TRUE((bool)B);
|
||||
EXPECT_FALSE(!B);
|
||||
EXPECT_TRUE(B.get());
|
||||
EXPECT_TRUE(B.isValid());
|
||||
EXPECT_EQ(3, (*B).val);
|
||||
EXPECT_EQ(3, B->val);
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, UniqueToOwningAssignment) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
std::unique_ptr<TrackDestructor> A(new TrackDestructor(3));
|
||||
OwningPtr<TrackDestructor> B(new TrackDestructor(4));
|
||||
B = std::move(A);
|
||||
EXPECT_FALSE(A);
|
||||
EXPECT_TRUE(!A);
|
||||
EXPECT_FALSE(A.get());
|
||||
EXPECT_TRUE((bool)B);
|
||||
EXPECT_FALSE(!B);
|
||||
EXPECT_TRUE(B.get());
|
||||
EXPECT_TRUE(B.isValid());
|
||||
EXPECT_EQ(3, (*B).val);
|
||||
EXPECT_EQ(3, B->val);
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
EXPECT_EQ(2u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, TakeUniqueConstruction) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> A(new TrackDestructor(3));
|
||||
std::unique_ptr<TrackDestructor> B = A.take_unique();
|
||||
EXPECT_FALSE(A);
|
||||
EXPECT_TRUE(!A);
|
||||
EXPECT_FALSE(A.get());
|
||||
EXPECT_FALSE(A.isValid());
|
||||
EXPECT_TRUE((bool)B);
|
||||
EXPECT_FALSE(!B);
|
||||
EXPECT_TRUE(B.get());
|
||||
EXPECT_EQ(3, (*B).val);
|
||||
EXPECT_EQ(3, B->val);
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
#if LLVM_HAS_RVALUE_REFERENCE_THIS
|
||||
TEST_F(OwningPtrTest, OwningToUniqueConstruction) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> A(new TrackDestructor(3));
|
||||
std::unique_ptr<TrackDestructor> B = std::move(A);
|
||||
EXPECT_FALSE(A);
|
||||
EXPECT_TRUE(!A);
|
||||
EXPECT_FALSE(A.get());
|
||||
EXPECT_FALSE(A.isValid());
|
||||
EXPECT_TRUE((bool)B);
|
||||
EXPECT_FALSE(!B);
|
||||
EXPECT_TRUE(B.get());
|
||||
EXPECT_EQ(3, (*B).val);
|
||||
EXPECT_EQ(3, B->val);
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user