Move test into test/std subdirectory.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@224658 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
181
test/std/utilities/memory/unique.ptr/deleter.h
Normal file
181
test/std/utilities/memory/unique.ptr/deleter.h
Normal file
@@ -0,0 +1,181 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Example move-only deleter
|
||||
|
||||
#ifndef DELETER_H
|
||||
#define DELETER_H
|
||||
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
template <class T>
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
Deleter(const Deleter&);
|
||||
Deleter& operator=(const Deleter&);
|
||||
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
|
||||
public:
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
Deleter(Deleter&& r) : state_(r.state_) {r.state_ = 0;}
|
||||
Deleter& operator=(Deleter&& r)
|
||||
{
|
||||
state_ = r.state_;
|
||||
r.state_ = 0;
|
||||
return *this;
|
||||
}
|
||||
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
operator std::__rv<Deleter>() {return std::__rv<Deleter>(*this);}
|
||||
Deleter(std::__rv<Deleter> r) : state_(r->state_) {r->state_ = 0;}
|
||||
Deleter& operator=(std::__rv<Deleter> r)
|
||||
{
|
||||
state_ = r->state_;
|
||||
r->state_ = 0;
|
||||
return *this;
|
||||
}
|
||||
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
|
||||
Deleter() : state_(0) {}
|
||||
explicit Deleter(int s) : state_(s) {}
|
||||
~Deleter() {assert(state_ >= 0); state_ = -1;}
|
||||
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
template <class U>
|
||||
Deleter(Deleter<U>&& d,
|
||||
typename std::enable_if<!std::is_same<U, T>::value>::type* = 0)
|
||||
: state_(d.state()) {d.set_state(0);}
|
||||
|
||||
private:
|
||||
template <class U>
|
||||
Deleter(const Deleter<U>& d,
|
||||
typename std::enable_if<!std::is_same<U, T>::value>::type* = 0);
|
||||
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
template <class U>
|
||||
Deleter(Deleter<U> d,
|
||||
typename std::enable_if<!std::is_same<U, T>::value>::type* = 0)
|
||||
: state_(d.state()) {}
|
||||
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
public:
|
||||
int state() const {return state_;}
|
||||
void set_state(int i) {state_ = i;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class Deleter<T[]>
|
||||
{
|
||||
int state_;
|
||||
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
Deleter(const Deleter&);
|
||||
Deleter& operator=(const Deleter&);
|
||||
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
|
||||
public:
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
Deleter(Deleter&& r) : state_(r.state_) {r.state_ = 0;}
|
||||
Deleter& operator=(Deleter&& r)
|
||||
{
|
||||
state_ = r.state_;
|
||||
r.state_ = 0;
|
||||
return *this;
|
||||
}
|
||||
#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
operator std::__rv<Deleter>() {return std::__rv<Deleter>(*this);}
|
||||
Deleter(std::__rv<Deleter> r) : state_(r->state_) {r->state_ = 0;}
|
||||
Deleter& operator=(std::__rv<Deleter> r)
|
||||
{
|
||||
state_ = r->state_;
|
||||
r->state_ = 0;
|
||||
return *this;
|
||||
}
|
||||
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
|
||||
Deleter() : state_(0) {}
|
||||
explicit Deleter(int s) : state_(s) {}
|
||||
~Deleter() {assert(state_ >= 0); state_ = -1;}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int i) {state_ = i;}
|
||||
|
||||
void operator()(T* p) {delete [] p;}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void
|
||||
swap(Deleter<T>& x, Deleter<T>& y)
|
||||
{
|
||||
Deleter<T> t(std::move(x));
|
||||
x = std::move(y);
|
||||
y = std::move(t);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
CDeleter() : state_(0) {}
|
||||
explicit CDeleter(int s) : state_(s) {}
|
||||
~CDeleter() {assert(state_ >= 0); state_ = -1;}
|
||||
|
||||
template <class U>
|
||||
CDeleter(const CDeleter<U>& d)
|
||||
: state_(d.state()) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int i) {state_ = i;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class CDeleter<T[]>
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
CDeleter() : state_(0) {}
|
||||
explicit CDeleter(int s) : state_(s) {}
|
||||
~CDeleter() {assert(state_ >= 0); state_ = -1;}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int i) {state_ = i;}
|
||||
|
||||
void operator()(T* p) {delete [] p;}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void
|
||||
swap(CDeleter<T>& x, CDeleter<T>& y)
|
||||
{
|
||||
CDeleter<T> t(std::move(x));
|
||||
x = std::move(y);
|
||||
y = std::move(t);
|
||||
}
|
||||
|
||||
#endif // DELETER_H
|
12
test/std/utilities/memory/unique.ptr/nothing_to_do.pass.cpp
Normal file
12
test/std/utilities/memory/unique.ptr/nothing_to_do.pass.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
// The only way to create an unique_ptr<T[]> is to default construct them.
|
||||
|
||||
class foo {
|
||||
public:
|
||||
foo () : val_(3) {}
|
||||
int get () const { return val_; }
|
||||
private:
|
||||
int val_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
#if _LIBCPP_STD_VER > 11
|
||||
{
|
||||
auto p1 = std::make_unique<int[]>(5);
|
||||
for ( int i = 0; i < 5; ++i )
|
||||
assert ( p1[i] == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
auto p2 = std::make_unique<std::string[]>(5);
|
||||
for ( int i = 0; i < 5; ++i )
|
||||
assert ( p2[i].size () == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
auto p3 = std::make_unique<foo[]>(7);
|
||||
for ( int i = 0; i < 7; ++i )
|
||||
assert ( p3[i].get () == 3 );
|
||||
}
|
||||
#endif // _LIBCPP_STD_VER > 11
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
auto up1 = std::make_unique<std::string[]>("error"); // doesn't compile - no bound
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
auto up2 = std::make_unique<int[]>(10, 20, 30, 40);
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
auto up3 = std::make_unique<int[5]>(); // this is deleted
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
auto up4 = std::make_unique<int[5]>(11, 22, 33, 44, 55); // deleted
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
#if _LIBCPP_STD_VER > 11
|
||||
{
|
||||
std::unique_ptr<int> p1 = std::make_unique<int>(1);
|
||||
assert ( *p1 == 1 );
|
||||
p1 = std::make_unique<int> ();
|
||||
assert ( *p1 == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<std::string> p2 = std::make_unique<std::string> ( "Meow!" );
|
||||
assert ( *p2 == "Meow!" );
|
||||
p2 = std::make_unique<std::string> ();
|
||||
assert ( *p2 == "" );
|
||||
p2 = std::make_unique<std::string> ( 6, 'z' );
|
||||
assert ( *p2 == "zzzzzz" );
|
||||
}
|
||||
#endif // _LIBCPP_STD_VER > 11
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<B> d2;
|
||||
std::default_delete<A> d1 = d2;
|
||||
A* p = new B;
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d1(p);
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<A> d;
|
||||
A* p = new A;
|
||||
assert(A::count == 1);
|
||||
d(p);
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
// Test that default_delete's operator() requires a complete type
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<A> d;
|
||||
A* p = 0;
|
||||
d(p);
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
// Test that default_delete's operator() requires a complete type
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<const void> d;
|
||||
const void* p = 0;
|
||||
d(p);
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
// Test that default_delete<T[]> does not have a working converting constructor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
};
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<B[]> d2;
|
||||
std::default_delete<A[]> d1 = d2;
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
// Test that default_delete<T[]> has a working default constructor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<A[]> d;
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
d(p);
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// default_delete
|
||||
|
||||
// Test that default_delete<T[]>'s operator() requires a complete type
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::default_delete<A[]> d;
|
||||
A* p = 0;
|
||||
d(p);
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't copy from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A> s(new A);
|
||||
std::unique_ptr<A> s2;
|
||||
s2 = s;
|
||||
}
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
// test move assignment. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[]> s1(new A[3]);
|
||||
A* p = s1.get();
|
||||
assert(A::count == 3);
|
||||
std::unique_ptr<A[]> s2(new A[2]);
|
||||
assert(A::count == 5);
|
||||
s2 = std::move(s1);
|
||||
assert(A::count == 3);
|
||||
assert(s2.get() == p);
|
||||
assert(s1.get() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
std::unique_ptr<A[], Deleter<A[]> > s1(new A[4], Deleter<A[]>(5));
|
||||
A* p = s1.get();
|
||||
assert(A::count == 4);
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2(new A[5]);
|
||||
assert(A::count == 9);
|
||||
s2 = std::move(s1);
|
||||
assert(s2.get() == p);
|
||||
assert(s1.get() == 0);
|
||||
assert(A::count == 4);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s1.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
CDeleter<A[]> d1(5);
|
||||
std::unique_ptr<A[], CDeleter<A[]>&> s1(new A[6], d1);
|
||||
A* p = s1.get();
|
||||
assert(A::count == 6);
|
||||
CDeleter<A[]> d2(6);
|
||||
std::unique_ptr<A[], CDeleter<A[]>&> s2(new A[3], d2);
|
||||
assert(A::count == 9);
|
||||
s2 = std::move(s1);
|
||||
assert(A::count == 6);
|
||||
assert(s2.get() == p);
|
||||
assert(s1.get() == 0);
|
||||
assert(d1.state() == 5);
|
||||
assert(d2.state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't copy from const lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<A[]> s(new A[3]);
|
||||
std::unique_ptr<A[]> s2;
|
||||
s2 = s;
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't copy from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A, Deleter> s(new A);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A, Deleter> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// test move ctor. Can't copy from const lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<A, Deleter> s(new A);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A, Deleter> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't assign from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// Can't assign from lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::unique_ptr<B[], Deleter<B> > s(new B);
|
||||
A* p = s.get();
|
||||
boost::unique_ptr<A[], Deleter<A> > s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// Can't assign from lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
Deleter<B> db(5);
|
||||
boost::unique_ptr<B[], Deleter<B>&> s(new B, db);
|
||||
A* p = s.get();
|
||||
Deleter<A> da(6);
|
||||
boost::unique_ptr<A[], Deleter<A>&> s2(new A, da);
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't assign from const lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const boost::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
boost::unique_ptr<A[]> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// Can't assign from const lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const boost::unique_ptr<B[], Deleter<B> > s(new B);
|
||||
A* p = s.get();
|
||||
boost::unique_ptr<A[], Deleter<A> > s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// Can't assign from const lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
Deleter<B> db(5);
|
||||
const boost::unique_ptr<B[], Deleter<B>&> s(new B, db);
|
||||
A* p = s.get();
|
||||
Deleter<A> da(6);
|
||||
boost::unique_ptr<A[], Deleter<A>&> s2(new A, da);
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
boost::unique_ptr<A[]> s2(new A);
|
||||
assert(A::count == 2);
|
||||
s2 = boost::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::unique_ptr<B[], Deleter<B> > s(new B);
|
||||
A* p = s.get();
|
||||
boost::unique_ptr<A[], Deleter<A> > s2(new A);
|
||||
assert(A::count == 2);
|
||||
s2 = (boost::move(s));
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// test converting move assignment with reference deleters
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
Deleter<B> db(5);
|
||||
boost::unique_ptr<B[], Deleter<B>&> s(new B, db);
|
||||
A* p = s.get();
|
||||
Deleter<A> da(6);
|
||||
boost::unique_ptr<A[], Deleter<A>&> s2(new A, da);
|
||||
s2 = boost::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// test assignment from null
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A> s2(new A);
|
||||
assert(A::count == 1);
|
||||
s2 = 0;
|
||||
assert(A::count == 0);
|
||||
assert(s2.get() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// The deleter is not called if get() == 0
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(0) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(void*) {++state_;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Deleter d;
|
||||
assert(d.state() == 0);
|
||||
{
|
||||
std::unique_ptr<int[], Deleter&> p(0, d);
|
||||
assert(p.get() == 0);
|
||||
assert(&p.get_deleter() == &d);
|
||||
}
|
||||
assert(d.state() == 0);
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// test assignment from null
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[]> s2(new A[3]);
|
||||
assert(A::count == 3);
|
||||
s2 = nullptr;
|
||||
assert(A::count == 0);
|
||||
assert(s2.get() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr<T[]>::pointer type
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
struct pointer {};
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::unique_ptr<int[]> P;
|
||||
static_assert((std::is_same<P::pointer, int*>::value), "");
|
||||
}
|
||||
{
|
||||
typedef std::unique_ptr<int[], Deleter> P;
|
||||
static_assert((std::is_same<P::pointer, Deleter::pointer>::value), "");
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr default ctor
|
||||
|
||||
// default unique_ptr ctor should require default Deleter ctor
|
||||
|
||||
// USE_VERIFY
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Deleter
|
||||
{
|
||||
// expected-error@memory:* {{base class 'Deleter' has private default constructor}}
|
||||
// expected-note@memory:* + {{in instantiation of member function}}
|
||||
Deleter() {} // expected-note {{implicitly declared private here}}
|
||||
|
||||
public:
|
||||
|
||||
Deleter(Deleter&) {}
|
||||
Deleter& operator=(Deleter&) { return *this; }
|
||||
|
||||
void operator()(void*) const {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[], Deleter> p;
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr default ctor
|
||||
|
||||
// default unique_ptr ctor should only require default Deleter ctor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(void*) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int[]> p;
|
||||
assert(p.get() == 0);
|
||||
}
|
||||
{
|
||||
std::unique_ptr<int[], Deleter> p;
|
||||
assert(p.get() == 0);
|
||||
assert(p.get_deleter().state() == 5);
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr default ctor
|
||||
|
||||
// default unique_ptr ctor should require non-reference Deleter ctor
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Deleter
|
||||
{
|
||||
public:
|
||||
|
||||
void operator()(void*) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[], Deleter&> p;
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test default unique_ptr<T[]> ctor
|
||||
|
||||
// default unique_ptr<T[]> ctor shouldn't require complete type
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p);
|
||||
};
|
||||
|
||||
void check(int i);
|
||||
|
||||
template <class D = std::default_delete<A> >
|
||||
struct B
|
||||
{
|
||||
std::unique_ptr<A[], D> a_;
|
||||
B();
|
||||
~B();
|
||||
|
||||
A* get() const {return a_.get();}
|
||||
D& get_deleter() {return a_.get_deleter();}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
B<> s;
|
||||
assert(s.get() == 0);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
B<Deleter> s;
|
||||
assert(s.get() == 0);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
}
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
void Deleter::operator()(A* p) {delete p;}
|
||||
|
||||
void check(int i)
|
||||
{
|
||||
assert(A::count == i);
|
||||
}
|
||||
|
||||
template <class D>
|
||||
B<D>::B() {}
|
||||
|
||||
template <class D>
|
||||
B<D>::~B() {}
|
@@ -0,0 +1,23 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr default ctor
|
||||
|
||||
// default unique_ptr ctor should require non-pointer Deleter
|
||||
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[], void (*)(void*)> p;
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// test move ctor. Can't copy from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[]> s(new A[3]);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
// test move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class NCDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
NCDeleter(NCDeleter&);
|
||||
NCDeleter& operator=(NCDeleter&);
|
||||
public:
|
||||
|
||||
NCDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[]> s(new A[3]);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 3);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
std::unique_ptr<A[], Deleter<A[]> > s(new A[3], Deleter<A[]>(5));
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 3);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
NCDeleter d;
|
||||
std::unique_ptr<A[], NCDeleter&> s(new A[3], d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], NCDeleter&> s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 3);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
// test move ctor. Can't copy from const lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<A[]> s(new A[3]);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
// test move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class NCDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
NCDeleter(NCDeleter&);
|
||||
NCDeleter& operator=(NCDeleter&);
|
||||
public:
|
||||
|
||||
NCDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
std::unique_ptr<A[]>
|
||||
source1()
|
||||
{
|
||||
return std::unique_ptr<A[]>(new A[3]);
|
||||
}
|
||||
|
||||
void sink1(std::unique_ptr<A[]> p)
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<A[], Deleter<A[]> >
|
||||
source2()
|
||||
{
|
||||
return std::unique_ptr<A[], Deleter<A[]> >(new A[3]);
|
||||
}
|
||||
|
||||
void sink2(std::unique_ptr<A[], Deleter<A[]> > p)
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<A[], NCDeleter&>
|
||||
source3()
|
||||
{
|
||||
static NCDeleter d;
|
||||
return std::unique_ptr<A[], NCDeleter&>(new A[3], d);
|
||||
}
|
||||
|
||||
void sink3(std::unique_ptr<A[], NCDeleter&> p)
|
||||
{
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
sink1(source1());
|
||||
sink2(source2());
|
||||
sink3(source3());
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
// test move ctor. Can't copy from lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[], Deleter> s(new A[3]);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
// test move ctor. Can't copy from const lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<A[], Deleter> s(new A[3]);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[], Deleter<B[]> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
CDeleter(CDeleter&);
|
||||
CDeleter& operator=(CDeleter&);
|
||||
public:
|
||||
|
||||
CDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<A> d;
|
||||
std::unique_ptr<B[], CDeleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], CDeleter<A>&> s2(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// implicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Implicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[], Deleter<B[]> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
CDeleter(CDeleter&);
|
||||
CDeleter& operator=(CDeleter&);
|
||||
public:
|
||||
|
||||
CDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<A> d;
|
||||
std::unique_ptr<B[], CDeleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], CDeleter<A>&> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<B[], Deleter<B[]> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
CDeleter(CDeleter&);
|
||||
CDeleter& operator=(CDeleter&);
|
||||
public:
|
||||
|
||||
CDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<A> d;
|
||||
const std::unique_ptr<B[], CDeleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], CDeleter<A>&> s2(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// implicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Implicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<B[], Deleter<B[]> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
CDeleter(CDeleter&);
|
||||
CDeleter& operator=(CDeleter&);
|
||||
public:
|
||||
|
||||
CDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<A> d;
|
||||
const std::unique_ptr<B[], CDeleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], CDeleter<A>&> s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2(std::move(s));
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[], Deleter<B[]> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2(std::move(s));
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
CDeleter(CDeleter&);
|
||||
CDeleter& operator=(CDeleter&);
|
||||
public:
|
||||
|
||||
CDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<A> d;
|
||||
std::unique_ptr<B[], CDeleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], CDeleter<A>&> s2(std::move(s));
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// implicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[]> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[]> s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Implicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B[], Deleter<B[]> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move ctor
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
// Explicit version
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
template <class T>
|
||||
class CDeleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
CDeleter(CDeleter&);
|
||||
CDeleter& operator=(CDeleter&);
|
||||
public:
|
||||
|
||||
CDeleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(T* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<A> d;
|
||||
std::unique_ptr<B[], CDeleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A[], CDeleter<A>&> s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
d.set_state(6);
|
||||
assert(s2.get_deleter().state() == d.state());
|
||||
assert(s.get_deleter().state() == d.state());
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// unique_ptr(nullptr_t);
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// default unique_ptr ctor should only require default Deleter ctor
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(void*) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int[]> p(nullptr);
|
||||
assert(p.get() == 0);
|
||||
}
|
||||
{
|
||||
std::unique_ptr<int[], Deleter> p(nullptr);
|
||||
assert(p.get() == 0);
|
||||
assert(p.get_deleter().state() == 5);
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr<T[]>(pointer) ctor
|
||||
|
||||
// unique_ptr<T[]>(pointer) ctor should require default Deleter ctor
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Deleter
|
||||
{
|
||||
|
||||
Deleter() {}
|
||||
|
||||
public:
|
||||
|
||||
Deleter(Deleter&) {}
|
||||
Deleter& operator=(Deleter&) {}
|
||||
|
||||
void operator()(void*) const {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[], Deleter> p(new int);
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer) ctor
|
||||
|
||||
// unique_ptr<T[]>(pointer) ctor should only require default Deleter ctor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
std::unique_ptr<A[]> s(p);
|
||||
assert(s.get() == p);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
std::unique_ptr<A[], Deleter> s(p);
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr<T[]>(pointer) ctor
|
||||
|
||||
#include <memory>
|
||||
|
||||
// unique_ptr<T[]>(pointer) ctor should require non-reference Deleter ctor
|
||||
class Deleter
|
||||
{
|
||||
public:
|
||||
|
||||
void operator()(void*) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[], Deleter&> p(new int);
|
||||
}
|
@@ -0,0 +1,95 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr<T[]>(pointer) ctor
|
||||
|
||||
// unique_ptr<T[]>(pointer) ctor shouldn't require complete type
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p);
|
||||
};
|
||||
|
||||
void check(int i);
|
||||
|
||||
template <class D = std::default_delete<A[]> >
|
||||
struct B
|
||||
{
|
||||
std::unique_ptr<A[], D> a_;
|
||||
explicit B(A*);
|
||||
~B();
|
||||
|
||||
A* get() const {return a_.get();}
|
||||
D& get_deleter() {return a_.get_deleter();}
|
||||
};
|
||||
|
||||
A* get();
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = get();
|
||||
check(3);
|
||||
B<> s(p);
|
||||
assert(s.get() == p);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
A* p = get();
|
||||
check(3);
|
||||
B<Deleter> s(p);
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
}
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
A* get() {return new A[3];}
|
||||
|
||||
void Deleter::operator()(A* p) {delete [] p;}
|
||||
|
||||
void check(int i)
|
||||
{
|
||||
assert(A::count == i);
|
||||
}
|
||||
|
||||
template <class D>
|
||||
B<D>::B(A* a) : a_(a) {}
|
||||
|
||||
template <class D>
|
||||
B<D>::~B() {}
|
@@ -0,0 +1,23 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr<T[]>(pointer) ctor
|
||||
|
||||
// unique_ptr<T[]>(pointer) ctor should require non-pointer Deleter
|
||||
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[], void (*)(void*)> p(new int);
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer) ctor
|
||||
|
||||
// unique_ptr(pointer) ctor should not work with derived pointers
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(Deleter&);
|
||||
Deleter& operator=(Deleter&);
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
B* p = new B[3];
|
||||
std::unique_ptr<A[]> s(p);
|
||||
}
|
||||
{
|
||||
B* p = new B[3];
|
||||
std::unique_ptr<A[], Deleter> s(p);
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer, deleter) ctor
|
||||
|
||||
// unique_ptr(pointer, deleter()) only requires MoveConstructible deleter
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
std::unique_ptr<A[], Deleter<A[]> > s(p, Deleter<A[]>());
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer, deleter) ctor
|
||||
|
||||
// unique_ptr(pointer, d) requires CopyConstructible deleter
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
Deleter d;
|
||||
std::unique_ptr<A[], Deleter> s(p, d);
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer, deleter) ctor
|
||||
|
||||
// unique_ptr<T[], D&>(pointer, d) does not requires CopyConstructible deleter
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(const Deleter&);
|
||||
Deleter& operator=(const Deleter&);
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
Deleter d;
|
||||
std::unique_ptr<A[], Deleter&> s(p, d);
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
assert(s.get_deleter().state() == 6);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer, deleter) ctor
|
||||
|
||||
// unique_ptr<T, const D&>(pointer, D()) should not compile
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(A* p) const {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
std::unique_ptr<A[], const Deleter&> s(p, Deleter());
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer, deleter) ctor
|
||||
|
||||
// unique_ptr<T[], const D&>(pointer, d) does not requires CopyConstructible deleter
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
Deleter(const Deleter&);
|
||||
Deleter& operator=(const Deleter&);
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(A* p) const {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p = new A[3];
|
||||
assert(A::count == 3);
|
||||
Deleter d;
|
||||
std::unique_ptr<A[], const Deleter&> s(p, d);
|
||||
assert(s.get() == p);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr(pointer, deleter) ctor
|
||||
|
||||
// unique_ptr(pointer, deleter) should not work with derived pointers
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete [] p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
B* p = new B[3];
|
||||
std::unique_ptr<A[], Deleter> s(p, Deleter());
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test release
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[]> p(new int[3]);
|
||||
int* i = p.get();
|
||||
int* j = p.release();
|
||||
assert(p.get() == 0);
|
||||
assert(i == j);
|
||||
delete [] j;
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test reset
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[]> p(new A[3]);
|
||||
assert(A::count == 3);
|
||||
A* i = p.get();
|
||||
p.reset();
|
||||
assert(A::count == 0);
|
||||
assert(p.get() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
std::unique_ptr<A[]> p(new A[4]);
|
||||
assert(A::count == 4);
|
||||
A* i = p.get();
|
||||
p.reset(new A[5]);
|
||||
assert(A::count == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test reset
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A[]> p(new A);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 0);
|
||||
A* i = p.get();
|
||||
p.reset(new B);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
{
|
||||
std::unique_ptr<A[]> p(new B);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
A* i = p.get();
|
||||
p.reset(new B);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test swap
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
int state_;
|
||||
static int count;
|
||||
A() : state_(0) {++count;}
|
||||
explicit A(int i) : state_(i) {++count;}
|
||||
A(const A& a) : state_(a.state_) {++count;}
|
||||
A& operator=(const A& a) {state_ = a.state_; return *this;}
|
||||
~A() {--count;}
|
||||
|
||||
friend bool operator==(const A& x, const A& y)
|
||||
{return x.state_ == y.state_;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
A* p1 = new A[3];
|
||||
std::unique_ptr<A[], Deleter<A[]> > s1(p1, Deleter<A[]>(1));
|
||||
A* p2 = new A[3];
|
||||
std::unique_ptr<A[], Deleter<A[]> > s2(p2, Deleter<A[]>(2));
|
||||
assert(s1.get() == p1);
|
||||
assert(s1.get_deleter().state() == 1);
|
||||
assert(s2.get() == p2);
|
||||
assert(s2.get_deleter().state() == 2);
|
||||
s1.swap(s2);
|
||||
assert(s1.get() == p2);
|
||||
assert(s1.get_deleter().state() == 2);
|
||||
assert(s2.get() == p1);
|
||||
assert(s2.get_deleter().state() == 1);
|
||||
assert(A::count == 6);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test op*()
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[]> p(new int(3));
|
||||
assert(*p == 3);
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test op*()
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int[]> p(new int [3]);
|
||||
if (p)
|
||||
;
|
||||
else
|
||||
assert(false);
|
||||
if (!p)
|
||||
assert(false);
|
||||
}
|
||||
{
|
||||
std::unique_ptr<int[]> p;
|
||||
if (!p)
|
||||
;
|
||||
else
|
||||
assert(false);
|
||||
if (p)
|
||||
assert(false);
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test get
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
int* p = new int[3];
|
||||
std::unique_ptr<int[]> s(p);
|
||||
assert(s.get() == p);
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test get_deleter()
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
void operator()(void*) {}
|
||||
|
||||
int test() {return 5;}
|
||||
int test() const {return 6;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int[], Deleter> p;
|
||||
assert(p.get_deleter().test() == 5);
|
||||
}
|
||||
{
|
||||
const std::unique_ptr<int[], Deleter> p;
|
||||
assert(p.get_deleter().test() == 6);
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test op[](size_t)
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
class A
|
||||
{
|
||||
int state_;
|
||||
static int next_;
|
||||
public:
|
||||
A() : state_(++next_) {}
|
||||
int get() const {return state_;}
|
||||
|
||||
friend bool operator==(const A& x, int y)
|
||||
{return x.state_ == y;}
|
||||
|
||||
A& operator=(int i) {state_ = i; return *this;}
|
||||
};
|
||||
|
||||
int A::next_ = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<A[]> p(new A[3]);
|
||||
assert(p[0] == 1);
|
||||
assert(p[1] == 2);
|
||||
assert(p[2] == 3);
|
||||
p[0] = 3;
|
||||
p[1] = 2;
|
||||
p[2] = 1;
|
||||
assert(p[0] == 3);
|
||||
assert(p[1] == 2);
|
||||
assert(p[2] == 1);
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// test op->()
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
int i_;
|
||||
|
||||
A() : i_(7) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<A[]> p(new A);
|
||||
assert(p->i_ == 7);
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr::pointer type
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
struct pointer {};
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::unique_ptr<int> P;
|
||||
static_assert((std::is_same<P::pointer, int*>::value), "");
|
||||
}
|
||||
{
|
||||
typedef std::unique_ptr<int, Deleter> P;
|
||||
static_assert((std::is_same<P::pointer, Deleter::pointer>::value), "");
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't copy from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A> s(new A);
|
||||
std::unique_ptr<A> s2;
|
||||
s2 = s;
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
// test move assignment. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A> s1(new A);
|
||||
A* p = s1.get();
|
||||
std::unique_ptr<A> s2(new A);
|
||||
assert(A::count == 2);
|
||||
s2 = std::move(s1);
|
||||
assert(A::count == 1);
|
||||
assert(s2.get() == p);
|
||||
assert(s1.get() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
std::unique_ptr<A, Deleter<A> > s1(new A, Deleter<A>(5));
|
||||
A* p = s1.get();
|
||||
std::unique_ptr<A, Deleter<A> > s2(new A);
|
||||
assert(A::count == 2);
|
||||
s2 = std::move(s1);
|
||||
assert(s2.get() == p);
|
||||
assert(s1.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s1.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
{
|
||||
CDeleter<A> d1(5);
|
||||
std::unique_ptr<A, CDeleter<A>&> s1(new A, d1);
|
||||
A* p = s1.get();
|
||||
CDeleter<A> d2(6);
|
||||
std::unique_ptr<A, CDeleter<A>&> s2(new A, d2);
|
||||
s2 = std::move(s1);
|
||||
assert(s2.get() == p);
|
||||
assert(s1.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(d1.state() == 5);
|
||||
assert(d2.state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't copy from const lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<A> s(new A);
|
||||
std::unique_ptr<A> s2;
|
||||
s2 = s;
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't copy from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<A, Deleter> s(new A);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A, Deleter> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr move ctor
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// test move ctor. Can't copy from const lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
class Deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
|
||||
Deleter() : state_(5) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void operator()(A* p) {delete p;}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<A, Deleter> s(new A);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A, Deleter> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't assign from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A> s2(new A);
|
||||
assert(A::count == 2);
|
||||
s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
// Can't assign from lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B, Deleter<B> > s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A, Deleter<A> > s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<B, Deleter<B> > s(new B, Deleter<B>(5));
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A, Deleter<A> > s2(new A);
|
||||
assert(A::count == 2);
|
||||
s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
assert(s.get_deleter().state() == 0);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// Can't assign from lvalue
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
Deleter<B> db(5);
|
||||
std::unique_ptr<B, Deleter<B>&> s(new B, db);
|
||||
A* p = s.get();
|
||||
Deleter<A> da(6);
|
||||
std::unique_ptr<A, Deleter<A>&> s2(new A, da);
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
// test converting move assignment with reference deleters
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../deleter.h"
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
CDeleter<B> db(5);
|
||||
std::unique_ptr<B, CDeleter<B>&> s(new B, db);
|
||||
A* p = s.get();
|
||||
CDeleter<A> da(6);
|
||||
std::unique_ptr<A, CDeleter<A>&> s2(new A, da);
|
||||
s2 = std::move(s);
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
assert(s.get_deleter().state() == 5);
|
||||
assert(s2.get_deleter().state() == 5);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <memory>
|
||||
|
||||
// unique_ptr
|
||||
|
||||
// Test unique_ptr converting move assignment
|
||||
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
|
||||
// Can't assign from const lvalue
|
||||
|
||||
struct A
|
||||
{
|
||||
static int count;
|
||||
A() {++count;}
|
||||
A(const A&) {++count;}
|
||||
virtual ~A() {--count;}
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
const std::unique_ptr<B> s(new B);
|
||||
A* p = s.get();
|
||||
std::unique_ptr<A> s2;
|
||||
s2 = s;
|
||||
assert(s2.get() == p);
|
||||
assert(s.get() == 0);
|
||||
assert(A::count == 1);
|
||||
assert(B::count == 1);
|
||||
}
|
||||
assert(A::count == 0);
|
||||
assert(B::count == 0);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user