Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:08:24

0001 /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
0002  *
0003  * Distributed under the Boost Software License, Version 1.0. (See
0004  * accompanying file LICENSE.txt)
0005  */
0006 
0007 #ifndef BOOST_REDIS_RESP3_TYPE_HPP
0008 #define BOOST_REDIS_RESP3_TYPE_HPP
0009 
0010 #include <boost/assert.hpp>
0011 #include <ostream>
0012 #include <vector>
0013 #include <string>
0014 
0015 namespace boost::redis::resp3 {
0016 
0017 /** \brief RESP3 data types.
0018     \ingroup high-level-api
0019   
0020     The RESP3 specification can be found at https://github.com/redis/redis-specifications/blob/master/protocol/RESP3.md.
0021  */
0022 enum class type
0023 { /// Aggregate
0024   array,
0025   /// Aaggregate
0026   push,
0027   /// Aggregate
0028   set,
0029   /// Aggregate
0030   map,
0031   /// Aggregate
0032   attribute,
0033   /// Simple
0034   simple_string,
0035   /// Simple
0036   simple_error,
0037   /// Simple
0038   number,
0039   /// Simple
0040   doublean,
0041   /// Simple
0042   boolean,
0043   /// Simple
0044   big_number,
0045   /// Simple
0046   null,
0047   /// Simple
0048   blob_error,
0049   /// Simple
0050   verbatim_string,
0051   /// Simple
0052   blob_string,
0053   /// Simple
0054   streamed_string,
0055   /// Simple
0056   streamed_string_part,
0057   /// Invalid
0058   invalid
0059 };
0060 
0061 /** \brief Converts the data type to a string.
0062  *  \ingroup high-level-api
0063  *  \param t RESP3 type.
0064  */
0065 auto to_string(type t) noexcept -> char const*;
0066 
0067 /** \brief Writes the type to the output stream.
0068  *  \ingroup high-level-api
0069  *  \param os Output stream.
0070  *  \param t RESP3 type.
0071  */
0072 auto operator<<(std::ostream& os, type t) -> std::ostream&;
0073 
0074 /* Checks whether the data type is an aggregate.
0075  */
0076 constexpr auto is_aggregate(type t) noexcept -> bool
0077 {
0078    switch (t) {
0079       case type::array:
0080       case type::push:
0081       case type::set:
0082       case type::map:
0083       case type::attribute: return true;
0084       default: return false;
0085    }
0086 }
0087 
0088 // For map and attribute data types this function returns 2.  All
0089 // other types have value 1.
0090 constexpr auto element_multiplicity(type t) noexcept -> std::size_t
0091 {
0092    switch (t) {
0093       case type::map:
0094       case type::attribute: return 2ULL;
0095       default: return 1ULL;
0096    }
0097 }
0098 
0099 // Returns the wire code of a given type.
0100 constexpr auto to_code(type t) noexcept -> char
0101 {
0102    switch (t) {
0103       case type::blob_error:           return '!';
0104       case type::verbatim_string:      return '=';
0105       case type::blob_string:          return '$';
0106       case type::streamed_string_part: return ';';
0107       case type::simple_error:         return '-';
0108       case type::number:               return ':';
0109       case type::doublean:             return ',';
0110       case type::boolean:              return '#';
0111       case type::big_number:           return '(';
0112       case type::simple_string:        return '+';
0113       case type::null:                 return '_';
0114       case type::push:                 return '>';
0115       case type::set:                  return '~';
0116       case type::array:                return '*';
0117       case type::attribute:            return '|';
0118       case type::map:                  return '%';
0119 
0120       default: BOOST_ASSERT(false); return ' ';
0121    }
0122 }
0123 
0124 // Converts a wire-format RESP3 type (char) to a resp3 type.
0125 constexpr auto to_type(char c) noexcept -> type
0126 {
0127    switch (c) {
0128       case '!': return type::blob_error;
0129       case '=': return type::verbatim_string;
0130       case '$': return type::blob_string;
0131       case ';': return type::streamed_string_part;
0132       case '-': return type::simple_error;
0133       case ':': return type::number;
0134       case ',': return type::doublean;
0135       case '#': return type::boolean;
0136       case '(': return type::big_number;
0137       case '+': return type::simple_string;
0138       case '_': return type::null;
0139       case '>': return type::push;
0140       case '~': return type::set;
0141       case '*': return type::array;
0142       case '|': return type::attribute;
0143       case '%': return type::map;
0144       default: return type::invalid;
0145    }
0146 }
0147 
0148 } // boost::redis::resp3
0149 
0150 #endif // BOOST_REDIS_RESP3_TYPE_HPP