coroutine.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_IMPL_INTERNAL_COROUTINE_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_COROUTINE_HPP
  9. // asio::coroutine uses __COUNTER__ internally, which can trigger
  10. // ODR violations if we use them in header-only code. These manifest as
  11. // extremely hard-to-debug bugs only present in release builds
  12. // Coroutine state is represented as an integer (resume_point_var).
  13. // Every yield gets assigned a unique value (resume_point_id).
  14. // Yielding sets the next resume point, returns, and sets a case label for re-entering.
  15. // Coroutines need to switch on resume_point_var to re-enter.
  16. // Enclosing this in a scope allows placing the macro inside a brace-less for/while loop
  17. // The empty scope after the case label is required because labels can't be at the end of a compound statement
  18. #define BOOST_MYSQL_YIELD(resume_point_var, resume_point_id, ...) \
  19. { \
  20. resume_point_var = resume_point_id; \
  21. return __VA_ARGS__; \
  22. case resume_point_id: \
  23. { \
  24. } \
  25. }
  26. #define BOOST_MYSQL_YIELD_VOID(resume_point_var, resume_point_id) \
  27. BOOST_MYSQL_YIELD(resume_point_var, resume_point_id, static_cast<void>(0))
  28. #endif