File: func-params-09-awaitable-parms.C

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (106 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download | duplicates (2)
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
// { dg-do run }

// Check that we correctly handle params with non-trivial DTORs and
// use the correct copy/move CTORs.

#include "../coro.h"
#include <vector>

// boiler-plate for tests of codegen
#include "../coro1-ret-int-yield-int.h"

int regular = 0;
int copy = 0;
int move = 0;

/* This is a more sophisticated awaitable... */

struct FooAwaitable {
  FooAwaitable(int _v) : value(_v), x(1, _v)
    {
      regular++;
      PRINTF ("FooAwaitable(%d)\n",_v);
    }

  FooAwaitable(const FooAwaitable& t)
    {
      value = t.value;
      x = t.x;
      copy++;
      PRINTF ("FooAwaitable(&), %d\n",value);
    }

  FooAwaitable(FooAwaitable&& s)
    {
      value = s.value;
      s.value = -1;
      x = std::move(s.x);
      s.x = std::vector<int> ();
      move++;
      PRINTF ("FooAwaitable(&&), %d\n", value);
    }

  ~FooAwaitable() {PRINTF ("~FooAwaitable(), %d\n", value);}

  bool await_ready() { return false; }
  void await_suspend(coro::coroutine_handle<>) {}
  int await_resume() { return value + x[0];}

  void return_value(int _v) { value = _v;}

  int value;
  std::vector<int> x;
};

coro1
my_coro (FooAwaitable t_lv, FooAwaitable& t_ref, FooAwaitable&& t_rv_ref)
{
  PRINT ("my_coro");
  // We are created suspended, so correct operation depends on
  // the parms being copied.
  int sum = co_await t_lv;
  PRINT ("my_coro 1");
  sum += co_await t_ref;
  PRINT ("my_coro 2");
  // This can't work for the rvalue ref, it's always dangling.
  //sum += co_await t_rv_ref;
  //PRINT ("my_coro 3");
  co_return sum;
}

int main ()
{

  PRINT ("main: create coro1");
  FooAwaitable thing (4);
  coro1 x = my_coro (FooAwaitable (1), thing, FooAwaitable (2));
  PRINT ("main: done ramp");

  if (x.handle.done())
    abort();
  x.handle.resume();
  PRINT ("main: after resume (initial suspend)");

  // now do the three co_awaits.
  while(!x.handle.done())
    x.handle.resume();
  PRINT ("main: after resuming 2 co_awaits");

  /* Now we should have the co_returned value.  */
  int y = x.handle.promise().get_value();
  if (y != 10)
    {
      PRINTF ("main: wrong result (%d).", y);
      abort ();
    }

  if (regular != 3 || copy != 0 || move != 1)
    {
      PRINTF ("main: unexpected ctor use (R:%d, C:%d, M:%d)\n",
	      regular, copy, move);
      abort ();
    }

  PRINT ("main: returning");
  return 0;
}