• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //          Copyright Oliver Kowalke 2016.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <cstdlib>
8 #include <iostream>
9 
10 #include <boost/context/continuation.hpp>
11 
12 namespace ctx = boost::context;
13 
14 class moveable {
15 public:
16     int     value;
17 
moveable()18     moveable() :
19         value( -1) {
20         }
21 
moveable(int v)22     moveable( int v) :
23         value( v) {
24         }
25 
moveable(moveable && other)26     moveable( moveable && other) {
27         std::swap( value, other.value);
28         }
29 
operator =(moveable && other)30     moveable & operator=( moveable && other) {
31         if ( this == & other) return * this;
32         value = other.value;
33         other.value = -1;
34         return * this;
35     }
36 
37     moveable( moveable const& other) = delete;
38     moveable & operator=( moveable const& other) = delete;
39 };
40 
main()41 int main() {
42     ctx::continuation c;
43     moveable data{ 1 };
44     c = ctx::callcc( std::allocator_arg, ctx::fixedsize_stack{},
45                      [&data](ctx::continuation && c){
46                         std::cout << "entered first time: " << data.value << std::endl;
47                         data = std::move( moveable{ 3 });
48                         c = c.resume();
49                         std::cout << "entered second time: " << data.value << std::endl;
50                         data = std::move( moveable{});
51                         return std::move( c);
52                      });
53     std::cout << "returned first time: " << data.value << std::endl;
54     data.value = 5;
55     c = c.resume();
56     std::cout << "returned second time: " << data.value << std::endl;
57     std::cout << "main: done" << std::endl;
58     return EXIT_SUCCESS;
59 }
60