1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // shared_ptr 13 14 // Example move-only deleter 15 16 #ifndef DELETER_H 17 #define DELETER_H 18 19 #include <type_traits> 20 #include <cassert> 21 22 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS 23 #define DELETE_FUNCTION = delete 24 #else 25 #define DELETE_FUNCTION { assert(false); } 26 #endif 27 28 struct test_deleter_base 29 { 30 static int count; 31 static int dealloc_count; 32 }; 33 34 int test_deleter_base::count = 0; 35 int test_deleter_base::dealloc_count = 0; 36 37 template <class T> 38 class test_deleter 39 : public test_deleter_base 40 { 41 int state_; 42 43 public: 44 test_deleter()45 test_deleter() : state_(0) {++count;} test_deleter(int s)46 explicit test_deleter(int s) : state_(s) {++count;} test_deleter(const test_deleter & d)47 test_deleter(const test_deleter& d) 48 : state_(d.state_) {++count;} ~test_deleter()49 ~test_deleter() {assert(state_ >= 0); --count; state_ = -1;} 50 state()51 int state() const {return state_;} set_state(int i)52 void set_state(int i) {state_ = i;} 53 operator()54 void operator()(T* p) {assert(state_ >= 0); ++dealloc_count; delete p;} 55 56 test_deleter* operator&() const DELETE_FUNCTION; 57 }; 58 59 template <class T> 60 void swap(test_deleter<T> & x,test_deleter<T> & y)61swap(test_deleter<T>& x, test_deleter<T>& y) 62 { 63 test_deleter<T> t(std::move(x)); 64 x = std::move(y); 65 y = std::move(t); 66 } 67 68 #endif // DELETER_H 69