command.cpp
3.28 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
/**
* Redis C++11 wrapper.
*/
#include <vector>
#include <assert.h>
#include "command.hpp"
namespace redox {
template<class ReplyT>
bool Command<ReplyT>::is_error_reply() {
if (reply_obj->type == REDIS_REPLY_ERROR) {
std::cerr << "[ERROR] " << cmd << ": " << reply_obj->str << std::endl;
return true;
}
return false;
}
template<class ReplyT>
bool Command<ReplyT>::is_nil_reply() {
if (reply_obj->type == REDIS_REPLY_NIL) {
std::cerr << "[WARNING] " << cmd << ": Nil reply." << std::endl;
return true;
}
return false;
}
template<>
void Command<redisReply*>::invoke_callback() {
invoke(reply_obj);
}
template<>
void Command<std::string>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_STRING && reply_obj->type != REDIS_REPLY_STATUS) {
std::cerr << "[ERROR] " << cmd << ": Received non-string reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
} else {
std::string s = reply_obj->str;
invoke(s);
}
}
template<>
void Command<char*>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_STRING && reply_obj->type != REDIS_REPLY_STATUS) {
std::cerr << "[ERROR] " << cmd << ": Received non-string reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke(reply_obj->str);
}
}
template<>
void Command<int>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_INTEGER) {
std::cerr << "[ERROR] " << cmd << ": Received non-integer reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke((int) reply_obj->integer);
}
}
template<>
void Command<long long int>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_INTEGER) {
std::cerr << "[ERROR] " << cmd << ": Received non-integer reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke(reply_obj->integer);
}
}
template<>
void Command<std::nullptr_t>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(reply_obj->type != REDIS_REPLY_NIL) {
std::cerr << "[ERROR] " << cmd << ": Received non-nil reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke(nullptr);
}
}
template<>
void Command<std::vector<std::string>>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(reply_obj->type != REDIS_REPLY_ARRAY) {
std::cerr << "[ERROR] " << cmd << ": Received non-array reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
} else {
std::vector<std::string> v;
size_t count = reply_obj->elements;
for(size_t i = 0; i < count; i++) {
redisReply* r = *(reply_obj->element + i);
if(r->type != REDIS_REPLY_STRING) {
std::cerr << "[ERROR] " << cmd << ": Received non-array reply." << std::endl;
invoke_error(REDOX_WRONG_TYPE);
}
v.push_back(r->str);
}
invoke(v);
}
}
} // End namespace redox