forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathreply.cpp
More file actions
167 lines (128 loc) · 4.56 KB
/
reply.cpp
File metadata and controls
167 lines (128 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// The MIT License (MIT)
//
// Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <cpp_redis/core/reply.hpp>
#include <cpp_redis/misc/error.hpp>
#include <cpp_redis/misc/logger.hpp>
namespace cpp_redis {
reply::reply() : m_type(type::null) {}
reply::reply(const string_t &value, string_type reply_type)
: m_type(static_cast<type>(reply_type)), m_str_val(value) {}
reply::reply(int_t value) : m_type(type::integer), m_int_val(value) {}
reply::reply(const std::vector<reply> &rows)
: m_type(type::array), m_rows(rows) {}
reply::reply(reply &&other) noexcept {
m_type = other.m_type;
m_rows = std::move(other.m_rows);
m_str_val = std::move(other.m_str_val);
m_int_val = other.m_int_val;
}
optional_t<int_t> reply::try_get_int() const {
if (is_integer())
return optional_t<int_t>(m_int_val);
__CPP_REDIS_LOG(1, "Reply is not an integer");
return {0};
}
reply &reply::operator=(reply &&other) noexcept {
if (this != &other) {
m_type = other.m_type;
m_rows = std::move(other.m_rows);
m_str_val = std::move(other.m_str_val);
m_int_val = other.m_int_val;
}
return *this;
}
bool reply::ok() const { return !is_error(); }
bool reply::ko() const { return !ok(); }
const string_t &reply::error() const {
if (!is_error())
throw cpp_redis::redis_error("Reply is not an error");
return as_string();
}
reply::operator bool() const { return !is_error() && !is_null(); }
void reply::set() { m_type = type::null; }
void reply::set(const string_t &value, string_type reply_type) {
m_type = static_cast<type>(reply_type);
m_str_val = value;
}
void reply::set(int_t value) {
m_type = type::integer;
m_int_val = value;
}
void reply::set(const std::vector<reply> &rows) {
m_type = type::array;
m_rows = rows;
}
reply &reply::operator<<(const reply &reply) {
m_type = type::array;
m_rows.push_back(reply);
return *this;
}
bool reply::is_array() const { return m_type == type::array; }
bool reply::is_string() const {
return is_simple_string() || is_bulk_string() || is_error();
}
bool reply::is_simple_string() const { return m_type == type::simple_string; }
bool reply::is_bulk_string() const { return m_type == type::bulk_string; }
bool reply::is_error() const { return m_type == type::error; }
bool reply::is_integer() const { return m_type == type::integer; }
bool reply::is_null() const { return m_type == type::null; }
const std::vector<reply> &reply::as_array() const {
if (!is_array())
throw cpp_redis::redis_error("Reply is not an array");
return m_rows;
}
const string_t &reply::as_string() const {
if (!is_string())
throw cpp_redis::redis_error("Reply is not a string");
return m_str_val;
}
int_t reply::as_integer() const {
if (!is_integer())
throw cpp_redis::redis_error("Reply is not an integer");
return m_int_val;
}
reply::type reply::get_type() const { return m_type; }
} // namespace cpp_redis
std::ostream &operator<<(std::ostream &os, const cpp_redis::reply_t &reply) {
switch (reply.get_type()) {
case cpp_redis::reply::type::error:
os << reply.error();
break;
case cpp_redis::reply::type::bulk_string:
os << reply.as_string();
break;
case cpp_redis::reply::type::simple_string:
os << reply.as_string();
break;
case cpp_redis::reply::type::null:
os << cpp_redis::string_t("(nil)");
break;
case cpp_redis::reply::type::integer:
os << reply.as_integer();
break;
case cpp_redis::reply::type::array:
for (const auto &item : reply.as_array())
os << item;
break;
}
return os;
}