any_resumable_ref.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_DETAIL_ANY_RESUMABLE_REF_HPP
  8. #define BOOST_MYSQL_DETAIL_ANY_RESUMABLE_REF_HPP
  9. #include <boost/mysql/detail/next_action.hpp>
  10. #include <cstddef>
  11. #include <type_traits>
  12. namespace boost {
  13. namespace mysql {
  14. namespace detail {
  15. class any_resumable_ref
  16. {
  17. template <class T>
  18. static next_action do_resume(void* self, error_code ec, std::size_t bytes_transferred)
  19. {
  20. return static_cast<T*>(self)->resume(ec, bytes_transferred);
  21. }
  22. using fn_t = next_action (*)(void*, error_code, std::size_t);
  23. void* algo_{};
  24. fn_t fn_{};
  25. public:
  26. template <class T, class = typename std::enable_if<!std::is_same<T, any_resumable_ref>::value>::type>
  27. explicit any_resumable_ref(T& op) noexcept : algo_(&op), fn_(&do_resume<T>)
  28. {
  29. }
  30. next_action resume(error_code ec, std::size_t bytes_transferred)
  31. {
  32. return fn_(algo_, ec, bytes_transferred);
  33. }
  34. };
  35. } // namespace detail
  36. } // namespace mysql
  37. } // namespace boost
  38. #endif