2013-08-02 14:50:29 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <tool/coroutine.h>
|
|
|
|
|
|
|
|
typedef COROUTINE<int, int> MyCoroutine;
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int CountTo( int n )
|
|
|
|
{
|
|
|
|
printf( "%s: Coroutine says hi. I will count from 1 to %d and yield each value.\n",
|
|
|
|
__FUNCTION__,
|
|
|
|
n );
|
|
|
|
|
|
|
|
for( int i = 1; i <= n; i++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
printf( "%s: Yielding %d\n", __FUNCTION__, i );
|
|
|
|
cofunc.Yield( i );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-10-14 14:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Run()
|
|
|
|
{
|
|
|
|
cofunc = MyCoroutine( this, &MyClass::CountTo );
|
|
|
|
printf( "%s: Calling coroutine that will count from 1 to 5.\n", __FUNCTION__ );
|
|
|
|
cofunc.Call( 5 );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
while( cofunc.Running() )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
printf( "%s: Got value: %d\n", __FUNCTION__, cofunc.ReturnValue() );
|
|
|
|
cofunc.Resume();
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
printf( "%s: Done!\n", __FUNCTION__ );
|
|
|
|
}
|
|
|
|
|
|
|
|
MyCoroutine cofunc;
|
2013-08-02 14:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
main() {
|
2013-10-14 11:43:57 +00:00
|
|
|
MyClass obj;
|
2013-08-02 14:50:29 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
obj.Run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|