response.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (c) 2018-2025 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_RESPONSE_HPP
  7. #define BOOST_REDIS_RESPONSE_HPP
  8. #include <boost/redis/adapter/result.hpp>
  9. #include <boost/redis/resp3/node.hpp>
  10. #include <boost/system/error_code.hpp>
  11. #include <string>
  12. #include <tuple>
  13. #include <vector>
  14. namespace boost::redis {
  15. /// Response with compile-time size.
  16. template <class... Ts>
  17. using response = std::tuple<adapter::result<Ts>...>;
  18. /** @brief A generic response to a request
  19. *
  20. * This response type can store any type of RESP3 data structure. It
  21. * contains the
  22. * [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
  23. * view of the response tree.
  24. */
  25. using generic_response = adapter::result<std::vector<resp3::node>>;
  26. /** @brief Consume on response from a generic response
  27. *
  28. * This function rotates the elements so that the start of the next
  29. * response becomes the new front element. For example the output of
  30. * the following code
  31. *
  32. * @code
  33. * request req;
  34. * req.push("PING", "one");
  35. * req.push("PING", "two");
  36. * req.push("PING", "three");
  37. *
  38. * generic_response resp;
  39. * co_await conn.async_exec(req, resp);
  40. *
  41. * std::cout << "PING: " << resp.value().front().value << std::endl;
  42. * consume_one(resp);
  43. * std::cout << "PING: " << resp.value().front().value << std::endl;
  44. * consume_one(resp);
  45. * std::cout << "PING: " << resp.value().front().value << std::endl;
  46. * @endcode
  47. *
  48. * Is:
  49. *
  50. * @code
  51. * PING: one
  52. * PING: two
  53. * PING: three
  54. * @endcode
  55. *
  56. * Given that this function rotates elements, it won't be very
  57. * efficient for responses with a large number of elements. It was
  58. * introduced mainly to deal with buffers server pushes as shown in
  59. * the cpp20_subscriber.cpp example. In the future queue-like
  60. * responses might be introduced to consume in O(1) operations.
  61. *
  62. * @param r The response to modify.
  63. * @param ec Will be populated in case of error.
  64. */
  65. void consume_one(generic_response& r, system::error_code& ec);
  66. /**
  67. * @brief Throwing overload of `consume_one`.
  68. *
  69. * @param r The response to modify.
  70. */
  71. void consume_one(generic_response& r);
  72. } // namespace boost::redis
  73. #endif // BOOST_REDIS_RESPONSE_HPP