You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
963 B

  1. #include <cstdio>
  2. #include <string>
  3. #include <tool/coroutine.h>
  4. typedef COROUTINE<int, int> MyCoroutine;
  5. class MyClass
  6. {
  7. public:
  8. int CountTo( int n )
  9. {
  10. printf( "%s: Coroutine says hi. I will count from 1 to %d and yield each value.\n",
  11. __FUNCTION__,
  12. n );
  13. for( int i = 1; i <= n; i++ )
  14. {
  15. printf( "%s: Yielding %d\n", __FUNCTION__, i );
  16. cofunc.Yield( i );
  17. }
  18. }
  19. void Run()
  20. {
  21. cofunc = MyCoroutine( this, &MyClass::CountTo );
  22. printf( "%s: Calling coroutine that will count from 1 to 5.\n", __FUNCTION__ );
  23. cofunc.Call( 5 );
  24. while( cofunc.Running() )
  25. {
  26. printf( "%s: Got value: %d\n", __FUNCTION__, cofunc.ReturnValue() );
  27. cofunc.Resume();
  28. }
  29. printf( "%s: Done!\n", __FUNCTION__ );
  30. }
  31. MyCoroutine cofunc;
  32. };
  33. main() {
  34. MyClass obj;
  35. obj.Run();
  36. return 0;
  37. }