• 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/fiber.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     moveable data{ 1 };
43     ctx::fiber f{ std::allocator_arg, ctx::fixedsize_stack{},
44                      [&data](ctx::fiber && f){
45                         std::cout << "entered first time: " << data.value << std::endl;
46                         data = std::move( moveable{ 3 });
47                         f = std::move( f).resume();
48                         std::cout << "entered second time: " << data.value << std::endl;
49                         data = std::move( moveable{});
50                         return std::move( f);
51                      }};
52     f = std::move( f).resume();
53     std::cout << "returned first time: " << data.value << std::endl;
54     data.value = 5;
55     f = std::move( f).resume();
56     std::cout << "returned second time: " << data.value << std::endl;
57     std::cout << "main: done" << std::endl;
58     return EXIT_SUCCESS;
59 }
60