command.cpp
2.61 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
/**
* Redis C++11 wrapper.
*/
#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() {
// std::cout << "invoking int callback" << std::endl;
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);
}
}
} // End namespace redox