Loading...
Searching...
No Matches
type.hpp
/* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
*
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE.txt)
*/
#ifndef BOOST_REDIS_RESP3_TYPE_HPP
#define BOOST_REDIS_RESP3_TYPE_HPP
#include <boost/assert.hpp>
#include <ostream>
#include <vector>
#include <string>
namespace boost::redis::resp3 {
enum class type
{
array,
push,
set,
map,
attribute,
simple_string,
simple_error,
number,
doublean,
boolean,
big_number,
null,
blob_error,
verbatim_string,
blob_string,
streamed_string,
streamed_string_part,
invalid
};
auto to_string(type t) noexcept -> char const*;
auto operator<<(std::ostream& os, type t) -> std::ostream&;
/* Checks whether the data type is an aggregate.
*/
constexpr auto is_aggregate(type t) noexcept -> bool
{
switch (t) {
case type::array:
case type::push:
case type::set:
case type::map:
case type::attribute: return true;
default: return false;
}
}
// For map and attribute data types this function returns 2. All
// other types have value 1.
constexpr auto element_multiplicity(type t) noexcept -> std::size_t
{
switch (t) {
case type::map:
case type::attribute: return 2ULL;
default: return 1ULL;
}
}
// Returns the wire code of a given type.
constexpr auto to_code(type t) noexcept -> char
{
switch (t) {
case type::blob_error: return '!';
case type::verbatim_string: return '=';
case type::blob_string: return '$';
case type::streamed_string_part: return ';';
case type::simple_error: return '-';
case type::number: return ':';
case type::doublean: return ',';
case type::boolean: return '#';
case type::big_number: return '(';
case type::simple_string: return '+';
case type::null: return '_';
case type::push: return '>';
case type::set: return '~';
case type::array: return '*';
case type::attribute: return '|';
case type::map: return '%';
default: BOOST_ASSERT(false); return ' ';
}
}
// Converts a wire-format RESP3 type (char) to a resp3 type.
constexpr auto to_type(char c) noexcept -> type
{
switch (c) {
case '!': return type::blob_error;
case '=': return type::verbatim_string;
case '$': return type::blob_string;
case ';': return type::streamed_string_part;
case '-': return type::simple_error;
case ':': return type::number;
case ',': return type::doublean;
case '#': return type::boolean;
case '(': return type::big_number;
case '+': return type::simple_string;
case '_': return type::null;
case '>': return type::push;
case '~': return type::set;
case '*': return type::array;
case '|': return type::attribute;
case '%': return type::map;
default: return type::invalid;
}
}
} // boost::redis::resp3
#endif // BOOST_REDIS_RESP3_TYPE_HPP
boost::redis::resp3::to_string
auto to_string(type t) noexcept -> char const *
Converts the data type to a string.
boost::redis::resp3::type
type
RESP3 data types.
Definition: type.hpp:23
boost::redis::resp3::operator<<
auto operator<<(std::ostream &os, type t) -> std::ostream &
Writes the type to the output stream.
boost::redis::resp3::type::streamed_string_part
@ streamed_string_part
Simple.
boost::redis::resp3::type::map
@ map
Aggregate.
boost::redis::resp3::type::push
@ push
Aaggregate.
boost::redis::resp3::type::blob_string
@ blob_string
Simple.
boost::redis::resp3::type::streamed_string
@ streamed_string
Simple.
boost::redis::resp3::type::null
@ null
Simple.
boost::redis::resp3::type::simple_string
@ simple_string
Simple.
boost::redis::resp3::type::boolean
@ boolean
Simple.
boost::redis::resp3::type::verbatim_string
@ verbatim_string
Simple.
boost::redis::resp3::type::simple_error
@ simple_error
Simple.
boost::redis::resp3::type::big_number
@ big_number
Simple.
boost::redis::resp3::type::number
@ number
Simple.
boost::redis::resp3::type::blob_error
@ blob_error
Simple.
boost::redis::resp3::type::doublean
@ doublean
Simple.
boost::redis::resp3::type::set
@ set
Aggregate.
boost::redis::resp3::type::attribute
@ attribute
Aggregate.
boost::redis::resp3::type::array
@ array
Aggregate.
boost::redis::resp3::type::invalid
@ invalid
Invalid.