any_resumable_ref.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Copyright (c) 2019-2025 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. public:
  18. using fn_t = next_action (*)(void*, error_code, std::size_t);
  19. template <class T, class = typename std::enable_if<!std::is_same<T, any_resumable_ref>::value>::type>
  20. explicit any_resumable_ref(T& op) noexcept : algo_(&op), fn_(&do_resume<T>)
  21. {
  22. }
  23. // Allow using standalone functions
  24. any_resumable_ref(void* algo, fn_t fn) noexcept : algo_(algo), fn_(fn) {}
  25. next_action resume(error_code ec, std::size_t bytes_transferred)
  26. {
  27. return fn_(algo_, ec, bytes_transferred);
  28. }
  29. private:
  30. template <class T>
  31. static next_action do_resume(void* self, error_code ec, std::size_t bytes_transferred)
  32. {
  33. return static_cast<T*>(self)->resume(ec, bytes_transferred);
  34. }
  35. void* algo_{};
  36. fn_t fn_{};
  37. };
  38. } // namespace detail
  39. } // namespace mysql
  40. } // namespace boost
  41. #endif