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