5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
17 coroutine() : value_(0) {}
18 bool is_child() const { return value_ < 0; }
19 bool is_parent() const { return !is_child(); }
20 bool is_complete() const { return value_ == -1; }
22 friend class coroutine_ref;
29 coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
30 coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
31 ~coroutine_ref() { if (!modified_) value_ = -1; }
32 operator int() const { return value_; }
33 int& operator=(int v) { modified_ = true; return value_ = v; }
35 void operator=(const coroutine_ref&);
40 #define CORO_REENTER(c) \
41 switch (coroutine_ref _coro_value = c) \
42 case -1: if (_coro_value) \
44 goto terminate_coroutine; \
45 terminate_coroutine: \
47 goto bail_out_of_coroutine; \
48 bail_out_of_coroutine: \
53 #define CORO_YIELD_IMPL(n) \
54 for (_coro_value = (n);;) \
55 if (_coro_value == 0) \
61 switch (_coro_value ? 0 : 1) \
63 case -1: if (_coro_value) \
64 goto terminate_coroutine; \
66 case 1: if (_coro_value) \
67 goto bail_out_of_coroutine; \
70 #define CORO_FORK_IMPL(n) \
71 for (_coro_value = -(n);; _coro_value = (n)) \
72 if (_coro_value == (n)) \
80 # define CORO_YIELD CORO_YIELD_IMPL(__COUNTER__ + 1)
81 # define CORO_FORK CORO_FORK_IMPL(__COUNTER__ + 1)
82 #else // defined(_MSC_VER)
83 # define CORO_YIELD CORO_YIELD_IMPL(__LINE__)
84 # define CORO_FORK CORO_FORK_IMPL(__LINE__)
85 #endif // defined(_MSC_VER)
87 #endif // COROUTINE_HPP