Boost.Redis: include/boost/redis/resp3/type.hpp Source File

Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
type.hpp
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
7#ifndef BOOST_REDIS_RESP3_TYPE_HPP
8#define BOOST_REDIS_RESP3_TYPE_HPP
9
10#include <boost/assert.hpp>
11#include <ostream>
12#include <vector>
13#include <string>
14
15namespace boost::redis::resp3 {
16
22 enum class type
23{
24 array,
26 push,
28 set,
30 map,
32 attribute,
34 simple_string,
36 simple_error,
38 number,
40 doublean,
42 boolean,
44 big_number,
46 null,
48 blob_error,
50 verbatim_string,
52 blob_string,
54 streamed_string,
56 streamed_string_part,
58 invalid
59};
60
65 auto to_string(type t) noexcept -> char const*;
66
72 auto operator<<(std::ostream& os, type t) -> std::ostream&;
73
74/* Checks whether the data type is an aggregate.
75 */
76constexpr auto is_aggregate(type t) noexcept -> bool
77{
78 switch (t) {
79 case type::array:
80 case type::push:
81 case type::set:
82 case type::map:
83 case type::attribute: return true;
84 default: return false;
85 }
86}
87
88// For map and attribute data types this function returns 2. All
89// other types have value 1.
90constexpr auto element_multiplicity(type t) noexcept -> std::size_t
91{
92 switch (t) {
93 case type::map:
94 case type::attribute: return 2ULL;
95 default: return 1ULL;
96 }
97}
98
99// Returns the wire code of a given type.
100constexpr auto to_code(type t) noexcept -> char
101{
102 switch (t) {
103 case type::blob_error: return '!';
104 case type::verbatim_string: return '=';
105 case type::blob_string: return '$';
106 case type::streamed_string_part: return ';';
107 case type::simple_error: return '-';
108 case type::number: return ':';
109 case type::doublean: return ',';
110 case type::boolean: return '#';
111 case type::big_number: return '(';
112 case type::simple_string: return '+';
113 case type::null: return '_';
114 case type::push: return '>';
115 case type::set: return '~';
116 case type::array: return '*';
117 case type::attribute: return '|';
118 case type::map: return '%';
119
120 default: BOOST_ASSERT(false); return ' ';
121 }
122}
123
124// Converts a wire-format RESP3 type (char) to a resp3 type.
125constexpr auto to_type(char c) noexcept -> type
126{
127 switch (c) {
128 case '!': return type::blob_error;
129 case '=': return type::verbatim_string;
130 case '$': return type::blob_string;
131 case ';': return type::streamed_string_part;
132 case '-': return type::simple_error;
133 case ':': return type::number;
134 case ',': return type::doublean;
135 case '#': return type::boolean;
136 case '(': return type::big_number;
137 case '+': return type::simple_string;
138 case '_': return type::null;
139 case '>': return type::push;
140 case '~': return type::set;
141 case '*': return type::array;
142 case '|': return type::attribute;
143 case '%': return type::map;
144 default: return type::invalid;
145 }
146}
147
148} // boost::redis::resp3
149
150#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.