submit.cpp
11.1 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//
// submit.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// 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)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include "asio/execution/submit.hpp"
#include "asio/error_code.hpp"
#include "../unit_test.hpp"
namespace exec = asio::execution;
static int call_count = 0;
struct operation_state
{
void start() ASIO_NOEXCEPT
{
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_START_MEMBER_TRAIT)
template <>
struct start_member<operation_state>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_START_MEMBER_TRAIT)
} // namespace traits
} // namespace asio
struct no_submit_1
{
};
struct no_submit_2 : exec::sender_base
{
};
struct no_submit_3
{
template <typename R>
void submit(ASIO_MOVE_ARG(R) r)
{
(void)r;
}
};
#if !defined(ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT)
namespace asio {
namespace traits {
template <typename R>
struct submit_member<no_submit_3, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef void result_type;
};
} // namespace traits
} // namespace asio
#endif // !defined(ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT)
struct const_member_submit : exec::sender_base
{
const_member_submit()
{
}
template <typename R>
operation_state connect(ASIO_MOVE_ARG(R) r) const
{
(void)r;
return operation_state();
}
template <typename R>
void submit(ASIO_MOVE_ARG(R) r) const
{
(void)r;
++call_count;
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_CONNECT_MEMBER_TRAIT)
template <typename R>
struct connect_member<const const_member_submit, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef operation_state result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_CONNECT_MEMBER_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT)
template <typename R>
struct submit_member<const const_member_submit, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT)
} // namespace traits
} // namespace asio
struct free_submit_const_receiver : exec::sender_base
{
free_submit_const_receiver()
{
}
template <typename R>
friend operation_state connect(
const free_submit_const_receiver&, ASIO_MOVE_ARG(R) r)
{
(void)r;
return operation_state();
}
template <typename R>
friend void submit(
const free_submit_const_receiver&, ASIO_MOVE_ARG(R) r)
{
(void)r;
++call_count;
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_CONNECT_FREE_TRAIT)
template <typename R>
struct connect_free<const free_submit_const_receiver, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef operation_state result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_CONNECT_FREE_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_SUBMIT_FREE_TRAIT)
template <typename R>
struct submit_free<const free_submit_const_receiver, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SUBMIT_FREE_TRAIT)
} // namespace traits
} // namespace asio
struct non_const_member_submit : exec::sender_base
{
non_const_member_submit()
{
}
template <typename R>
operation_state connect(ASIO_MOVE_ARG(R) r)
{
(void)r;
return operation_state();
}
template <typename R>
void submit(ASIO_MOVE_ARG(R) r)
{
(void)r;
++call_count;
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_CONNECT_MEMBER_TRAIT)
template <typename R>
struct connect_member<non_const_member_submit, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef operation_state result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_CONNECT_MEMBER_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT)
template <typename R>
struct submit_member<non_const_member_submit, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SUBMIT_MEMBER_TRAIT)
} // namespace traits
} // namespace asio
struct free_submit_non_const_receiver : exec::sender_base
{
free_submit_non_const_receiver()
{
}
template <typename R>
friend operation_state connect(
free_submit_non_const_receiver&, ASIO_MOVE_ARG(R) r)
{
(void)r;
return operation_state();
}
template <typename R>
friend void submit(
free_submit_non_const_receiver&, ASIO_MOVE_ARG(R) r)
{
(void)r;
++call_count;
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_CONNECT_FREE_TRAIT)
template <typename R>
struct connect_free<free_submit_non_const_receiver, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef operation_state result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_CONNECT_FREE_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_SUBMIT_FREE_TRAIT)
template <typename R>
struct submit_free<free_submit_non_const_receiver, R>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SUBMIT_FREE_TRAIT)
} // namespace traits
} // namespace asio
struct receiver
{
receiver()
{
}
receiver(const receiver&)
{
}
#if defined(ASIO_HAS_MOVE)
receiver(receiver&&) ASIO_NOEXCEPT
{
}
#endif // defined(ASIO_HAS_MOVE)
template <typename E>
void set_error(ASIO_MOVE_ARG(E) e) ASIO_NOEXCEPT
{
(void)e;
}
void set_done() ASIO_NOEXCEPT
{
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
template <typename E>
struct set_error_member<receiver, E>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_SET_DONE_MEMBER_TRAIT)
template <>
struct set_done_member<receiver>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SET_DONE_MEMBER_TRAIT)
} // namespace traits
} // namespace asio
struct executor
{
executor()
{
}
executor(const executor&) ASIO_NOEXCEPT
{
}
#if defined(ASIO_HAS_MOVE)
executor(executor&&) ASIO_NOEXCEPT
{
}
#endif // defined(ASIO_HAS_MOVE)
template <typename F>
void execute(ASIO_MOVE_ARG(F) f) const ASIO_NOEXCEPT
{
(void)f;
++call_count;
}
bool operator==(const executor&) const ASIO_NOEXCEPT
{
return true;
}
bool operator!=(const executor&) const ASIO_NOEXCEPT
{
return false;
}
};
namespace asio {
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
template <typename F>
struct execute_member<executor, F>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
typedef void result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
template <>
struct equality_comparable<executor>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
};
#endif // !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
} // namespace traits
} // namespace asio
void test_can_submit()
{
ASIO_CONSTEXPR bool b1 = exec::can_submit<
no_submit_1&, receiver>::value;
ASIO_CHECK(b1 == false);
ASIO_CONSTEXPR bool b2 = exec::can_submit<
const no_submit_1&, receiver>::value;
ASIO_CHECK(b2 == false);
ASIO_CONSTEXPR bool b3 = exec::can_submit<
no_submit_2&, receiver>::value;
ASIO_CHECK(b3 == false);
ASIO_CONSTEXPR bool b4 = exec::can_submit<
const no_submit_2&, receiver>::value;
ASIO_CHECK(b4 == false);
ASIO_CONSTEXPR bool b5 = exec::can_submit<
no_submit_3&, receiver>::value;
ASIO_CHECK(b5 == false);
ASIO_CONSTEXPR bool b6 = exec::can_submit<
const no_submit_3&, receiver>::value;
ASIO_CHECK(b6 == false);
ASIO_CONSTEXPR bool b7 = exec::can_submit<
const_member_submit&, receiver>::value;
ASIO_CHECK(b7 == true);
ASIO_CONSTEXPR bool b8 = exec::can_submit<
const const_member_submit&, receiver>::value;
ASIO_CHECK(b8 == true);
ASIO_CONSTEXPR bool b9 = exec::can_submit<
free_submit_const_receiver&, receiver>::value;
ASIO_CHECK(b9 == true);
ASIO_CONSTEXPR bool b10 = exec::can_submit<
const free_submit_const_receiver&, receiver>::value;
ASIO_CHECK(b10 == true);
ASIO_CONSTEXPR bool b11 = exec::can_submit<
non_const_member_submit&, receiver>::value;
ASIO_CHECK(b11 == true);
ASIO_CONSTEXPR bool b12 = exec::can_submit<
const non_const_member_submit&, receiver>::value;
ASIO_CHECK(b12 == false);
ASIO_CONSTEXPR bool b13 = exec::can_submit<
free_submit_non_const_receiver&, receiver>::value;
ASIO_CHECK(b13 == true);
ASIO_CONSTEXPR bool b14 = exec::can_submit<
const free_submit_non_const_receiver&, receiver>::value;
ASIO_CHECK(b14 == false);
ASIO_CONSTEXPR bool b15 = exec::can_submit<
executor&, receiver>::value;
ASIO_CHECK(b15 == true);
ASIO_CONSTEXPR bool b16 = exec::can_submit<
const executor&, receiver>::value;
ASIO_CHECK(b16 == true);
}
void increment(int* count)
{
++(*count);
}
void test_submit()
{
receiver r;
call_count = 0;
const_member_submit s1;
exec::submit(s1, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
const const_member_submit s2;
exec::submit(s2, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
exec::submit(const_member_submit(), r);
ASIO_CHECK(call_count == 1);
call_count = 0;
free_submit_const_receiver s3;
exec::submit(s3, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
const free_submit_const_receiver s4;
exec::submit(s4, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
exec::submit(free_submit_const_receiver(), r);
ASIO_CHECK(call_count == 1);
call_count = 0;
non_const_member_submit s5;
exec::submit(s5, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
free_submit_non_const_receiver s6;
exec::submit(s6, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
executor s7;
exec::submit(s7, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
const executor s8;
exec::submit(s8, r);
ASIO_CHECK(call_count == 1);
call_count = 0;
exec::submit(executor(), r);
ASIO_CHECK(call_count == 1);
}
ASIO_TEST_SUITE
(
"submit",
ASIO_TEST_CASE(test_can_submit)
ASIO_TEST_CASE(test_submit)
)