Realistic 3D camera system
3D camera system components
coroutine.hpp
Go to the documentation of this file.
1 //
2 // coroutine.hpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_COROUTINE_HPP
12 #define ASIO_COROUTINE_HPP
13 
14 namespace asio {
15 namespace detail {
16 
17 class coroutine_ref;
18 
19 } // namespace detail
20 
22 
242 {
243 public:
245  coroutine() : value_(0) {}
246 
248  bool is_child() const { return value_ < 0; }
249 
251  bool is_parent() const { return !is_child(); }
252 
254  bool is_complete() const { return value_ == -1; }
255 
256 private:
257  friend class detail::coroutine_ref;
258  int value_;
259 };
260 
261 
262 namespace detail {
263 
265 {
266 public:
267  coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
268  coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
269  ~coroutine_ref() { if (!modified_) value_ = -1; }
270  operator int() const { return value_; }
271  int& operator=(int v) { modified_ = true; return value_ = v; }
272 private:
273  void operator=(const coroutine_ref&);
274  int& value_;
275  bool modified_;
276 };
277 
278 } // namespace detail
279 } // namespace asio
280 
281 #define ASIO_CORO_REENTER(c) \
282  switch (::asio::detail::coroutine_ref _coro_value = c) \
283  case -1: if (_coro_value) \
284  { \
285  goto terminate_coroutine; \
286  terminate_coroutine: \
287  _coro_value = -1; \
288  goto bail_out_of_coroutine; \
289  bail_out_of_coroutine: \
290  break; \
291  } \
292  else case 0:
293 
294 #define ASIO_CORO_YIELD_IMPL(n) \
295  for (_coro_value = (n);;) \
296  if (_coro_value == 0) \
297  { \
298  case (n): ; \
299  break; \
300  } \
301  else \
302  switch (_coro_value ? 0 : 1) \
303  for (;;) \
304  case -1: if (_coro_value) \
305  goto terminate_coroutine; \
306  else for (;;) \
307  case 1: if (_coro_value) \
308  goto bail_out_of_coroutine; \
309  else case 0:
310 
311 #define ASIO_CORO_FORK_IMPL(n) \
312  for (_coro_value = -(n);; _coro_value = (n)) \
313  if (_coro_value == (n)) \
314  { \
315  case -(n): ; \
316  break; \
317  } \
318  else
319 
320 #if defined(_MSC_VER)
321 # define ASIO_CORO_YIELD ASIO_CORO_YIELD_IMPL(__COUNTER__ + 1)
322 # define ASIO_CORO_FORK ASIO_CORO_FORK_IMPL(__COUNTER__ + 1)
323 #else // defined(_MSC_VER)
324 # define ASIO_CORO_YIELD ASIO_CORO_YIELD_IMPL(__LINE__)
325 # define ASIO_CORO_FORK ASIO_CORO_FORK_IMPL(__LINE__)
326 #endif // defined(_MSC_VER)
327 
328 #endif // ASIO_COROUTINE_HPP
Provides support for implementing stackless coroutines.
Definition: coroutine.hpp:241
bool is_parent() const
Returns true if the coroutine is the parent of a fork.
Definition: coroutine.hpp:251
bool is_child() const
Returns true if the coroutine is the child of a fork.
Definition: coroutine.hpp:248
coroutine()
Constructs a coroutine in its initial state.
Definition: coroutine.hpp:245
bool is_complete() const
Returns true if the coroutine has reached its terminal state.
Definition: coroutine.hpp:254