pointer03.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===// 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// ��������������������The LLVM Compiler Infrastructure 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is distributed under the University of Illinois Open Source 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// License. See LICENSE.TXT for details. 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===// 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// <memory> 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// unique_ptr 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Test unique_ptr(pointer) ctor 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <memory> 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cassert> 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// unique_ptr(pointer) ctor should work with derived pointers 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct A 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static int count; 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry A() {++count;} 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry A(const A&) {++count;} 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry virtual ~A() {--count;} 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint A::count = 0; 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct B 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry : public A 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static int count; 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry B() {++count;} 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry B(const B&) {++count;} 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry virtual ~B() {--count;} 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint B::count = 0; 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Deleter 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry int state_; 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry Deleter(Deleter&); 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry Deleter& operator=(Deleter&); 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic: 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry Deleter() : state_(5) {} 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry int state() const {return state_;} 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry void operator()(A* p) {delete p;} 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint main() 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry B* p = new B; 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(A::count == 1); 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(B::count == 1); 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::unique_ptr<A> s(p); 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(s.get() == p); 653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(A::count == 0); 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(B::count == 0); 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry B* p = new B; 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(A::count == 1); 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(B::count == 1); 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::unique_ptr<A, Deleter> s(p); 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(s.get() == p); 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(s.get_deleter().state() == 5); 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(A::count == 0); 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry assert(B::count == 0); 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry