jump_mov.cpp 1.6 KB

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