Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Loading...
Searching...
No Matches
type.hpp
1/* Copyright (c) 2018-2023 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
22enum class type
23{
24 array,
26 push,
28 set,
30 map,
38 number,
42 boolean,
46 null,
59};
60
65auto to_string(type t) noexcept -> char const*;
66
72auto 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
auto to_string(type t) noexcept -> char const *
Converts the data type to a string.
type
RESP3 data types.
Definition: type.hpp:23
auto operator<<(std::ostream &os, type t) -> std::ostream &
Writes the type to the output stream.