noop.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  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_COBALT_NOOP_HPP
  8. #define BOOST_COBALT_NOOP_HPP
  9. #include <type_traits>
  10. #include <utility>
  11. namespace boost::cobalt
  12. {
  13. // tag::outline[]
  14. // This is a tag type allowing the creation of promises or generators without creating a coroutine.
  15. template<typename T = void>
  16. struct noop
  17. {
  18. template<typename ... Args>
  19. constexpr noop(Args && ... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...>)
  20. : value(std::forward<Args>(args)...)
  21. {
  22. }
  23. // end::outline[]
  24. T value;
  25. constexpr static bool await_ready() {return true;}
  26. template<typename P>
  27. constexpr static void await_suspend(std::coroutine_handle<P>) {}
  28. constexpr T await_resume() {return std::move(value);}
  29. // tag::outline[]
  30. };
  31. // end::outline[]
  32. template<> struct noop<void>
  33. {
  34. constexpr static bool await_ready() {return true;}
  35. template<typename P>
  36. constexpr static void await_suspend(std::coroutine_handle<P>) {}
  37. constexpr static void await_resume() {}
  38. };
  39. template<typename T> noop( T &&) -> noop<T>;
  40. template<typename T> noop(const T & ) -> noop<T>;
  41. noop() -> noop<void>;
  42. }
  43. #endif //BOOST_COBALT_NOOP_HPP