adapt.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_ADAPTER_ADAPT_HPP
  7. #define BOOST_REDIS_ADAPTER_ADAPT_HPP
  8. #include <boost/redis/resp3/node.hpp>
  9. #include <boost/redis/response.hpp>
  10. #include <boost/redis/adapter/detail/result_traits.hpp>
  11. #include <boost/redis/adapter/detail/response_traits.hpp>
  12. #include <boost/mp11.hpp>
  13. #include <boost/system.hpp>
  14. #include <tuple>
  15. #include <limits>
  16. #include <string_view>
  17. #include <variant>
  18. namespace boost::redis::adapter
  19. {
  20. /** @brief Adapts a type to be used as a response.
  21. *
  22. * The type T must be either
  23. *
  24. * 1. a response<T1, T2, T3, ...> or
  25. * 2. std::vector<node<String>>
  26. *
  27. * The types T1, T2, etc can be any STL container, any integer type
  28. * and `std::string`.
  29. *
  30. * @param t Tuple containing the responses.
  31. */
  32. template<class T>
  33. auto boost_redis_adapt(T& t) noexcept
  34. {
  35. return detail::response_traits<T>::adapt(t);
  36. }
  37. /** @brief Adapts user data to read operations.
  38. * @ingroup low-level-api
  39. *
  40. * STL containers, \c resp3::response and built-in types are supported and
  41. * can be used in conjunction with \c std::optional<T>.
  42. *
  43. * Example usage:
  44. *
  45. * @code
  46. * std::unordered_map<std::string, std::string> cont;
  47. * co_await async_read(socket, buffer, adapt(cont));
  48. * @endcode
  49. *
  50. * For a transaction
  51. *
  52. * @code
  53. * sr.push(command::multi);
  54. * sr.push(command::ping, ...);
  55. * sr.push(command::incr, ...);
  56. * sr.push_range(command::rpush, ...);
  57. * sr.push(command::lrange, ...);
  58. * sr.push(command::incr, ...);
  59. * sr.push(command::exec);
  60. *
  61. * co_await async_write(socket, buffer(request));
  62. *
  63. * // Reads the response to a transaction
  64. * resp3::response<std::string, int, int, std::vector<std::string>, int> execs;
  65. * co_await resp3::async_read(socket, dynamic_buffer(buffer), adapt(execs));
  66. * @endcode
  67. */
  68. template<class T>
  69. auto adapt2(T& t = redis::ignore) noexcept
  70. { return detail::result_traits<T>::adapt(t); }
  71. } // boost::redis::adapter
  72. #endif // BOOST_REDIS_ADAPTER_ADAPT_HPP