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 // <functional> 11 12 // class function<R(ArgTypes...)> 13 14 // template <MoveConstructible R, MoveConstructible ... ArgTypes> 15 // void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&); 16 17 18 #include <functional> 19 #include <cstdlib> 20 #include <cassert> 21 22 #include "count_new.hpp" 23 24 class A 25 { 26 int data_[10]; 27 public: 28 static int count; 29 A(int j)30 explicit A(int j) 31 { 32 ++count; 33 data_[0] = j; 34 } 35 A(const A & a)36 A(const A& a) 37 { 38 ++count; 39 for (int i = 0; i < 10; ++i) 40 data_[i] = a.data_[i]; 41 } 42 ~A()43 ~A() {--count;} 44 operator ()(int i) const45 int operator()(int i) const 46 { 47 for (int j = 0; j < 10; ++j) 48 i += data_[j]; 49 return i; 50 } 51 id() const52 int id() const {return data_[0];} 53 }; 54 55 int A::count = 0; 56 g(int)57int g(int) {return 0;} h(int)58int h(int) {return 1;} 59 main()60int main() 61 { 62 assert(globalMemCounter.checkOutstandingNewEq(0)); 63 { 64 std::function<int(int)> f1 = A(1); 65 std::function<int(int)> f2 = A(2); 66 assert(A::count == 2); 67 assert(globalMemCounter.checkOutstandingNewEq(2)); 68 assert(f1.target<A>()->id() == 1); 69 assert(f2.target<A>()->id() == 2); 70 swap(f1, f2); 71 assert(A::count == 2); 72 assert(globalMemCounter.checkOutstandingNewEq(2)); 73 assert(f1.target<A>()->id() == 2); 74 assert(f2.target<A>()->id() == 1); 75 } 76 assert(A::count == 0); 77 assert(globalMemCounter.checkOutstandingNewEq(0)); 78 { 79 std::function<int(int)> f1 = A(1); 80 std::function<int(int)> f2 = g; 81 assert(A::count == 1); 82 assert(globalMemCounter.checkOutstandingNewEq(1)); 83 assert(f1.target<A>()->id() == 1); 84 assert(*f2.target<int(*)(int)>() == g); 85 swap(f1, f2); 86 assert(A::count == 1); 87 assert(globalMemCounter.checkOutstandingNewEq(1)); 88 assert(*f1.target<int(*)(int)>() == g); 89 assert(f2.target<A>()->id() == 1); 90 } 91 assert(A::count == 0); 92 assert(globalMemCounter.checkOutstandingNewEq(0)); 93 { 94 std::function<int(int)> f1 = g; 95 std::function<int(int)> f2 = A(1); 96 assert(A::count == 1); 97 assert(globalMemCounter.checkOutstandingNewEq(1)); 98 assert(*f1.target<int(*)(int)>() == g); 99 assert(f2.target<A>()->id() == 1); 100 swap(f1, f2); 101 assert(A::count == 1); 102 assert(globalMemCounter.checkOutstandingNewEq(1)); 103 assert(f1.target<A>()->id() == 1); 104 assert(*f2.target<int(*)(int)>() == g); 105 } 106 assert(A::count == 0); 107 assert(globalMemCounter.checkOutstandingNewEq(0)); 108 { 109 std::function<int(int)> f1 = g; 110 std::function<int(int)> f2 = h; 111 assert(A::count == 0); 112 assert(globalMemCounter.checkOutstandingNewEq(0)); 113 assert(*f1.target<int(*)(int)>() == g); 114 assert(*f2.target<int(*)(int)>() == h); 115 swap(f1, f2); 116 assert(A::count == 0); 117 assert(globalMemCounter.checkOutstandingNewEq(0)); 118 assert(*f1.target<int(*)(int)>() == h); 119 assert(*f2.target<int(*)(int)>() == g); 120 } 121 assert(A::count == 0); 122 assert(globalMemCounter.checkOutstandingNewEq(0)); 123 } 124