node.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (c) 2018-2024 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_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. *
  12. * RESP3 can contain recursive data structures, like a map of sets of
  13. * vectors. This class is called a node
  14. * because it can be seen as the element of the response tree. It
  15. * is a template so that users can use it with any string type, like
  16. * `std::string` or `boost::static_string`.
  17. *
  18. * @tparam String A `std::string`-like type.
  19. */
  20. template <class String>
  21. struct basic_node {
  22. /// The RESP3 type of the data in this node.
  23. type data_type = type::invalid;
  24. /// The number of elements of an aggregate.
  25. std::size_t aggregate_size{};
  26. /// The depth of this node in the response tree.
  27. std::size_t depth{};
  28. /// The actual data. For aggregate types this is usually empty.
  29. String value{};
  30. };
  31. /** @brief Compares a node for equality.
  32. * @relates basic_node
  33. *
  34. * @param a Left hand side node object.
  35. * @param b Right hand side node object.
  36. */
  37. template <class String>
  38. auto operator==(basic_node<String> const& a, basic_node<String> const& b)
  39. {
  40. // clang-format off
  41. return a.aggregate_size == b.aggregate_size
  42. && a.depth == b.depth
  43. && a.data_type == b.data_type
  44. && a.value == b.value;
  45. // clang-format on
  46. };
  47. /// A node in the response tree that owns its data.
  48. using node = basic_node<std::string>;
  49. /// A node in the response tree that does not own its data.
  50. using node_view = basic_node<std::string_view>;
  51. } // namespace boost::redis::resp3
  52. #endif // BOOST_REDIS_RESP3_NODE_HPP