• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // void swap(function& other);
15 
16 #include <functional>
17 #include <new>
18 #include <cstdlib>
19 #include <cassert>
20 
21 int new_called = 0;
22 
operator new(std::size_t s)23 void* operator new(std::size_t s) throw(std::bad_alloc)
24 {
25     ++new_called;
26     return std::malloc(s);
27 }
28 
operator delete(void * p)29 void  operator delete(void* p) throw()
30 {
31     --new_called;
32     std::free(p);
33 }
34 
35 class A
36 {
37     int data_[10];
38 public:
39     static int count;
40 
A(int j)41     explicit A(int j)
42     {
43         ++count;
44         data_[0] = j;
45     }
46 
A(const A & a)47     A(const A& a)
48     {
49         ++count;
50         for (int i = 0; i < 10; ++i)
51             data_[i] = a.data_[i];
52     }
53 
~A()54     ~A() {--count;}
55 
operator ()(int i) const56     int operator()(int i) const
57     {
58         for (int j = 0; j < 10; ++j)
59             i += data_[j];
60         return i;
61     }
62 
id() const63     int id() const {return data_[0];}
64 };
65 
66 int A::count = 0;
67 
g(int)68 int g(int) {return 0;}
h(int)69 int h(int) {return 1;}
70 
main()71 int main()
72 {
73     assert(new_called == 0);
74     {
75     std::function<int(int)> f1 = A(1);
76     std::function<int(int)> f2 = A(2);
77     assert(A::count == 2);
78     assert(new_called == 2);
79     assert(f1.target<A>()->id() == 1);
80     assert(f2.target<A>()->id() == 2);
81     f1.swap(f2);
82     assert(A::count == 2);
83     assert(new_called == 2);
84     assert(f1.target<A>()->id() == 2);
85     assert(f2.target<A>()->id() == 1);
86     }
87     assert(A::count == 0);
88     assert(new_called == 0);
89     {
90     std::function<int(int)> f1 = A(1);
91     std::function<int(int)> f2 = g;
92     assert(A::count == 1);
93     assert(new_called == 1);
94     assert(f1.target<A>()->id() == 1);
95     assert(*f2.target<int(*)(int)>() == g);
96     f1.swap(f2);
97     assert(A::count == 1);
98     assert(new_called == 1);
99     assert(*f1.target<int(*)(int)>() == g);
100     assert(f2.target<A>()->id() == 1);
101     }
102     assert(A::count == 0);
103     assert(new_called == 0);
104     {
105     std::function<int(int)> f1 = g;
106     std::function<int(int)> f2 = A(1);
107     assert(A::count == 1);
108     assert(new_called == 1);
109     assert(*f1.target<int(*)(int)>() == g);
110     assert(f2.target<A>()->id() == 1);
111     f1.swap(f2);
112     assert(A::count == 1);
113     assert(new_called == 1);
114     assert(f1.target<A>()->id() == 1);
115     assert(*f2.target<int(*)(int)>() == g);
116     }
117     assert(A::count == 0);
118     assert(new_called == 0);
119     {
120     std::function<int(int)> f1 = g;
121     std::function<int(int)> f2 = h;
122     assert(A::count == 0);
123     assert(new_called == 0);
124     assert(*f1.target<int(*)(int)>() == g);
125     assert(*f2.target<int(*)(int)>() == h);
126     f1.swap(f2);
127     assert(A::count == 0);
128     assert(new_called == 0);
129     assert(*f1.target<int(*)(int)>() == h);
130     assert(*f2.target<int(*)(int)>() == g);
131     }
132     assert(A::count == 0);
133     assert(new_called == 0);
134 }
135