response.hpp 2.2 KB

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