fix8  version 1.4.0
Open Source C++ FIX Framework
coroutine.hpp
Go to the documentation of this file.
1 //
2 // coroutine.hpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)
9 //
10 
11 #ifndef COROUTINE_HPP
12 #define COROUTINE_HPP
13 
14 class coroutine
15 {
16  friend class coroutine_ref;
17  int value_;
18 
19 public:
20  coroutine() : value_(0) {}
21  bool is_child() const { return value_ < 0; }
22  bool is_parent() const { return !is_child(); }
23  bool is_complete() const { return value_ == -1; }
24 };
25 
27 {
28  void operator=(const coroutine_ref&) = delete;
29  int& value_;
30  bool modified_;
31 
32 public:
33  coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
34  coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
35  ~coroutine_ref() { if (!modified_) value_ = -1; }
36  operator int() const { return value_; }
37  int& operator=(int v) { modified_ = true; return value_ = v; }
38 };
39 
40 #define CORO_REENTER(c) \
41  switch (coroutine_ref _coro_value = c) \
42  case -1: if (_coro_value) \
43  { \
44  goto terminate_coroutine; \
45  terminate_coroutine: \
46  _coro_value = -1; \
47  goto bail_out_of_coroutine; \
48  bail_out_of_coroutine: \
49  break; \
50  } \
51  else case 0:
52 
53 #define CORO_YIELD_IMPL(n) \
54  for (_coro_value = (n);;) \
55  if (_coro_value == 0) \
56  { \
57  case (n): ; \
58  break; \
59  } \
60  else \
61  switch (_coro_value ? 0 : 1) \
62  for (;;) \
63  case -1: if (_coro_value) \
64  goto terminate_coroutine; \
65  else for (;;) \
66  case 1: if (_coro_value) \
67  goto bail_out_of_coroutine; \
68  else case 0:
69 
70 #define CORO_FORK_IMPL(n) \
71  for (_coro_value = -(n);; _coro_value = (n)) \
72  if (_coro_value == (n)) \
73  { \
74  case -(n): ; \
75  break; \
76  } \
77  else
78 
79 #if defined(_MSC_VER)
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)
86 
87 #endif // COROUTINE_HPP
bool is_parent() const
Definition: coroutine.hpp:22
bool is_complete() const
Definition: coroutine.hpp:23
coroutine_ref(coroutine &c)
Definition: coroutine.hpp:33
void operator=(const coroutine_ref &)=delete
int value_
Definition: coroutine.hpp:17
bool is_child() const
Definition: coroutine.hpp:21
int & operator=(int v)
Definition: coroutine.hpp:37
coroutine_ref(coroutine *c)
Definition: coroutine.hpp:34