KiCad PCB EDA Suite
coroutine_example.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <string>
3 
4 #include <tool/coroutine.h>
5 
7 
8 class MyClass
9 {
10 public:
11  int CountTo( int n )
12  {
13  printf( "%s: Coroutine says hi. I will count from 1 to %d and yield each value.\n",
14  __FUNCTION__,
15  n );
16 
17  for( int i = 1; i <= n; i++ )
18  {
19  printf( "%s: Yielding %d\n", __FUNCTION__, i );
20  cofunc.Yield( i );
21  }
22  }
23 
24  void Run()
25  {
27  printf( "%s: Calling coroutine that will count from 1 to 5.\n", __FUNCTION__ );
28  cofunc.Call( 5 );
29 
30  while( cofunc.Running() )
31  {
32  printf( "%s: Got value: %d\n", __FUNCTION__, cofunc.ReturnValue() );
33  cofunc.Resume();
34  }
35 
36  printf( "%s: Done!\n", __FUNCTION__ );
37  }
38 
40 };
41 
42 
43 main() {
44  MyClass obj;
45 
46  obj.Run();
47 
48  return 0;
49 }
bool Call(ArgType aArg)
Function Call()
Definition: coroutine.h:204
MyCoroutine cofunc
const ReturnType & ReturnValue() const
Function ReturnValue()
Definition: coroutine.h:269
bool Resume()
Function Resume()
Definition: coroutine.h:238
COROUTINE< int, int > MyCoroutine
int CountTo(int n)
bool Running() const
Function Running()
Definition: coroutine.h:279