File: invoke_rvalue_pass.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (227 lines) | stat: -rw-r--r-- 5,193 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//===----------------------------------------------------------------------===//
//
//                     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.
//
//===----------------------------------------------------------------------===//

// Copyright (C) 2014 Vicente J. Botet Escriba
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// <boost/thread/detail/invoke.hpp>

#include <boost/thread/detail/invoke.hpp>
#include <boost/detail/lightweight_test.hpp>

int count = 0;

// 1 arg, return void

void f_void_1(int i)
{
    count += i;
}

struct A_void_1
{
    void operator()(int i)
    {
        count += i;
    }

    void mem1() {++count;}
    void mem2() const {count += 2;}
};

void
test_void_1()
{
    int save_count = count;
    // function
    {
    boost::detail::invoke(f_void_1, 2);
    BOOST_TEST(count == save_count + 2);
    save_count = count;
    }
    // function pointer
    {
    void (*fp)(int) = f_void_1;
    boost::detail::invoke(fp, 3);
    BOOST_TEST(count == save_count+3);
    save_count = count;
    }
    // functor
    {
    A_void_1 a0;
#if defined BOOST_THREAD_PROVIDES_INVOKE
    boost::detail::invoke(a0, 4);
    BOOST_TEST(count == save_count+4);
    save_count = count;
#endif
    boost::detail::invoke<void>(a0, 4);
    BOOST_TEST(count == save_count+4);
    save_count = count;
    }
    // member function pointer
    {
#if defined BOOST_THREAD_PROVIDES_INVOKE
    void (A_void_1::*fp)() = &A_void_1::mem1;
    boost::detail::invoke(fp, A_void_1());
    BOOST_TEST(count == save_count+1);
    save_count = count;
    //BUG
    boost::detail::invoke<void>(fp, A_void_1());
    BOOST_TEST(count == save_count+1);
    save_count = count;

#endif
#if defined BOOST_THREAD_PROVIDES_INVOKE
    A_void_1 a;
    boost::detail::invoke(fp, &a);
    BOOST_TEST(count == save_count+1);
    save_count = count;
    //BUG
    boost::detail::invoke<int>(fp, &a);
    BOOST_TEST(count == save_count+1);
    save_count = count;

#endif
    }
    // const member function pointer
    {
    void (A_void_1::*fp)() const = &A_void_1::mem2;
    boost::detail::invoke(fp, A_void_1());
    BOOST_TEST(count == save_count+2);
    save_count = count;
    A_void_1 a;
    boost::detail::invoke(fp, &a);
    BOOST_TEST(count == save_count+2);
    save_count = count;
    }
}

// 1 arg, return int

int f_int_1(int i)
{
    return i + 1;
}

struct A_int_1
{
    A_int_1() : data_(5) {}
    int operator()(int i)
    {
        return i - 1;
    }

    int mem1() {return 3;}
    int mem2() const {return 4;}
    int data_;
};

void
test_int_1()
{
    // function
    {
    BOOST_TEST(boost::detail::invoke(f_int_1, 2) == 3);
    }
    // function pointer
    {
    int (*fp)(int) = f_int_1;
    BOOST_TEST(boost::detail::invoke(fp, 3) == 4);
    }
    // functor
    {
#if defined BOOST_THREAD_PROVIDES_INVOKE
    BOOST_TEST(boost::detail::invoke(A_int_1(), 4) == 3);
    BOOST_TEST(boost::detail::invoke<int>(A_int_1(), 4) == 3);
#endif
    }
    // member function pointer
    {
#if defined BOOST_THREAD_PROVIDES_INVOKE
    BOOST_TEST(boost::detail::invoke(&A_int_1::mem1, A_int_1()) == 3);
    BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, A_int_1()) == 3);
#endif

    A_int_1 a;
    BOOST_TEST(boost::detail::invoke(&A_int_1::mem1, &a) == 3);
    }
    // const member function pointer
    {
    BOOST_TEST(boost::detail::invoke(&A_int_1::mem2, A_int_1()) == 4);
    A_int_1 a;
    BOOST_TEST(boost::detail::invoke(&A_int_1::mem2, &a) == 4);
    }
    // member data pointer
    {
#if defined BOOST_THREAD_PROVIDES_INVOKE
    BOOST_TEST(boost::detail::invoke(&A_int_1::data_, A_int_1()) == 5);
    BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, A_int_1()) == 5);
    A_int_1 a;
    BOOST_TEST(boost::detail::invoke(&A_int_1::data_, a) == 5);
    boost::detail::invoke(&A_int_1::data_, a) = 6;
    BOOST_TEST(boost::detail::invoke(&A_int_1::data_, a) == 6);
    BOOST_TEST(boost::detail::invoke(&A_int_1::data_, &a) == 6);
    boost::detail::invoke(&A_int_1::data_, &a) = 7;
    BOOST_TEST(boost::detail::invoke(&A_int_1::data_, &a) == 7);
#endif
    }
}

// 2 arg, return void

void f_void_2(int i, int j)
{
    count += i+j;
}

struct A_void_2
{
    void operator()(int i, int j)
    {
        count += i+j;
    }

    void mem1(int i) {count += i;}
    void mem2(int i) const {count += i;}
};

void
test_void_2()
{
    int save_count = count;
    // function
    {
    boost::detail::invoke(f_void_2, 2, 3);
    BOOST_TEST(count == save_count+5);
    save_count = count;
    }
    // member function pointer
    {
#if defined BOOST_THREAD_PROVIDES_INVOKE
    boost::detail::invoke(&A_void_2::mem1, A_void_2(), 3);
    BOOST_TEST(count == save_count+3);
    save_count = count;

    boost::detail::invoke<void>(&A_void_2::mem1, A_void_2(), 3);
    BOOST_TEST(count == save_count+3);
    save_count = count;
#endif

    }
}

int main()
{
    test_void_1();
    test_int_1();
    test_void_2();
    return boost::report_errors();
}