ADT
        arm-eh
        ast-experimental
        enum
        forall-pointer-decay
        jacob/cs343-translation
        new-ast
        new-ast-unique-expr
        pthread-emulation
        qualifiedEnum
      
      
        
          | Last change
 on this file since c7816be was             571bf75, checked in by tdelisle <tdelisle@…>, 7 years ago | 
        
          | 
Clean existing coroutines using std::experimental::suspend_never and fixed Format coroutines to use RAII
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.4 KB | 
      
      
| Rev | Line |  | 
|---|
| [571bf75] | 1 | #include <iostream> | 
|---|
|  | 2 | #include <experimental/coroutine> | 
|---|
| [9cb4fc8] | 3 |  | 
|---|
|  | 4 | struct counter_cor { | 
|---|
|  | 5 | struct promise_type { | 
|---|
|  | 6 | counter_cor get_return_object() { | 
|---|
|  | 7 | return counter_cor(std::experimental::coroutine_handle<promise_type>::from_promise(*this)); | 
|---|
|  | 8 | } | 
|---|
|  | 9 |  | 
|---|
| [571bf75] | 10 | auto initial_suspend() { return std::experimental::suspend_never(); } | 
|---|
|  | 11 | auto final_suspend()   { return std::experimental::suspend_never(); } | 
|---|
| [9cb4fc8] | 12 |  | 
|---|
|  | 13 | void return_void() {} | 
|---|
|  | 14 |  | 
|---|
|  | 15 | void unhandled_exception() {} | 
|---|
|  | 16 | }; | 
|---|
|  | 17 |  | 
|---|
|  | 18 | std::experimental::coroutine_handle<promise_type> _coroutine = nullptr; | 
|---|
|  | 19 |  | 
|---|
|  | 20 | explicit counter_cor(std::experimental::coroutine_handle<promise_type> coroutine) | 
|---|
|  | 21 | : _coroutine(coroutine) | 
|---|
|  | 22 | {} | 
|---|
|  | 23 |  | 
|---|
|  | 24 | ~counter_cor() { | 
|---|
|  | 25 | if(_coroutine) { _coroutine.destroy(); } | 
|---|
|  | 26 | } | 
|---|
|  | 27 |  | 
|---|
|  | 28 | counter_cor() = default; | 
|---|
|  | 29 | counter_cor(counter_cor const &) = delete; | 
|---|
|  | 30 | counter_cor& operator=(counter_cor const &) = delete; | 
|---|
|  | 31 |  | 
|---|
|  | 32 | counter_cor(counter_cor&& other) { | 
|---|
|  | 33 | std::swap(_coroutine, other._coroutine); | 
|---|
|  | 34 | } | 
|---|
|  | 35 |  | 
|---|
|  | 36 | counter_cor& operator=(counter_cor&& other) { | 
|---|
|  | 37 | if(&other != this) { | 
|---|
|  | 38 | _coroutine = other._coroutine; | 
|---|
|  | 39 | other._coroutine = nullptr; | 
|---|
|  | 40 | } | 
|---|
|  | 41 | return *this; | 
|---|
|  | 42 | } | 
|---|
|  | 43 |  | 
|---|
|  | 44 | void resume() { _coroutine.resume(); } | 
|---|
|  | 45 | }; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | counter_cor counter() { | 
|---|
|  | 48 | std::cout << "Counter: called\n"; | 
|---|
|  | 49 | for(unsigned i = 1;; i++) { | 
|---|
| [571bf75] | 50 | co_await std::experimental::suspend_always{}; | 
|---|
| [9cb4fc8] | 51 | std::cout << "Counter: Resumed " << i << " time(s)\n"; | 
|---|
|  | 52 | } | 
|---|
|  | 53 | } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | int main() { | 
|---|
|  | 56 | std::cout << "Main: calling counter\n"; | 
|---|
|  | 57 | auto c = counter(); | 
|---|
|  | 58 | std::cout << "Main: resumes\n"; | 
|---|
|  | 59 | c.resume(); | 
|---|
|  | 60 | c.resume(); | 
|---|
|  | 61 | std::cout << "Main: done\n"; | 
|---|
|  | 62 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.