node.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_RESP3_NODE_HPP
  7. #define BOOST_REDIS_RESP3_NODE_HPP
  8. #include <boost/redis/resp3/type.hpp>
  9. namespace boost::redis::resp3 {
  10. /** \brief A node in the response tree.
  11. * \ingroup high-level-api
  12. *
  13. * RESP3 can contain recursive data structures: A map of sets of
  14. * vector of etc. As it is parsed each element is passed to user
  15. * callbacks (push parser). The signature of this
  16. * callback is `f(resp3::node<std::string_view)`. This class is called a node
  17. * because it can be seen as the element of the response tree. It
  18. * is a template so that users can use it with owing strings e.g.
  19. * `std::string` or `boost::static_string` etc.
  20. *
  21. * @tparam String A `std::string`-like type.
  22. */
  23. template <class String>
  24. struct basic_node {
  25. /// The RESP3 type of the data in this node.
  26. type data_type = type::invalid;
  27. /// The number of elements of an aggregate.
  28. std::size_t aggregate_size{};
  29. /// The depth of this node in the response tree.
  30. std::size_t depth{};
  31. /// The actual data. For aggregate types this is usually empty.
  32. String value{};
  33. };
  34. /** @brief Compares a node for equality.
  35. * @relates basic_node
  36. *
  37. * @param a Left hand side node object.
  38. * @param b Right hand side node object.
  39. */
  40. template <class String>
  41. auto operator==(basic_node<String> const& a, basic_node<String> const& b)
  42. {
  43. return a.aggregate_size == b.aggregate_size
  44. && a.depth == b.depth
  45. && a.data_type == b.data_type
  46. && a.value == b.value;
  47. };
  48. /** @brief A node in the response tree.
  49. * @ingroup high-level-api
  50. */
  51. using node = basic_node<std::string>;
  52. } // boost::redis::resp3
  53. #endif // BOOST_REDIS_RESP3_NODE_HPP