forked from OSchip/llvm-project
Remove OwningPtr.h and associated tests
llvm::OwningPtr is superseded by std::unique_ptr. llvm-svn: 211259
This commit is contained in:
parent
6cf6c05322
commit
0b346e6be7
|
@ -1,165 +0,0 @@
|
|||
//===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file defines and implements the OwningPtr class.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_ADT_OWNINGPTR_H
|
||||
#define LLVM_ADT_OWNINGPTR_H
|
||||
|
||||
#include "llvm/Support/Compiler.h"
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
namespace llvm {
|
||||
|
||||
/// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
|
||||
/// guarantees deletion of the object pointed to, either on destruction of the
|
||||
/// OwningPtr or via an explicit reset(). Once created, ownership of the
|
||||
/// pointee object can be taken away from OwningPtr by using the take method.
|
||||
template<class T>
|
||||
class OwningPtr {
|
||||
OwningPtr(OwningPtr const &) LLVM_DELETED_FUNCTION;
|
||||
OwningPtr &operator=(OwningPtr const &) LLVM_DELETED_FUNCTION;
|
||||
T *Ptr;
|
||||
public:
|
||||
explicit OwningPtr(T *P = 0) : Ptr(P) {}
|
||||
|
||||
OwningPtr(OwningPtr &&Other) : Ptr(Other.take()) {}
|
||||
|
||||
OwningPtr &operator=(OwningPtr &&Other) {
|
||||
reset(Other.take());
|
||||
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;
|
||||
}
|
||||
|
||||
/// reset - Change the current pointee to the specified pointer. Note that
|
||||
/// calling this with any pointer (including a null pointer) deletes the
|
||||
/// current pointer.
|
||||
void reset(T *P = 0) {
|
||||
if (P == Ptr) return;
|
||||
T *Tmp = Ptr;
|
||||
Ptr = P;
|
||||
delete Tmp;
|
||||
}
|
||||
|
||||
/// take - Reset the owning pointer to null and return its pointer. This does
|
||||
/// not delete the pointer before returning it.
|
||||
T *take() {
|
||||
T *Tmp = Ptr;
|
||||
Ptr = nullptr;
|
||||
return Tmp;
|
||||
}
|
||||
|
||||
T *release() { return take(); }
|
||||
|
||||
std::unique_ptr<T> take_unique() { return std::unique_ptr<T>(take()); }
|
||||
|
||||
T &operator*() const {
|
||||
assert(Ptr && "Cannot dereference null pointer");
|
||||
return *Ptr;
|
||||
}
|
||||
|
||||
T *operator->() const { return Ptr; }
|
||||
T *get() const { return Ptr; }
|
||||
LLVM_EXPLICIT operator bool() const { return Ptr != nullptr; }
|
||||
bool operator!() const { return Ptr == nullptr; }
|
||||
bool isValid() const { return Ptr != nullptr; }
|
||||
|
||||
void swap(OwningPtr &RHS) {
|
||||
T *Tmp = RHS.Ptr;
|
||||
RHS.Ptr = Ptr;
|
||||
Ptr = Tmp;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
/// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
|
||||
/// functionality as OwningPtr, except that it works for array types.
|
||||
template<class T>
|
||||
class OwningArrayPtr {
|
||||
OwningArrayPtr(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
|
||||
OwningArrayPtr &operator=(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
|
||||
T *Ptr;
|
||||
public:
|
||||
explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
|
||||
|
||||
OwningArrayPtr(OwningArrayPtr &&Other) : Ptr(Other.take()) {}
|
||||
|
||||
OwningArrayPtr &operator=(OwningArrayPtr &&Other) {
|
||||
reset(Other.take());
|
||||
return *this;
|
||||
}
|
||||
|
||||
~OwningArrayPtr() {
|
||||
delete [] Ptr;
|
||||
}
|
||||
|
||||
/// reset - Change the current pointee to the specified pointer. Note that
|
||||
/// calling this with any pointer (including a null pointer) deletes the
|
||||
/// current pointer.
|
||||
void reset(T *P = 0) {
|
||||
if (P == Ptr) return;
|
||||
T *Tmp = Ptr;
|
||||
Ptr = P;
|
||||
delete [] Tmp;
|
||||
}
|
||||
|
||||
/// take - Reset the owning pointer to null and return its pointer. This does
|
||||
/// not delete the pointer before returning it.
|
||||
T *take() {
|
||||
T *Tmp = Ptr;
|
||||
Ptr = 0;
|
||||
return Tmp;
|
||||
}
|
||||
|
||||
T &operator[](std::ptrdiff_t i) const {
|
||||
assert(Ptr && "Cannot dereference null pointer");
|
||||
return Ptr[i];
|
||||
}
|
||||
|
||||
T *get() const { return Ptr; }
|
||||
LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
|
||||
bool operator!() const { return Ptr == nullptr; }
|
||||
|
||||
void swap(OwningArrayPtr &RHS) {
|
||||
T *Tmp = RHS.Ptr;
|
||||
RHS.Ptr = Ptr;
|
||||
Ptr = Tmp;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
} // end namespace llvm
|
||||
|
||||
#endif
|
|
@ -23,7 +23,6 @@ set(ADTSources
|
|||
MakeUniqueTest.cpp
|
||||
MapVectorTest.cpp
|
||||
OptionalTest.cpp
|
||||
OwningPtrTest.cpp
|
||||
PackedVectorTest.cpp
|
||||
PointerIntPairTest.cpp
|
||||
PointerUnionTest.cpp
|
||||
|
|
|
@ -1,273 +0,0 @@
|
|||
//===- llvm/unittest/ADT/OwningPtrTest.cpp - OwningPtr unit tests ---------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "gtest/gtest.h"
|
||||
using namespace llvm;
|
||||
|
||||
namespace {
|
||||
|
||||
struct TrackDestructor {
|
||||
static unsigned Destructions;
|
||||
int val;
|
||||
explicit TrackDestructor(int val) : val(val) {}
|
||||
~TrackDestructor() { ++Destructions; }
|
||||
static void ResetCounts() { Destructions = 0; }
|
||||
|
||||
private:
|
||||
TrackDestructor(const TrackDestructor &other) LLVM_DELETED_FUNCTION;
|
||||
TrackDestructor &
|
||||
operator=(const TrackDestructor &other) LLVM_DELETED_FUNCTION;
|
||||
TrackDestructor(TrackDestructor &&other) LLVM_DELETED_FUNCTION;
|
||||
TrackDestructor &operator=(TrackDestructor &&other) LLVM_DELETED_FUNCTION;
|
||||
};
|
||||
|
||||
unsigned TrackDestructor::Destructions = 0;
|
||||
|
||||
// Test fixture
|
||||
class OwningPtrTest : public testing::Test {};
|
||||
|
||||
TEST_F(OwningPtrTest, DefaultConstruction) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> O;
|
||||
EXPECT_FALSE(O);
|
||||
EXPECT_TRUE(!O);
|
||||
EXPECT_FALSE(O.get());
|
||||
EXPECT_FALSE(O.isValid());
|
||||
}
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, PtrConstruction) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> O(new TrackDestructor(3));
|
||||
EXPECT_TRUE((bool)O);
|
||||
EXPECT_FALSE(!O);
|
||||
EXPECT_TRUE(O.get());
|
||||
EXPECT_TRUE(O.isValid());
|
||||
EXPECT_EQ(3, (*O).val);
|
||||
EXPECT_EQ(3, O->val);
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, Reset) {
|
||||
TrackDestructor::ResetCounts();
|
||||
OwningPtr<TrackDestructor> O(new TrackDestructor(3));
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
O.reset();
|
||||
EXPECT_FALSE((bool)O);
|
||||
EXPECT_TRUE(!O);
|
||||
EXPECT_FALSE(O.get());
|
||||
EXPECT_FALSE(O.isValid());
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, Take) {
|
||||
TrackDestructor::ResetCounts();
|
||||
TrackDestructor *T = nullptr;
|
||||
{
|
||||
OwningPtr<TrackDestructor> O(new TrackDestructor(3));
|
||||
T = O.take();
|
||||
EXPECT_FALSE((bool)O);
|
||||
EXPECT_TRUE(!O);
|
||||
EXPECT_FALSE(O.get());
|
||||
EXPECT_FALSE(O.isValid());
|
||||
EXPECT_TRUE(T);
|
||||
EXPECT_EQ(3, T->val);
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
delete T;
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, Release) {
|
||||
TrackDestructor::ResetCounts();
|
||||
TrackDestructor *T = nullptr;
|
||||
{
|
||||
OwningPtr<TrackDestructor> O(new TrackDestructor(3));
|
||||
T = O.release();
|
||||
EXPECT_FALSE((bool)O);
|
||||
EXPECT_TRUE(!O);
|
||||
EXPECT_FALSE(O.get());
|
||||
EXPECT_FALSE(O.isValid());
|
||||
EXPECT_TRUE(T);
|
||||
EXPECT_EQ(3, T->val);
|
||||
EXPECT_EQ(0u, TrackDestructor::Destructions);
|
||||
}
|
||||
delete T;
|
||||
EXPECT_EQ(1u, TrackDestructor::Destructions);
|
||||
}
|
||||
|
||||
TEST_F(OwningPtrTest, MoveConstruction) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> A(new TrackDestructor(3));
|
||||
OwningPtr<TrackDestructor> B = std::move(A);
|
||||
EXPECT_FALSE((bool)A);
|
||||
EXPECT_TRUE(!A);
|
||||
EXPECT_FALSE(A.get());
|
||||
EXPECT_FALSE(A.isValid());
|
||||
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, MoveAssignment) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<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_FALSE(A.isValid());
|
||||
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, Swap) {
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> A(new TrackDestructor(3));
|
||||
OwningPtr<TrackDestructor> B(new TrackDestructor(4));
|
||||
B.swap(A);
|
||||
EXPECT_TRUE((bool)A);
|
||||
EXPECT_FALSE(!A);
|
||||
EXPECT_TRUE(A.get());
|
||||
EXPECT_TRUE(A.isValid());
|
||||
EXPECT_EQ(4, (*A).val);
|
||||
EXPECT_EQ(4, A->val);
|
||||
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(2u, TrackDestructor::Destructions);
|
||||
TrackDestructor::ResetCounts();
|
||||
{
|
||||
OwningPtr<TrackDestructor> A(new TrackDestructor(3));
|
||||
OwningPtr<TrackDestructor> B(new TrackDestructor(4));
|
||||
swap(A, B);
|
||||
EXPECT_TRUE((bool)A);
|
||||
EXPECT_FALSE(!A);
|
||||
EXPECT_TRUE(A.get());
|
||||
EXPECT_TRUE(A.isValid());
|
||||
EXPECT_EQ(4, (*A).val);
|
||||
EXPECT_EQ(4, A->val);
|
||||
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(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
|
||||
}
|
|
@ -108,14 +108,6 @@ or create a symbolic link so it updates automatically.
|
|||
</Expand>
|
||||
</Type>
|
||||
|
||||
<Type Name="llvm::OwningPtr<*>">
|
||||
<DisplayString Condition="Ptr == 0">empty</DisplayString>
|
||||
<DisplayString Condition="Ptr != 0">OwningPtr {*Ptr}</DisplayString>
|
||||
<Expand>
|
||||
<ExpandedItem Condition="Ptr != 0">Ptr</ExpandedItem>
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<Type Name="llvm::SmallPtrSet<*,*>">
|
||||
<DisplayString Condition="CurArray == SmallArray">{{ [Small Mode] size={NumElements}, capacity={CurArraySize} }}</DisplayString>
|
||||
<DisplayString Condition="CurArray != SmallArray">{{ [Big Mode] size={NumElements}, capacity={CurArraySize} }}</DisplayString>
|
||||
|
|
Loading…
Reference in New Issue